Code examples from book “C++ GUI Programming with Qt4” are not recognized by Qt Creator
Hi,
I’m currently reading the latest publication of the C++ GUI Programming with QT4 book(second edition), but for some reason I cannot run the code shown in the book. Originally I thought it was because I was using the old publication (2006) book and I thought the code was some how out dated but I just got the newest release and the code is the same as the one shown in the previous version.
When I paste this code in Qt Creator the includes are not recognized (they are underlined).
- #include <QApplication>
- #include <QLabel>
- int main(int argc, char *argv[])
- {
- label->show();
- return app.exec();
- }
I also tried what the book suggests, basically compile the code using qmake.
This is what I have tried.
1-I created a folder called “hello” and added a file inside also called hello.ccp
2-Opened terminal and changed the directory to the folder called “hello”
3-Typed qmake -project
But after I type qmake I get a message that says “-bash: qmake: command not found”.
I guess this is a two questions thread.
1- Why is the code above not recognized by Qt Creator?
2- What do I need to use qmake if I would wanted to?
Thanks in advance.
12 replies
Which OS are you using? Looks like some unix or OS X from the bash.
qmake of the SDK is usually not in the search path of the command line shells, this is why it is not found. The book was written well before the SDK was out, so it cannot respect that special settings. You can make it work by calling qmake with the complete path. For example, on my Mac it is something like this:
The path on Linux will differ slightly. If I remember correctly it’s either somewhere in your home dir or in /opt.
After calling qmake this way, you get your .pro file and Creator can open the project.
Ahm, no, that was created by the SDK. It’s from September last year, though. Maybe the directory layout has changed since then.
You may want to open Terminal.app, got to the SDK folder in /Applications and run find:
- cd /Applications/QtSDK
- find . -name qmake
This prints all occurences of a file called qmake relative to the SDK directory. Example output from my box is:
- ./Examples/4.7/qmake
- ./Madde/madbin/qmake
- ./Madde/targets/harmattan-nokia-meego-api/bin/qmake
- ./Madde/tools/qt-tools_snow-leopard_2011-07-27_UnivBin_qt4.7.4-m0.22.11/bin/qmake
Ignore all the Madde, Simulator and Examples entries.
Opps, it looks like my post was added before yours. As I said I found it and works.
One more question though.
Does this mean that I wont be able to create a projects in Qt Creator paste code from the book and run it (something like the above code)? If this is true, can someone explain me why it does work with qmake but not in Qt creator?
Found it, awesome, it works./Applications//QtSDK/Desktop/Qt/4.8.0/gcc/bin/qmake -project
One more question though.
Does this mean that I wont be able to create a project in Qt Creator and run code from the book (something like the above code)? If this is true, can someone explain me why it does work with qmake but not in Qt creator?Thanks a lot for your help!
Ah, our answers overlapped – good to know it works now.
Qt Creator does not support running “qmake -project”. You can create new Qt projects in Creator too, though. Usually you run one of the wizards
- Hit Cmd-N for a new something…
- choose Qt Widget Project on the left
- choose Qt GUI application on the right
This will create a project and little, empty ui.
If you want to start from scratch, choose “Other Project” in step #2 and “Empty Qt project” in #3. You will have to add the Qt configuration and the source files you create afterwards to that file.
I usually prefer to create a Qt GUI project.
The way you described above is actually the way I have been using but I wanted to create everything myself and follow the book.
But what I dont know understand is why the following includes were not recognized in Qt Creator until I input the path in the terminal, they are now working but they were not before.
- #include <QApplication>
- #include <QLabel>
Thanks
You must log in to post a reply. Not a member yet? Register here!



