试图运行“Hello,world!”的多个错误桌面应用程序QT

时间:2018-01-22 20:53:20

标签: c++ qt

我正在努力开始使用QT。具体来说,“你好,世界!”本基础教程中的桌面应用程序:Getting Started on the Commandline

.cpp文件名为helloX.cpp,其中包含以下内容:

#include <QtGui>

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

这是我运行make时遇到的大量错误,我无法在任何地方找到任何对这些错误的引用!任何帮助故障排除将不胜感激。

/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

非常感谢

3 个答案:

答案 0 :(得分:1)

您需要确保为Qt应用程序提供了正确的包含文件,并且您的构建环境可以查看并链接到所有Qt库。这些错误是因为您的编译器无法找到您正在使用的Qt API。

答案 1 :(得分:1)

  1. qmake应该在你的道路上。在终端中执行export PATH=<path to qmake exe>;$PATH

  2. 您需要像教程中所述正确设置.pro文件。

    2a上。使用INCLUDE+=为qt标题添加正确的包含路径

    2B。使用Qt+=添加您要使用的库。对于您的简单示例, gui 小部件应该没问题。

  3. 转到您的项目根目录并运行 qmake make ,如您所引用的教程中所述。

  4. 不要忘记为您在应用中使用的每个小部件添加标头,如QApplication,QLabel等。

答案 2 :(得分:1)

非常感谢您的反馈。事实证明我需要的是以下内容:

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

这解决了所有问题。

最佳,

相关问题