你还不知道什么是Qt?... ...什么?你还不知道C++能快速开发部署GUI?...you got be kidding me
这篇文章,教你怎么用Qt Creator2.0来做一个GUI。下载地址什么的不罗嗦,google之。我这里给出的,是每个QT GUI 工程用QT creator来做时的流程。
假设你已经安装好了qt sdk。
1.文件->新建工程或文件。
2.选择Qt控件项目->QT GUI应用。
3.取消 “隐藏生成的文件选项”
4.布局ui文件(这步的详细操作略去,很多书都会说这个,我只说书上说得不清楚的)
5.编译该ui文件,这时,会出现一个ui_xxx.h的文件。
接下来,就是代码修改了。这里分别给出原始文件和修改后的文件内容。代码注释在修改后的文件上。
以建立一个叫test的工程为例,修改后的工程,可以执行对话框的功能。
一、首先修改test.h
test.h未修改前代码:(需要说明的是,我的修改前代码和你的可能不同,不过没影响)
-
#ifndef TEST_H
-
#define TEST_H
-
-
#include
-
-
namespace Ui {
-
class test;
-
}
-
-
class test : public QMainWindow
-
{
-
Q_OBJECT
-
-
public:
-
explicit test(QWidget *parent = 0);
-
~test();
-
-
private:
-
Ui::test *ui;
-
};
-
-
#endif // TEST_H
修改后的test.h代码
-
#ifndef TEST_H
-
#define TEST_H
-
-
#include //因为要继承QDialog
-
#include "ui_test.h" //引用了ui_test.h,ui_test.h文件不能做任何更改,即使做了更改,在再次编译test.ui的时候,这些更改都会消失
-
-
-
class GoToCellDialog : public QDialog, public Ui::GoToCellDialog
-
-
-
{
-
Q_OBJECT
-
-
public:
-
explicit GoToCellDialog(QWidget *parent = 0);
-
-
~GoToCellDialog();
-
-
private slots:
-
void on_lineEdit_textChanged();
-
-
-
-
};
-
-
#endif // TEST_H
这里的操作很简单:
1.删除关于namespace的声明代码段,因为已经在ui_test.h中有了声明
2.添加slot或者任何你想添加的函数
二、接下来修改test.cpp代码
test.cpp未修改前代码
-
#include "test.h"
-
#include "ui_test.h"
-
-
test::test(QWidget *parent) :
-
QMainWindow(parent),
-
ui(new Ui::test)
-
{
-
ui->setupUi(this);
-
}
-
-
test::~test()
-
{
-
delete ui;
-
}
这里的操作:
1.删除 "ui_test.h"的引用,因为在test.h中已经完成了
2. 增加#include 的引用
3.构造函数的实现,函数名 删除ui(new Ui::test)
4.ui->setupUi(this);改成setupUi(this);
5.做connect操作
6.实现slot函数和任何在test.h中声明的函数
7.实现析构函数,析构函数已经存在了,把里面那句话删掉,因为ui已经不存在了撒~
test.cpp修改后代码
-
#include
-
#include "test.h"
-
-
-
GoToCellDialog::GoToCellDialog(QWidget *parent) :
-
QDialog(parent)
-
{
-
-
setupUi(this);
-
-
QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
-
lineEdit->setValidator(new QRegExpValidator(regExp, this));
-
-
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
-
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
-
-
-
}
-
-
void GoToCellDialog::on_lineEdit_textChanged()
-
{
-
okButton->setEnabled(lineEdit->hasAcceptableInput());
-
}
-
-
GoToCellDialog::~GoToCellDialog()
-
{
-
-
}
三、修改main.cpp代码[/b]
main.cpp修改前代码
-
#include
-
#include "test.h"
-
-
int main(int argc, char *argv[])
-
{
-
QApplication a(argc, argv);
-
test w;
-
w.show();
-
-
return a.exec();
-
}
这里
1.删除对的引用,因为用不到
2.实现一个类,然后show它
main.cpp修改后代码
-
#include
-
-
#include "test.h"
-
-
int main(int argc, char *argv[])
-
{
-
QApplication a(argc, argv);
-
GoToCellDialog *dialog = new GoToCellDialog;
-
dialog->show();
-
-
return a.exec();
-
}
然后编译运行,搞定。
可以说,基本上每个QT GUI工程都是按照这个来做的。修改后的代码执行一个对话框的功能。