Chapter6 Qt程序基本开发方法和流程
1,开发方法
(1)全部采取手写代码,在命令行下完成编译和运行
(2)在集成开发环境(IDE)中采用手写代码(包括设计界面),使用IDE完成编译与运行
(3)使用Qt Designer设计界面,用IDE完成编译和运行
2,示例hello qt!
代码
#include <QApplication>
#include <QPushButton>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QPushButton pushButton(QObject::tr(“Hello Qt!”);
pushButton.show();
QObject::connect(&pushButton,SIGNAL(clicked()),&app,SLOT(quit()));
return app.exec();
}
编译运行方法:
进入hello.cpp所在目录,输入qmake -project
qmake hello.pro
make
./hello
或者
打开Qt Creator ,加入Qt空项目;新建c++文件
3.几个重要的知识点
(1)信号和槽
信号和槽提供了任意两个对象间的通信机制。每个Qt对象都包含特定的信号和槽以及相应的关联定义,当有事件发生或者对象的状态发生改变时,对应的信号就被发射出来,与其相关联的槽被执行以响应该信号,完成处理工作。
一般格式:
connect(OBJECT1,SIGNAL(signal),OBJECT2,SLOT(slot));
(2)构建Qt应用程序的流程
(3)编译Qt应用程序的方法
(4)qmake文件的结构
前几行注释
TARGET变量描述了目标工程文件的名称
TEMPLATE 变量描述了生成什么样的makefile(app,lib,subdirs,vcapp,vclib)
SOURCES变量描述了源代码文件相对于工程文件的位置以及文件名字
HEADERS变量告诉编译器头文件的路径
FORMS变量告诉编译器.ui文件的位置
Chapter7 对话框
Qt如何创建对话框?
Qt内建的对话框类
1,QDialog类
QDialog类是对话框窗口的基类。对话框窗口是一个顶级窗口,通常用作短期任务,或者与用户的简短会话。
分为模态对话框和非模态对话框
父类是QWidget,子类一堆,常用的有QColorDialog,QFileDialog,QInputDialog,QMessage-Box
2,子类化QDialog
(1)实例名:extensionDlg
(2)组成:主程序文件main.cpp
工程文件extensionDlg.pro
对话框类ExtensionDlg.h,ExtensionDlg.c
(3)ExtensionDlg.h:
#ifnedf EXTENSIONDLG_H
#define EXTENSIONDLG_H
#include <QtGui>
class ExtensionDLg:public QDialog
{
Q_OBJECT;
public:
ExtensionDlg();
void initBasicInfo();
void initDetailInfo();
public slots:
void slot2Extension();
private:
QWidgetbaseWidget;
QWidgetdetailWidget;
};
#endif
(4)ExtensionDlg.c
#include “ExtensionDlg.h”
ExtensionDlg::ExtensionDlg()
{
setWindowTitle(tr(“Extension Dialog”));
initBasicInfo();
initDetailInfo();
QVBoxLayout *layout = new QVboxLayout;
layout -> addWidget(baseWidget);
layout -> addWidget(detailWidget);
layout -> setSizeConstraint(QLayout::SetFixedSize);
layout -> setSpacing(6);
setLayout(layout);
}
void ExtensionDlg::initBsicInfo()
{
baseWidget = new QWidget;
QLabel *nameLabel= new QLabel(tr(“Name”));
QLineEdit *nameEdit = new QLineEdit;
QLabel *sexLabel= new QLabel(tr(“Sex”));
QComboBox *sexComboBox = new QComboBox;
sexComboBox->addItem(tr(“male”));
sexComboBox->addItem(tr(“female”));
QPushButton *okButton = new QPushButton(tr(“ok”));
QPushButton *detailButton = new QPushButton(tr(“detail”));
connect(detailButton,SIGNAL(clicked()),this,SLOT(slot2Extension());
QDialogButtonBox *btnBox=new QDialogButtonBox(Qt::Horizontal);
btnBox->addButton(okButton,QDialogButtonBox::ActionRole);
btnBox->addButton(detailButton,QDialogButtonBox::ActionRole);
QFormLayout *formLayout = new QFormLayout;
formLayout->addRow(nameLabel,nameEdit);
formLayout->addRow(sexLabel,setComboBox);
QVboxLayout *vboxLayout=new QVboxLayout;
vboxLayout->addLayout(formLayout);
vboxLayout->addWidget(btnBox);
baseWidget->setLayout(vboxLayout);
}
void ExtensionDlg::initDetailInfo()
{
detailWidget = new QWidget;
QLabel *ageLabel= new QLabel(tr(“Age”));
QLineEdit *ageEdit = new QLineEdit;
ageEdit->setText(tr(25));
QLabel *deptLabel= new QLabel(tr(“department”));
QComboBox *deptComboBox = new QComboBox;
deptComboBox->addItem(tr(“department 1”);
deptComboBox->addItem(tr(“department 2”);
deptComboBox->addItem(tr(“department 3”);
deptComboBox->addItem(tr(“department 4”);
QLabel *addressLabel= new QLabel(tr(“Address”));
QLineEdit *addressEdit = new QLineEdit;
QFormLayout *formLayout = new QFormLayout;
formLayout->addRow(ageLabel,ageEdit);
formLayout->addRow(addressLabel,addressEdit);
detailWidget->setLayout(formLayout);
detailWidget->hide;
}
void ExtensionDlg::slot2Extension()
{
if(detailWidget->isHidden())
{
detailWidget->show();
}
else
{detaliWidget->show;}
}
(5)main.c
#include <QApplication>
#include “ExtensionDlg.h”
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
ExtensionDlg exDlg;
exDlg.show();
return app.exec;
}
(6)ExtensionDlg.pro
TEMPLATE = app
TARGET =
DEPENDPATH + =
INCLUDEPATH + =
#INPUT
HEADERS + = extensionDlg.h
SOURCES + = extensionDlg.cpp main.cpp
3.快速设计对话框
首先新建项目
然后Qt Designer设置GUI(见Chapter 5),生成extensionDlgDesigner.ui
第三步,创建extensionDlg.h
#ifnedf EXTENSIONDLG_H
#define EXTENSIONDLG_H
#include <QtGui>
#include “ui_extensionDlg.h”
class ExtensionDLg:public QDialog,public Ui::Dialog
{
Q_OBJECT;
public:
ExtensionDlg(QWidgetparent=0);
};
#endif
第四步,main.c
#include <QApplication>
#include “ExtensionDlg.h”
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
ExtensionDlg *exDlg= new ExtensionDlg;
exDlg->show();
return app.exec;
}
4,常见内建对话框的使用(建议查看书)
(1)标准输入框QInputDialog
常用方法:getText(),getInt,getDouble(),getItem()
(2)标准颜色对话框QColorDialog
(3)标准字体对话框
(4)标准消息对话框
(5)标准对话框的使用实例
5,模态对话框和非模态对话框