13

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.

Simplicity
  • 47,404
  • 98
  • 256
  • 385

5 Answers5

18

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.

Daniel Gallagher
  • 6,915
  • 25
  • 31
12

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();}

OMG-1
  • 498
  • 1
  • 6
  • 20
3

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.

Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • 1
    Yes, Qt Console Application will create QtCoreApplication which is non-gui and start a console for you. You should use QtApplication instead. – winterTTr Apr 02 '11 at 07:36
  • Another note: GUI requires `QApplication` and `QT+=gui`, and a console requires use of `QCoreApplication` and `CONFIG+=console`. You cannot get a console to display with `QT+=gui` no matter how you link. (You can have two executables, or have one call the other, or implement your own console-like thing from your GUI app.) – charley Jul 27 '11 at 01:14
  • My problem was the same. By changing "QtCoreApplication" to "QtApplication", it worked. – Kamran Bigdely Sep 02 '14 at 04:39
2

"QWidget: Cannot create a QWidget when no GUI is being used" happens when you application isn't QApplication instance. From Qt docs:

QApplication specializes QGuiApplication with some functionality needed for QWidget-based applications. 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. For non-QWidget based Qt applications, use QGuiApplication instead, as it does not depend on the QtWidgets library.

INeedMySpace
  • 325
  • 4
  • 12
0

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

#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