April 6, 2012

fs_tigre fs_tigre
Ant Farmer
212 posts

Code examples from book “C++ GUI Programming with Qt4” are not recognized by Qt Creator

Page  
1

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).

  1. #include <QApplication>
  2. #include <QLabel>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.      QApplication app(argc, argv);
  7.      QLabel *label = new QLabel("Hello Qt!");
  8.      label->show();
  9.      return app.exec();
  10. }

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.

17 replies

April 6, 2012

koahnig koahnig
Area 51 Engineer
2881 posts

Does Qt creator work without problems otherwise?

It looks like the setup of Qt and Qt creator is somehow broken, since qmake is not found.

How did you install Qt create and Qt? Is it the latest version?

April 6, 2012

fs_tigre fs_tigre
Ant Farmer
212 posts

Qt Creator works perfect.

I basically installed the Qt SDK version 1.2, which I’m assuming it includes everything, right?

The about QT Creator window reads as follow
Qt Creator 2.4.1
Based on Qt 4.7.4 (64 bit)

April 6, 2012

koahnig koahnig
Area 51 Engineer
2881 posts

Are the examples are coming with .pro file?
In principle you should be able to import those files into qt creator and all should work fine.

April 6, 2012

Volker Volker
Ant Farmer
5428 posts

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:

  1. /Developer/QtSDK/Desktop/Qt/474/gcc/bin/qmake -project

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.

April 6, 2012

fs_tigre fs_tigre
Ant Farmer
212 posts

I’m on a Mac.

Sorry but how do I find the path?

I looked in the Developer folder and I don’t see the QtSDK folder shown above.

Thank you all for your help!

April 6, 2012

fs_tigre fs_tigre
Ant Farmer
212 posts

Found it, it was in my Applications folder, the only folder that I couldn’t found based on your path description is the /bin folder.

Is this a folder you created?

Again thanks a lot for your help.

April 6, 2012

fs_tigre fs_tigre
Ant Farmer
212 posts

Found it, awesome, it works.

/Applications//QtSDK/Desktop/Qt/4.8.0/gcc/bin/qmake -project

Thanks a lot for your help!

April 6, 2012

Volker Volker
Ant Farmer
5428 posts

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:

  1. cd /Applications/QtSDK
  2. find . -name qmake

This prints all occurences of a file called qmake relative to the SDK directory. Example output from my box is:

  1. ./Desktop/Qt/474/gcc/bin/qmake
  2. ./Examples/4.7/qmake
  3. ./Madde/madbin/qmake
  4. ./Madde/targets/harmattan-nokia-meego-api/bin/qmake
  5. ./Madde/tools/qt-tools_snow-leopard_2011-07-27_UnivBin_qt4.7.4-m0.22.11/bin/qmake
  6. ./Simulator/Qt/gcc/bin/qmake

Ignore all the Madde, Simulator and Examples entries.

April 6, 2012

fs_tigre fs_tigre
Ant Farmer
212 posts

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?

April 6, 2012

Volker Volker
Ant Farmer
5428 posts

fs_tigre wrote:
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

  1. Hit Cmd-N for a new something…
  2. choose Qt Widget Project on the left
  3. 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.

April 6, 2012

fs_tigre fs_tigre
Ant Farmer
212 posts

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.

  1. #include <QApplication>
  2. #include <QLabel>

Thanks

April 10, 2012

Volker Volker
Ant Farmer
5428 posts

The book was written without knowledge of Qt Creator. It describes the steps for doing the stuff entirely on the command line.

Maybe you are better off using one of the Qt Creator tutorials?

January 7, 2014

ayushj ayushj
Lab Rat
3 posts

i want to display more than one labels on same window , how could i do that ? i am trying somethong like this
but its opening two different windows ?
Please can any one explain me

  1.  #include <QtGui/QApplication>
  2. #include "mainwindow.h"
  3. #include <QLabel>
  4. #include <stdlib.h>
  5.  
  6.  int main(int argc, char *argv[])
  7.  {
  8.  QApplication app(argc, argv);
  9.  QLabel *label = new QLabel("Hello Qt!");
  10.  label->show();
  11.  QLabel *label1 = new QLabel("qwerty!");
  12.  label1->show();
  13.  return app.exec();
  14.  }

January 7, 2014

Zlatomir Zlatomir
Hobby Entomologist
346 posts

ayushj it’s recommended to create a new topic for each new question, and your question is not related to the original question.

Another recommendation related to your code, when you experiment with somethings do it in a small project separated from anything else, that way you can understand it easier.
Anyway, you can use layouts from code too, so to have two QLabels in an window you need a widget that will be the parent, an layout and those two labels:

  1.  #include <QApplication>
  2.  //#include "mainwindow.h"
  3.  #include <QLabel>
  4.  #include <QWidget>
  5.  #include <QVBoxLayout>
  6. int main(int argc, char *argv[])
  7.  {
  8.  QApplication app(argc, argv);
  9.  QWidget widget;
  10.  
  11.  //create the layout with the widget as parent
  12.  //(we use address of operator to get the address of widget)
  13.  QVBoxLayout* mainLayout = new QVBoxLayout(&widget);
  14.  QLabel *label = new QLabel("Hello Qt!");
  15.  
  16.  //label->show(); don't show them individually
  17.  
  18.  QLabel *label1 = new QLabel("qwerty!");
  19.  
  20.  //add the widgets to the layout:
  21.  mainLayout->addWidget(label);
  22.  mainLayout->addWidget(label1);
  23.  
  24.  //show the widget and that will show the children too
  25.  widget.show();
  26.  return app.exec();
  27.  }

January 7, 2014

ayushj ayushj
Lab Rat
3 posts

i am trying to display labels (two labels one is “Hello qt” & “qwerty”) on qt window (which is created qtwidget window ) one by one but it is showing three different windows with different labels first one is simple window which have nothing to display 2nd is label as “Hello Qt!” and 3rd as label “QWERTY

i want to show this label on mainwindow one by one……………?

  1. #include <QLabel>
  2. #include <stdlib.h>
  3.  
  4.  int main(int argc, char *argv[])
  5.  {
  6.  QApplication app(argc, argv);
  7.  QWidget window;
  8.  window.resize(320, 240);
  9.  QLabel *label = new QLabel("Hello Qt!");
  10.  label->show();
  11.  window.show();
  12.  
  13.  sleep(5);
  14.  QLabel *label1 = new QLabel("!QWERTY!");
  15.  label1->show();
  16.  return app.exec();
  17. }

Page  
1

  ‹‹ qt assistant translation files      tab order problem in widget ››

You must log in to post a reply. Not a member yet? Register here!