【问题标题】:Simple Qt program builds but doesn't show output简单的 Qt 程序构建但不显示输出
【发布时间】:2013-06-24 18:27:13
【问题描述】:

刚开始学习Qt,尝试编译运行一个简单的hello world程序。该程序构建没有任何问题,并在 compiler output

中提供此输出 开始:/qtbuild/bin/qmake /home/ved/Qt/train1/train1.pro -spec /qtbuild/mkspecs/qws/linux-arm-g++ -r CONFIG+=debug 以代码 0 退出。 开始:/usr/bin/make -w make: 进入目录`/home/ved/Qt/train1' make: `first' 什么都不做。 make: 离开目录`/home/ved/Qt/train1' 以代码 0 退出。

但在尝试运行程序时,它只显示:

开始 /home/ved/Qt/train1/train1... /home/ved/Qt/train1/train1 以代码 255 退出

我的代码:

#包括 #包括 int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QLabel *label = new QLabel("Hello World!!!"); 标签->显示(); 返回 a.exec(); }

我对 Qt 构建过程完全陌生,不明白哪里出了问题。

更新

尝试将QCoreApplication 更改为QApplication。没有变化。

为项目 train1 运行构建步骤... 开始:/qtbuild//bin/qmake /home/ved/Qt/train1/train1.pro -spec /qtbuild/mkspecs/qws/linux-arm-g++ -r CONFIG+=debug 以代码 0 退出。 开始:/usr/bin/make -w make: 进入目录`/home/ved/Qt/train1' arm-linux-g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED -I/qtbuild/mkspecs/qws/linux-arm-g++ -I。 -I/qtbuild/include/QtCore -I/qtbuild/include/QtNetwork -I/qtbuild/include/QtGui -I/qtbuild/include -I. -I/usr/local/tslib-arm/include -o main.o main.cpp 在 /qtbuild/include/QtCore/qobject.h:48 包含的文件中, 来自/qtbuild/include/QtCore/qiodevice.h:46, 来自/qtbuild/include/QtCore/qxmlstream.h:45, 来自/qtbuild/include/QtCore/QtCore:3, 来自 main.cpp:1: /qtbuild/include/QtCore/qstring.h:91:注意:'va_list' 的修改在 GCC 4.4 中发生了变化 arm-linux-g++ -Wl,-rpath,/qtbuild/lib -o train1 main.o -L/usr/local/tslib-arm/lib -L/qtbuild//lib -lQtGui -L/qtbuild//lib - l/usr/local/tslib-arm/lib -lQtNetwork -lQtCore -lpthread make: 离开目录`/home/ved/Qt/train1' 以代码 0 退出。

我使用 Qt 4.6.3。

【问题讨论】:

  • 如果将 QCoreApplication 更改为 QApplication 会发生什么?

标签: qt


【解决方案1】:

如果要显示 QLabel,则需要运行 GUI 应用程序类 QApplication,而不是 QCoreApplication

【讨论】:

    【解决方案2】:

    你应该告诉 Qt,你想用 GUI 构建项目。打开您的项目 .pro 文件并更改行

    QT += ...
    

    QT += core gui
    

    示例,.pro 文件:

    QT       += core gui
    
    TARGET = untitled1
    TEMPLATE = app
    SOURCES += main.cpp
    

    main.cpp:

    #include <QtGui/QApplication>
    #include <QLabel>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QLabel lbl("hello world");
        lbl.show();
        return a.exec();
    }
    

    【讨论】:

      【解决方案3】:

      如果要显示标签,则需要创建一个窗口。基本上是这样的(未测试):

      QMainWindow* win = new QMainWindow();
      QLabel *label = new QLabel(win, "Hello World!!!");
      label->show();
      win->show();
      

      【讨论】:

      • 不正确。您可以将任何小部件显示为顶级窗口。
      • 我很确定您可以在没有 MainWindow 的情况下显示 QLabel。毕竟主窗口不过是一个小部件。问题似乎是他使用的是 QCoreApplication 而不是 QApplication。
      【解决方案4】:

      QCoreApplication 更改为QApplication 并添加一个主窗口

      QApplication a(argc, argv);
      QMainWindow* mainWin = new QMainWindow();
      QLabel *label = new QLabel(mainWin, "Hello World!!!");
      mainWin->setCentralWidget(label);
      mainWin->show();
      

      【讨论】:

        【解决方案5】:

        您必须在项目配置中设置您正在编译 Qt GUI 应用程序。使用 QApplication 代替 QCoreApplication 是不够的。我不知道你的 IDE,所以我不能提供“howto”——但我相信你会很容易找到必要的选项。对于 eapmle,在 MSVC 中,您可以在创建项目期间设置必要的应用程序类型(控制台或 GUI)。

        另外 - 退出代码 255 显示一些错误。当您手动更改退出代码时,退出代码必须为零,但情况除外。

        【讨论】:

          【解决方案6】:

          尝试在您的 Project/Build 属性中取消单击 Shadow build。

          【讨论】:

            【解决方案7】:

            我也有同样的问题。让它重新启动QT。肯定行得通

            【讨论】:

              猜你喜欢
              • 2018-06-05
              • 2016-03-17
              • 2020-01-16
              • 1970-01-01
              • 2021-10-23
              • 1970-01-01
              • 2014-11-12
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多