简体   繁体   English

Qt - QWidget:当没有使用GUI时无法创建QWidget

[英]Qt - QWidget: Cannot create a QWidget when no GUI is being used

I'm trying to run a simple Qt program, and when doing so, I get a console window mentioning: QWidget: Cannot create a QWidget when no GUI is being used , and the second line This application has requested the Runtime to terminate..... , and the .exe file thus stops working. 我正在尝试运行一个简单的Qt程序,当这样做时,我得到一个控制台窗口,提到: QWidget: Cannot create a QWidget when no GUI is being used ,第二行This application has requested the Runtime to terminate..... ,因此.exe文件停止工作。

My .pro file looks as follows: 我的.pro文件如下所示:

#-------------------------------------------------
#
# Project created by QtCreator 2011-04-02T07:38:50
#
#-------------------------------------------------

QT       += core

QT       += gui

TARGET = Hello
CONFIG += console
CONFIG += qt
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

Any ideas on that? 有什么想法吗?

Thanks. 谢谢。

The problem is not with this .pro ; 问题不在于.pro ; it is most likely in main.cpp . 它最有可能在main.cpp Qt requires you to create a QApplication before creating any QWidget subclasses (as well as certain other classes, such as QPixmap). Qt要求您在创建任何QWidget子类(以及某些其他类,如QPixmap)之前创建QApplication。 Your main function should begin with the line: 您的main功能应该从以下行开始:

QApplication app(argc, argv);

and will probably end with a line like: 并可能以如下行结束:

return app.exec();

In between these calls, you should create and show your main window. 在这些调用之间,您应该创建并显示主窗口。

I found that you can do it with a Qt Console project, but ofcourse it will not have the functionality of a console program when you are done with my edits. 我发现你可以使用Qt Console项目来完成它,但是当你完成我的编辑后,它将不具备控制台程序的功能。

First of all you need to exchange #include <QtCoreApplication> with #include <QApplication> in your main.cpp (where you start your application) 首先,您需要在main.cpp (启动应用程序的地方)中使用#include <QApplication>交换#include <QtCoreApplication>

In the main(int,char**){ main(int,char**){

exchange QCoreApplication a(argc, argv); 交换QCoreApplication a(argc, argv); with QApplication a(argc, argv); 使用QApplication a(argc, argv);

and in between QApplication and return a.exec you have your widget and other gui related stuff 在QApplication和返回a.exec之间你有你的小部件和其他gui相关的东西

and in the end you use return a.exec();} 最后你使用return a.exec();}

I think I found where the issue is. 我想我找到了问题所在。

Since I'm using Qt Creator , and when creating a new project, I was choosing Qt Console Application instead of Qt Gui Application . 由于我正在使用Qt Creator ,并且在创建新项目时,我选择了Qt Console Application而不是Qt Gui Application

"QWidget: Cannot create a QWidget when no GUI is being used" happens when you application isn't QApplication instance. 当您的应用程序不是QApplication实例时,“QWidget:当没有使用GUI时无法创建QWidget”。 From Qt docs : 来自Qt docs

QApplication specializes QGuiApplication with some functionality needed for QWidget-based applications. QApplication专门为QGuiApplication提供了基于QWidget的应用程序所需的一些功能。 It handles widget specific initialization, finalization, and provides session management. 它处理特定于小部件的初始化,完成并提供会话管理。

For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. 对于使用Qt的任何GUI应用程序,无论应用程序在任何给定时间是否具有0,1,2或更多窗口,都只有一个QApplication对象。 For non-QWidget based Qt applications, use QGuiApplication instead, as it does not depend on the QtWidgets library. 对于基于非QWidget的Qt应用程序,请改用QGuiApplication,因为它不依赖于QtWidgets库。

From the docs, the QApplication class manages the GUI application's control flow and main settings whilst the QCoreApplication class provides an event loop for console Qt applications 从文档中, QApplication类管理GUI应用程序的控制流和主要设置,而QCoreApplication类为控制台Qt应用程序提供事件循环

I had the same problem, the default QT Console App uses the QCoreApplication instead of the QApplication to run the application. 我遇到了同样的问题,默认的QT控制台应用程序使用QCoreApplication而不是QApplication来运行应用程序。

Here is what i did to make it work 这就是我做的工作

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget widget;

    widget.show();

    return a.exec();
}

And i did not change anything in my project file 我没有改变我的项目文件中的任何内容

QT       += core

QT       += gui

TARGET = Layouts
CONFIG   += gui
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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