1

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.

3
  • 1
    @user588855 Why don't you use Qt Creator? It will make your life much easier. Commented Mar 28, 2011 at 7:40
  • gcc is the c compiler. you want g++. 1) But using qmake is the prefered way. 2) use headers like <QApplication> and <QLabel> Commented Mar 28, 2011 at 9:09
  • @Ronny It depends on what version of Qt you are using. v3 = qapplication.h, v4 = QApplication Commented Mar 28, 2011 at 12:33

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I can totally agree... first run qmake -project which will generate you the necessary files... (then have a look at the .pro file which contains all the necessary stuff). Then simply run qmake and make to build your program. Or you start using the QtCreator... it's a pretty good tool for writing and managing projects and allows debugging and much more....
Is -project here my label.cc?
@user588855. You just need to type (provided your qt environment is properly installed) 'qmake -project' in same directory with your source file(s) no file names necessary, qmake will scan the directory. You should then see a default .pro file in that directory that contains the line 'SOURCES += label.cc'.
2

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.

2 Comments

How can I do that? Any resources that explain this?
I am utterly shocked if the qmake system is not even mentioned in your book. There is a lot of documentation including a tutorial if you follow the qmake link in my answer.
2

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

-Ipath_to_qt/include

2 Comments

Does that mean that I should put label.cc in a specific location?
@user: no. it means you need to tell the compiler where to find the headers you use from Qt, and you'll need to tell the linker where to find the Qt libraries and which ones to use. Otherwise your application will not build. Normal practice with any library you're using.
0

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

QT += core

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.