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();
}

posted @ 2014-11-27 12:35  DaiHong  阅读(12067)  评论(0编辑  收藏  举报