Qt Designer下的一些基礎操作


第一個Qt Designer程序設計(Visual Stdio下)

<一>、在vs下新建一個Qt Console Application工程,打開designer,就開始布局你的窗口吧。

<二>、打開designer后,會讓你新建一個form文件,可以選繼承自QDialog、QWidget、QMainWindow、QFrame等

<三>、選好上一步后(我們以QMainWindow為列):

1、可以直接添加菜單欄,菜單欄下的action事件的

2、添加工具欄:在窗口空白處右鍵->選擇添加工具欄(可以添加多個)。Icon圖標下是否有文本:設置工具欄屬性:toolButtonStyle為ToolButtonTextUnderIcon/ToolButtonIconOnly。代碼為:toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

注意:為何窗口最大化時,布局能夠按照自己的需求比例增長,最后所有布局好了后,在主窗口設置頂級布局:
主窗口空白處->右鍵,選中布局->再選一次你最后一次(最外層布局)的布局(vertical / horizonal / grid)即可。

3、在工具欄中添加Icon圖標:
(1)、新建一個qrc文件:如mainwindow.qrc(位置:在自己項目的根目錄下),然后代碼如下:

<RCC>
    <qresource prefix="/QT_DESIGNER_TEST">
          <file>Icon/open.png</file>
    </qresource>
</RCC>
注意:Icon文件夾就放在根目錄下的,QT_DESIGNER_TEST為工程名文件夾。
(2)、添加資源文件(qrc文件):
選中動作編輯器->新建一個動作->圖標欄->選擇資源->編輯資源->選擇自己需要的圖標->確定
(3)、然后,把剛才新建的動作(action)拖到工具欄即可

<四>、把自己需要的控件放到QMainWindow上,回到vs中個編譯,得到一個ui_mainiwindow.h文件

<五>、在工程中,自己新建一個mainwindow.h和mainwindow.cpp
//mainwindow.h

#ifndef CWINDOW_H
#define CWINDOW_H

#include "GeneratedFiles/ui_mainwindow.h"
#include <QMainWindow>

class CWindow : public QMainWindow
{
Q_OBJECT

public:
CWindow();

private:
Ui::MainWindow ui;
};

#endif

//mainwindow.cpp

#include "window.h"

CWindow::CWindow()
{
ui.setupUi(this);
connect(ui.actionExit, SIGNAL(triggered()), this, SLOT(close()));
}

<六>、在main函數中,添加:
MainWindow manwin;
mainwin.show();
編譯運行,就ok了

注:在designer里可以改變每個控件的變量名的。如果要在QMainWindow上添加QGLWidget的類,如下:

class GLArea : public QGLWidget

{
public:
Q_OBJECT

public:
GLArea(QWidget *parent = 0);
~GLArea(void);

void initializeGL();
void resizeGL(int w, int h);
void paintGL();

}

GLArea::GLArea(QWidget *parent): QGLWidget(/*QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer |QGL::SampleBuffers),*/ parent)

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
~MainWindow();

}

MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
cout << "MainWindow constructed" << endl;
ui.setupUi(this);
area = new GLArea(this);//GLArea是繼承的QGLWidget類
setCentralWidget(area);

}

<七>、添加狀態欄

CMainWindow::CMainWindow()
{

ui.setupUi(this);
connect(ui.actionExit, SIGNAL(triggered()), this, SLOT(close()));
iniStatusBar();
}

void CWindow::iniStatusBar()
{
QStatusBar *statusBar = statusBar();

statusLbl_ = new QLabel;
statusLbl_->setMinimumSize(100,30);
statusLbl_->setIndent(2);
statusLbl_->setFrameShape(QFrame::NoFrame);
statusLbl_->setFrameShadow(QFrame::Plain);
statusLbl_->setText(tr("status : 111"));

//updateStatusBar();

statusBar->setLayoutDirection(Qt::RightToLeft);
statusBar->addWidget(statusLbl_);

//update();
//repaint();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM