這三個軟件安裝好后,在VS的菜單界面上就會出現"QT"選項了。
(1) 選擇“可執行文件”,在里面添加:C:\Qt4.7.0\bin;
(2) 選擇“包含文件”,在里面添加:C:\Qt4.7.0\include; C:\Qt4.7.0\include\Qt;
C:\Qt4.7.0\include\QtCore; C:\Qt4.7.0\include\QtGui
(3) 選擇“庫文件”,在里面添加:C:\Qt4.7.0\lib;
寫為:helloQT。然后保存為Hello.ui,再將它添加到源文件中。
命令行:uic.exe Hello.ui -o HelloUi.h
輸出:HelloUi.h
附加依賴項:uic.exe; Hello.ui
然后,點擊“確定”。這時再右擊“Hello.ui”,選擇“編譯”,則會生成HelloUi.h。
- #include "stdafx.h"
- #include "HelloUi.h"
- #include <QtGui/QApplication>
- #include <QtGui/QMainWindow>
- int _tmain(int argc, _TCHAR* argv[])
- {
- QApplication app(argc,argv);
- QMainWindow *dlg=new QMainWindow();
- Ui::Form ui;
- ui.setupUi(dlg);
- dlg->show();
- return app.exec();
- }
(1) “常規”:選擇“字符集”為“使用多字節字符集”;
(2) “調試”:填寫“環境”值為:PATH=C:\Qt4.7.0\bin;
填寫“合並環境”值為:是;
(3) “鏈接器”:進入“常規”選項,填寫“附加庫目錄”為:C:\Qt4.7.0\lib;
(注意,這兩個庫之間為空格,不能寫逗號)
四、 信號與槽
信號與槽的使用,需要用到moc(即meta object compiler)。
這是因為:當要在GUI中用到信號與槽,就需在.h文件中的類里寫入Q_OBJECT宏。而任何含有Q_Object的類都必須使用Qt的moc工具生成對應的cpp文件,然后在項目里面包含這個cpp,編譯才能成功,否則會出錯鏈接錯誤,如下。
總結:
1) Qt中的元對象系統是用來處理對象間通訊的信號/槽機制、運行時的類型信息和動態屬性系統。
2) moc讀取C++源文件(應該是.h頭文件吧)。如果它發現其中包含一個或多個類的聲明中含有Q_OBJECT宏,它就會給含有Q_OBJECT宏的類生成另一個含有元對象代碼的C++源文件。這個生成的源文件可以被類的源文件包含(#include)到或者和這個類的實現一起編譯和連接。
下面開始編程:
A 編寫 main.cpp
- #include "stdafx.h"
- #include <QtGui/QApplication>
- #include "hello.h"
- int _tmain(int argc, _TCHAR* argv[])
- {
- QApplication app(argc,argv);
- Widget w;
- w.show();
- return app.exec();
- }
B 編寫 hello.h
- #ifndef WIDGET_H
- #define WIDGET_H
- #include <QWidget>
- #include <stdio.h>
- namespace Ui {
- //class Widget; //1 把Widget換成Form (共3處)
- class Form;
- }
- class Widget : public QWidget {
- Q_OBJECT
- public:
- Widget(QWidget *parent = 0);
- ~Widget();
- protected:
- void changeEvent(QEvent *e);
- private:
- //Ui::Widget *ui; //2 把Widget換成Form (共3處)
- Ui::Form *ui;
- public slots:
- void on_pushButton_clicked(void);
- };
- #endif // WIDGET_H
C 編寫 hello.cpp
- #include "stdafx.h"
- #include "hello.h"
- #include "ui_hello.h"
- //#include "moc_hello.cpp" //不是頭文件,不可以加此句
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- //ui(new Ui::Widget) //3 把Widget換成Form (共3處)
- ui(new Ui::Form)
- {
- ui->setupUi(this);
- }
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::changeEvent(QEvent *e)
- {
- QWidget::changeEvent(e);
- switch (e->type()) {
- case QEvent::LanguageChange:
- ui->retranslateUi(this);
- break;
- default:
- break;
- }
- }
- void Widget::on_pushButton_clicked(void)
- {
- ui->label->setText("liangbing8612.blog.51cto.com");
- printf("liangbing8612.blog.51cto.com");
- }
D 制作.ui文件,並生成ui_hello.h
打開designer,拖入一個pushButton,一個label。保存為hello.ui。然后生成ui_hello.h,並添加到項目的源文件中。
編譯運行,則出現如下錯誤:
錯誤 1 error LNK2001: 無法解析的外部符號 "public: virtual struct QMetaObject const * __thiscall Widget::metaObject(void)const " (?metaObject@Widget@@UBEPBUQMetaObject@@XZ)
錯誤 2 error LNK2001: 無法解析的外部符號 "public: virtual void * __thiscall Widget::qt_metacast(char const *)" (?qt_metacast@Widget@@UAEPAXPBD@Z)
錯誤 3 error LNK2001: 無法解析的外部符號 "public: virtual int __thiscall Widget::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Widget@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
錯誤 4 fatal error LNK1120: 3 個無法解析的外部命令
這是因為在源文件中沒有添加上moc_hello.cpp文件。
解決方法:右擊hello.h,選擇“自定義生成步驟”,“常規”
命令行:moc.exe hello.h -o moc_hello.cpp
輸出:moc_hello.cpp
附加依賴項:moc.exe hello.h
確定,然后,右擊hello.h,選擇 “編譯”,則在文件夾中生成moc_hello.cpp,再將其添加到源文件中。
然后,運行程序,出現錯誤:
錯誤 fatal error C1010: 在查找預編譯頭時遇到意外的文件結尾。是否忘記了向源中添加“#include "stdafx.h"”?
則在moc_hello.cpp文件的開頭添加上:#include "stdafx.h"。
然后,再運行。仍然出現上面錯誤。這是因為當運行程序,又重新生成了moc_hello.cpp文件(這個新的文件的開頭顯然是沒有#include "stdafx.h"),覆蓋了已經修改過的文件。
解決方法:右擊hello.h,選擇“自定義生成步驟”,“常規”
清空“命令行” “輸出” “附加依賴項” 里對應的內容。這樣在運行程序時就不會再生成新的moc_hello.cpp文件了。然后確定。
這樣再次運行程序,可以成功運行。運行結果如圖。
trackback: http://liangbing8612.blog.51cto.com/2633208/471849
http://liangbing8612.blog.51cto.com/2633208/596269