When i compile my qt project showed below error?
QWidget: Cannot create a QWidget without QApplication
What is the problem?
Main.cpp
#include<QApplication>
#include"MainWindow.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}
You need a QApplication
to have a QWidget
. Change QGuiApplication
to QApplication
and the code will run just fine.
#include <QApplication>
#include "MainWindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}
If you want to know "Why there are three main classes like QApplication, QGuiApplication and QCoreApplication", see this. It says:
QCoreApplication is the base class, QGuiApplication extends the base class with functionality related to handling windows and GUI stuff (non-widget related, e.g. OpenGL or QtQuick), QApplication extends QGuiApplication with functionality related to handling widgets.
Btw, isn't it the basic example available on Qt Creator? You need a book to learn Qt, and I suggest you to read "C++ GUI Programming with Qt 4 (2nd Edition)" from Jasmin Blanchette.
You should change QGuiApplication
to QApplication
in your main.
From QGuiApplication Class Description:
For QWidget based Qt applications, use QApplication instead, as it provides some functionality needed for creating QWidget instances.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With