Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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[])
    {
    QApplication app(argc, argv);
    QLabel *label = new QLabel("Hello Qt!");
    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.



  • 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?



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



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



  • 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:

    @
    /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.



  • 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!



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



  • Found it, awesome, it works.

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

    Thanks a lot for your help!



  • 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:

    @
    ./Desktop/Qt/474/gcc/bin/qmake
    ./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
    ./Simulator/Qt/gcc/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?



  • [quote author="fs_tigre" date="1333716969"]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![/quote]

    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



  • 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?



  • 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

    @
    #include <QtGui/QApplication>
    #include "mainwindow.h"
    #include <QLabel>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QLabel *label = new QLabel("Hello Qt!");
    label->show();
    QLabel *label1 = new QLabel("qwerty!");
    label1->show();
    return app.exec();
    }
    @



  • 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:
    @
    #include <QApplication>
    //#include "mainwindow.h"
    #include <QLabel>
    #include <QWidget>
    #include <QVBoxLayout>
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QWidget widget;

    //create the layout with the widget as parent
    //(we use address of operator to get the address of widget)
    QVBoxLayout* mainLayout = new QVBoxLayout(&widget);
    QLabel *label = new QLabel("Hello Qt!");

    //label->show(); don't show them individually

    QLabel *label1 = new QLabel("qwerty!");

    //add the widgets to the layout:
    mainLayout->addWidget(label);
    mainLayout->addWidget(label1);

    //show the widget and that will show the children too
    widget.show();
    return app.exec();
    }
    @



  • 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...............?
    @#include <QLabel>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QWidget window;
    window.resize(320, 240);
    QLabel *label = new QLabel("Hello Qt!");
    label->show();
    window.show();

    sleep(5);
    QLabel *label1 = new QLabel("!QWERTY!");
    label1->show();
    return app.exec();
    }@



  • Zlatomir thanks for your advice regarding the topic & objective,i will remember your recomendatons thanks much.
    But, i think i am unable to explain my actual query to display labels "one by one" not in simultaneous manner.one by one means one label is first display then hide completely then second one come into the existent and it should be continous................is there any solutions related to what i am asking for ? Thanks much in advance


  • Moderators

    [quote author="ayushj" date="1389158197"]But, i think i am unable to explain my actual query to display labels "one by one" not in simultaneous manner.one by one means one label is first display then hide completely then second one come into the existent and it should be continous................is there any solutions related to what i am asking for ?[/quote]You only need one QLabel. Update the text one by one using QLabel::setText().

    Use "QTimer":http://qt-project.org/doc/qt-5/qtimer.html to trigger the updates.


Log in to reply