繁体   English   中英

编译并运行Qt程序

[英]Compiling and running a Qt program

我是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();
}

当我这样做时: Qt command prompt C:\\Qt\\2010.05\\qt>gcc label.cc ,我得到以下信息:

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程序的方法正确吗?

谢谢。

Qmake可以生成一些默认的项目文件,如下所示:

qmake -project
qmake
make

第一行生成项目文件,第二行从项目文件生成makefile并进行构建项目。

要使用Qt系统进行构建,您需要使用元对象编译器moc。 可能是用户界面编译器,uic并定义了包含文件的路径并链接到Qt库。

通常的方法是使用Qt提供的qmake 您必须为qmake编写一个项目文件。 这比编写命令行或makefile容易很多倍。

您缺少包含Qt标头的包含路径。

-Ipath_to_qt/include

在您的项目中为专业添加核心,就像这样:

QT + =核心

QT += widgets添加到您的pro文件中,并将#include <QtWidgets>到您的main.cpp中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM