Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

qt

qwidget

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.

like image 488
Simplicity Avatar asked Apr 02 '11 07:04

Simplicity


3 Answers

The problem is not with this .pro; it is most likely in main.cpp. Qt requires you to create a QApplication before creating any QWidget subclasses (as well as certain other classes, such as QPixmap). Your main function should begin with the line:

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.

like image 144
Daniel Gallagher Avatar answered Oct 27 '22 01:10

Daniel Gallagher


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 your main.cpp (where you start your application)

In the main(int,char**){

exchange QCoreApplication a(argc, argv); with QApplication 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();}

like image 12
OMG-1 Avatar answered Oct 27 '22 02:10

OMG-1


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.

like image 3
Simplicity Avatar answered Oct 27 '22 00:10

Simplicity