简体   繁体   中英

multiple errors trying to run “Hello, world!” desktop application QT

I am trying to get started with QT. Specifically, the "Hello, world!" desktop application in this basic tutorial: Getting Started on the Commandline

The .cpp file is called helloX.cpp with the following contents:

#include <QtGui>

int main(int argc, char **argv) {
 QApplication app(argc, argv);
 QLabel label("Hello, world!");
 label.show();
 return app.exec();
}

here are a multitude of errors that I get when I run make and I can't find any reference to these errors anywhere! any help troubleshooting would be greatly appreciated.

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -stdlib=libc++ -O2 -std=gnu++11 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -mmacosx-version-min=10.9 -Wall -W -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -I/Users/hassanshallal/anaconda/include/qt -I/Users/hassanshallal/anaconda/include/qt/QtGui -I/Users/hassanshallal/anaconda/include/qt/QtCore -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AGL.framework/Headers -I/Users/hassanshallal/anaconda/mkspecs/macx-clang -o helloX.o helloX.cpp
helloX.cpp:4:15: error: variable has incomplete type 'QApplication'
 QApplication app(argc, argv);
              ^
/Users/hassanshallal/anaconda/include/qt/QtCore/qobject.h:452:18: note: forward declaration of
      'QApplication'
    friend class QApplication;
                 ^
helloX.cpp:5:2: error: use of undeclared identifier 'QLabel'; did you mean 'QAccessible::Label'?
 QLabel label("Hello, world!");
 ^~~~~~
 QAccessible::Label
/Users/hassanshallal/anaconda/include/qt/QtGui/qaccessible.h:360:9: note: 'QAccessible::Label' declared
      here
        Label         = 0x00000001,
        ^
helloX.cpp:5:8: error: expected ';' after expression
 QLabel label("Hello, world!");
       ^
       ;
helloX.cpp:5:9: error: use of undeclared identifier 'label'
 QLabel label("Hello, world!");
        ^
helloX.cpp:6:2: error: use of undeclared identifier 'label'
 label.show();
 ^
helloX.cpp:5:2: warning: expression result unused [-Wunused-value]
 QLabel label("Hello, world!");
 ^~~~~~
1 warning and 5 errors generated.
make: *** [helloX.o] Error 1

Thanks a lot

You need to make sure that you have the correct include files for your Qt application and that your build environment can see and link to all of the Qt libraries. These errors are because your compiler cannot find the Qt APIs you're using.

  1. qmake should be in your path. In the terminal do export PATH=<path to qmake exe>;$PATH

  2. You need to correctly set the .pro file like stated in the tutorial.

    2a. With INCLUDE+= add the correct include path for qt headers

    2b. with Qt+= add the libraries you're gonna use. For your simple example gui and widget should be fine.

  3. Go to your project root and run qmake and make like stated in the tutorial you are referencing.

  4. Don't forget to include the headers for every widget you use in your app like QApplication, QLabel, etc.

thanks a lot for the feedback. It turned out all I need is the following:

1) add #include <QApplication> to the .cpp file
2) add #include <QLabel> to the .cpp file
3) add: QT += widgets to the .pro file

And this took care of all the issues.

Best,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM