Important: Please read the Qt Code of Conduct -

No such file or directory #include <QtWidgets>

  • Hello,
    I am a beginner with qt, and I just download and install the last vesion of qt with this link "":

    Then, I tried some examples to understand how qt works, but i have the save error with each examle I tried. This is an example
    @/opt/qt/5.2.1/gcc_64/examples/widgets/tutorials/addressbook/part1/addressbook.cpp:41: erreur : QtWidgets: No such file or directory
    #include <QtWidgets>@

    always the same error : @erreur : QtWidgets: No such file or directory #include <QtWidgets>@

    Is there anything especial to do to work with the examples ?

    what is the problem ?

    Thank a lot for your help.

  • Hi, and welcome to DevNet,
    You should only add QT += widgets in your .pro file and not include QtWidgets

  • greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    use this in your .pro file

  • I tried your 2 solutions, but i still have a problem:
    A lot of functions are not declared.
    This is the example I tested: childwidget (with the modifications you gave me)
    @greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    SOURCES = main.cpp


    target.path = $$[QT_INSTALL_EXAMPLES]/widgets/tutorials/widgets/childwidget
    INSTALLS += target

    @//! [main program]
    //#include <QtWidgets>

    int main(int argc, char *argv[])
    QApplication app(argc, argv);
    QWidget window;
    window.resize(320, 240);
    window.setWindowTitle(QApplication::translate("childwidget", "Child widget"));;

    //! [create, position and show]
    QPushButton *button = new QPushButton(
    QApplication::translate("childwidget", "Press me"), &window);
    button->move(100, 100);
    //! [create, position and show]
    return app.exec();
    //! [main program]

    And this is an extract of the errors (only the 2 first) i have when i compile:

    @/opt/qt/5.2.1/gcc_64/examples/widgets/tutorials/widgets/childwidget/main.cpp:46: erreur : 'QApplication' was not declared in this scope
    QApplication app(argc, argv);
    /opt/qt/5.2.1/gcc_64/examples/widgets/tutorials/widgets/childwidget/main.cpp:46: erreur : expected ';' before 'app'
    QApplication app(argc, argv);

  • you have to include the Qt classes you need, for a simple program like this you can include the whole module but I would not recommend that because it will drastically increase compile time.
    #include <QApplication>
    #include <QWidget>
    #include <QPushButton>
    essentially you have to include all Qt classes like this, if you want to do it right. :)

    if you are lazy or have a super fast computer you can include whole Qt modules at once with
    #include <QtCore>
    #include <QtGui>
    #include <QtWidgets>
    i would only use that if you need many classes from one module.

  • Thank you,
    with your help, I managed to run the example.

    I find it unfortunate that the examples do not work as they are.
    That make the learning more difficult.

  • Usually, the examples work as they are.
    I have opened your examples/widgets/tutorials/addressbook/part1 with the Qt Creator, built it and had no errors.
    How did you work with the example?

  • That is strange, because, as I said, I just install qtcreator with the script, in the /opt directory/

    Then, I open it and tried some examples, and I have the same problem eact time.

  • Do you open the examples from Qt creator directly? (Welcome -> examples)
    they should work out of the box.

    And is there a reason you use and old version of Qt? I mean that is like 2 years old or something. :)

  • Do you open the examples from Qt creator directly? No : I opened with open file or project

    And is there a reason you use and old version of Qt? I used the version found on the site web

    I tried another version comming from the ubuntu deposite(Qt Creator 3.0.1) , but there was another problem because the binary /usr/bin/qmlpuppet was not installed (because not in the ubuntu deposite), and I did not find he way to install it.

  • I have Qt 5.2.1 running on my Ubuntu machine, using the online installer ( in my case), worked like a charm so I don't know what you did. You have so weird bugs, I have no idea why, sorry.

  • I think we should try to find out, what happend to your install.
    If you look at your Creator's About screen, do you see this:
    Qt Creator 3.1.0 (opensource)
    Based on Qt 5.2.1 (GCC 4.6.1, 64 bit)
    Try to compare your Creator settings with "this: ":

  • Thank form your help.
    This is the version installed
    @Qt Creator 3.0.1
    Based on Qt 5.2.1 (GCC 4.8.2, 64 bit)
    Built on Apr 9 2014 at 09:12:59

    And the version from the .run
    @Qt Creator 3.1.0 (opensource)
    Basé sur Qt 5.2.1 (GCC 4.6.1, 64 bit)
    Compilé le Apr 14 2014 à 15:03:42
    Depuis la révision 52592aa879@

    But it is strange because, now, the qtcreator installed from the unbuntu deposites is now working. Maybe the .run install added the missing files ?

  • As the Qt Creator 3.1.0 is the newest version it would be better to use it instead of 3.0.1 - but if you succeed with 3.0.1 you shouldn't have problems.
    Perhaps you may give 5.3.0 a try when it's released in a few days.

Log in to reply