Simplicity
Simplicity

Reputation: 48916

Compiling and running a Qt program

I'm new to Qt, and trying to compile and run this Qt program I typed from the Programming with Qt book:

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

When I do this: C:\Qt\2010.05\qt>gcc label.cc on the Qt command prompt, I get the following:

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

Why is that? Is it correct the way I did for compiling a Qt program?

Thanks.

Upvotes: 1

Views: 4716

Answers (5)

Francuz
Francuz

Reputation: 461

Add QT += widgets to your pro file, and #include <QtWidgets> to your main.cpp

Upvotes: 0

Sandy
Sandy

Reputation: 143

Add core to pro fie in your project.Like this:

QT += core

Upvotes: 0

deo
deo

Reputation: 999

Qmake can generate some default project file like this:

qmake -project
qmake
make

First line generates project file, second generates makefile from the project file and make builds the project.

Upvotes: 7

murrekatt
murrekatt

Reputation: 6129

You're missing the include path to where you have the Qt headers.

-Ipath_to_qt/include

Upvotes: 2

koan
koan

Reputation: 3686

To build with the Qt system you need to use the meta object compiler, moc; maybe the user interface compiler, uic and define paths to the include files and link to the Qt libraries.

The usual way to do this is using qmake as provided by Qt. You must write a project file for qmake. This is many times easier than writing a command line or makefile.

Upvotes: 2

Related Questions