Qt - QWidget:未使用 GUI 时无法创建 QWidget
我正在尝试运行一个简单的 Qt 程序,当这样做时,我得到一个控制台窗口,其中提到:QWidget:在没有使用 GUI 时无法创建 QWidget
,第二行此应用程序已请求运行时终止......
,.exe
文件因此停止工作。
我的 .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
对此有什么想法吗?
谢谢。
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.
My .pro
file looks as follows:
#-------------------------------------------------
#
# 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题不在于这个
.pro
;它很可能位于main.cpp
中。 Qt 要求您在创建任何 QWidget 子类(以及某些其他类,例如 QPixmap)之前创建 QApplication。您的main
函数应以以下行开始:并可能以以下行结束:
在这些调用之间,您应该创建并显示主窗口。
The problem is not with this
.pro
; it is most likely inmain.cpp
. Qt requires you to create a QApplication before creating any QWidget subclasses (as well as certain other classes, such as QPixmap). Yourmain
function should begin with the line:and will probably end with a line like:
In between these calls, you should create and show your main window.
我发现您可以使用 Qt Console 项目来完成此操作,但是当您完成我的编辑后,它当然不会具有控制台程序的功能。
首先,您需要在
main.cpp
(您开始的位置)中将#include
与#include
交换您的应用程序)在
main(int,char**){
中将
QCoreApplication a(argc, argv);
与QApplication a(argc, argv);< /code>
在 QApplication 和 return a.exec 之间,你有你的小部件和其他 gui 相关的东西
,最后你使用
return a.exec();}
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.
First of all you need to exchange
#include <QtCoreApplication>
with#include <QApplication>
in yourmain.cpp
(where you start your application)In the
main(int,char**){
exchange
QCoreApplication a(argc, argv);
withQApplication a(argc, argv);
and in between QApplication and return a.exec you have your widget and other gui related stuff
and in the end you use
return a.exec();}
我想我找到了问题所在。
由于我使用的是
Qt Creator
,并且在创建新项目时,我选择了Qt Console Application
而不是Qt Gui Application
。I think I found where the issue is.
Since I'm using
Qt Creator
, and when creating a new project, I was choosingQt Console Application
instead ofQt Gui Application
.当您的应用程序不是 QApplication 实例时,会发生“QWidget:未使用 GUI 时无法创建 QWidget”。
来自 Qt 文档:
"QWidget: Cannot create a QWidget when no GUI is being used" happens when you application isn't QApplication instance.
From Qt docs:
从文档中,
QApplication 类管理 GUI 应用程序的控制流和主要设置,同时
QCoreApplication 类为控制台 Qt 应用程序提供事件循环
我遇到了同样的问题,默认的 QT 控制台应用程序使用 QCoreApplication 而不是 QApplication 来运行应用程序。
这是我为使其工作所做的事情
并且我没有更改项目文件中的任何内容
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
I had the same problem, the default QT Console App uses the QCoreApplication instead of the QApplication to run the application.
Here is what i did to make it work
And i did not change anything in my project file