介绍使用Qt Designer,从简单的hello qt程序开始,(假设我们已安装好Qt)

     1、新建文件夹 helloQT。
     2、打开qt designer。点击“应用程序”-“编程”-“Qt Designer”;或者在终端里输入命令:designer。
     3、选择“File”--“New”,选择“Widget”,然后“Create”。
     4、拖入“PushButton” 和“Label”。
     5、保存为 helloQT.ui ,然后关闭 qt designer 。
     6、在helloQT文件夹里右击打开终端,输入命令:uic helloQT.ui -o ui_helloQT.h
     7、编写程序,在helloQT文件夹里:
         1)  新建文件main.cpp。输入程序:

 
  
  1. #include <QtGui/QApplication>  
  2. #include "helloQT.h"  
  3.  
  4. int main(int argc,char *argv[])  
  5. {  
  6.     QApplication a(argc,argv);  
  7.     helloQT hello;  
  8.     hello.show();  
  9.     return a.exec();  

         2)  新建文件helloQT.h。输入程序:

 
  
  1. #ifndef HELLOQT_H  
  2. #define HELLOQT_H  
  3. #include <QWidget> 
  4.  
  5. namespace Ui{  
  6.     class Form;  
  7. }  
  8.  
  9. class helloQT:public QWidget  
  10. {  
  11.     Q_OBJECT  
  12.  
  13. public:  
  14.     helloQT(QWidget *parent=0);  
  15.     ~helloQT();  
  16.  
  17. private:  
  18.     Ui::Form *ui;  
  19.  
  20. public slots:  
  21.     void on_pushButton_clicked();  
  22.  
  23. };  // 不能少分号,否则出错  
  24.  
  25. #endif 


         3)  新建文件helloQT.cpp。输入程序:

 
  
  1. #include "helloQT.h"  
  2. #include "ui_helloQT.h"  
  3.  
  4. helloQT::helloQT(QWidget *parent):  
  5.       QWidget(parent),  
  6.       ui(new Ui::Form)  
  7. {  
  8.       ui->setupUi(this);  
  9. }  
  10.  
  11. helloQT::~helloQT()  
  12. {  
  13.       delete ui;  
  14. }  
  15.  
  16. void helloQT::on_pushButton_clicked()  
  17. {  
  18.       ui->label->setText("helloQT");  

     8、生成工程文件,编译并运行,如下:

       至此完成!

 

注:在第7步(2)中,在helloQT.h输入程序时,类的定义最后一句不能少了分号:否则make时会出错如下:“错误:expected initializer before QtCoreModule ”,出错图为: