首页 文章

简单的Qt程序构建但不显示输出

提问于
浏览
1

我刚开始学习Qt并尝试编译并运行一个简单的hello world程序 . 该程序构建没有任何问题,并在 compiler output 中提供此输出

Starting: /qtbuild/bin/qmake /home/ved/Qt/train1/train1.pro -spec /qtbuild/mkspecs/qws/linux-arm-g++ -r CONFIG+=debug 
Exited with code 0.
Starting: /usr/bin/make -w 
make: Entering directory `/home/ved/Qt/train1'
make: Nothing to be done for `first'.
make: Leaving directory `/home/ved/Qt/train1'
Exited with code 0.

但是在尝试运行程序时,它只显示以下内容:

Starting /home/ved/Qt/train1/train1...
/home/ved/Qt/train1/train1 exited with code 255

我的代码:

#include 
#include 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QLabel *label = new QLabel("Hello World!!!");
    label->show();
    return a.exec();
}

我对Qt构建过程完全不熟悉并且无法理解什么是错误的 .

更新

尝试将 QCoreApplication 更改为 QApplication . 没变 .

Running build steps for project train1...
Starting: /qtbuild//bin/qmake /home/ved/Qt/train1/train1.pro -spec /qtbuild/mkspecs/qws/linux-arm-g++ -r CONFIG+=debug 
Exited with code 0.
Starting: /usr/bin/make -w 
make: Entering directory `/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
In file included from /qtbuild/include/QtCore/qobject.h:48,
from /qtbuild/include/QtCore/qiodevice.h:46,
from /qtbuild/include/QtCore/qxmlstream.h:45,
from /qtbuild/include/QtCore/QtCore:3,
from main.cpp:1:
/qtbuild/include/QtCore/qstring.h:91: note: the mangling of 'va_list' has changed in 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: Leaving directory `/home/ved/Qt/train1'
Exited with code 0.

我使用Qt 4.6.3 .

7 回答

  • 0

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

  • 1

    你应该告诉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();
    }
    
  • 2

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

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

    QCoreApplication 更改为 QApplication 添加主窗口

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

    您必须在项目配置中设置要编译Qt GUI 应用程序 . 使用QApplication而不是QCoreApplication是不够的 . 我没有't know your IDE, so i can'提供"howto" - 但我相信你会很容易找到必要的选择 . 对于eapmle,在MSVC中,您可以在创建项目期间设置必要的应用程序类型(控制台或GUI) .

    此外 - 退出代码255显示一些错误 . 手动更改时,退出代码必须为零,例外情况 .

  • 0

    尝试在Project / Build属性中取消单击Shadow build .

  • 0

    我有同样的问题 . 让它重启QT . 当然有效

相关问题