2011-03-28 95 views
1

我是新來Qt,並試圖編譯和運行這個程序Qt的我從Programming with Qt書類型:編譯和運行一個Qt程序

#include <qapplication.h> 
#include <qlabel.h> 
int main(int argc, char *argv[]) 
{ 
QApplication myapp(argc, argv); 
QLabel *mylabel = new QLabel("Hello",0); 
mylabel->resize(120,30); 
myapp.setMainWidget(mylabel); 
mylabel->show(); 
return myapp.exec(); 
} 

當我這樣做:C:\Qt\2010.05\qt>gcc label.ccQt command prompt,我得到以下內容:

label.cc:1:26: error: qapplication.h: No such file or directory 
label.cc:2:20: error: qlabel.h: No such file or directory 
label.cc: In function 'int main(int, char**)': 
label.cc:5: error: 'QApplication' was not declared in this scope 
label.cc:5: error: expected ';' before 'myapp' 
label.cc:6: error: 'QLabel' was not declared in this scope 
label.cc:6: error: 'mylabel' was not declared in this scope 
label.cc:6: error: expected type-specifier before 'QLabel' 
label.cc:6: error: expected ';' before 'QLabel' 
label.cc:8: error: 'myapp' was not declared in this scope 

這是爲什麼?我編寫Qt程序的方式是否正確?

謝謝。

+1

@ user588855你爲什麼不使用[Qt Creator](http://qt.nokia.com/products/developer-tools/)?它會讓你的生活更輕鬆。 – sashoalm 2011-03-28 07:40:32

+0

gcc是c編譯器。你想要g ++。 1)但使用qmake是首選的方式。 2)使用像 2011-03-28 09:09:10

+0

@Ronny這樣的頭文件它取決於你使用的是什麼版本的Qt。 v3 = qapplication.h,v4 = QApplication – koan 2011-03-28 12:33:29

回答

7

QMAKE可以產生一些默認的項目文件是這樣的:

qmake -project 
qmake 
make 

第一行產生的項目文件,從項目文件第二生成makefile文件,並生成項目。

+1

我完全同意......首先運行qmake -project,它將生成必要的文件......(然後查看包含所有必要內容的.pro文件)。然後簡單地運行qmake並建立你的程序。或者你開始使用QtCreator ...這是一個非常好的工具,用於編寫和管理項目,並允許調試和更多...... – evident 2011-03-28 08:56:07

+0

'-project'在這裏是我的'label.cc'嗎? – Simplicity 2011-03-28 09:54:48

+0

@ user588855。你只需要在你的源文件的同一目錄中鍵入(提供你的qt環境已正確安裝)'qmake -project',那麼qmake將掃描目錄。然後,您應該在該目錄中看到一個默認的.pro文件,其中包含'SOURCES + = label.cc'這一行。 – deo 2011-03-28 10:25:50

2

要使用Qt系統構建,您需要使用元對象編譯器moc;也許是用戶界面編譯器,uic和定義包含文件的路徑以及鏈接到Qt庫。

通常的做法是使用由Qt提供的qmake。您必須爲qmake編寫一個項目文件。這比編寫命令行或生成文件要容易許多倍。

+0

我該怎麼做?任何解釋這個的資源? – Simplicity 2011-03-28 08:41:14

+0

如果您的書中沒有提到qmake系統,我感到非常震驚。如果您在我的答案中關注qmake鏈接,那麼包含教程的文檔很多。 – koan 2011-03-28 08:48:03

2

你錯過了包含Qt頭文件的路徑。

-Ipath_to_qt/include 
+0

這是否意味着我應該將'label.cc'放在特定位置? – Simplicity 2011-03-28 08:41:51

+0

不需要它不需要 – 2011-03-28 09:08:04

+0

@user:no。這意味着你需要告訴編譯器在哪裏找到你在Qt中使用的頭文件,並且你需要告訴鏈接器在哪裏可以找到Qt庫以及使用哪些庫。否則你的應用程序將無法構建。正在使用任何正在使用的庫。 – murrekatt 2011-03-28 09:54:13

0

添加核心親外商投資企業在project.Like這樣的:

QT + =核心

0

添加 QT += widgets您親文件, #include <QtWidgets> 您的main.cpp