Cannot create a QWidget without QApplication - c++

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.

Related

Using QT; QWidget: Must construct a QApplication before a QWidget

I am using vtk QT, i've installed VTK, PCL libraries well, after run the project the realese mode; i found the following error.
QWidget: Must construct a QApplication before a QWidget
As the error message says, you will need to create a QApplication before you can instantiate and use a QWidget. For example:
#include <QApplication>
#include <QDialog>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDialog dialog;
dialog.setModal(true);
dialog.show();
return app.exec();
}

Open widget in another window on macOS

The app opens another widget as a pane. I guess this follows macOS guidelines but is it possible to force widget to open in another window?
Like this:
#include <QApplication>
#include <QMainWindow>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QMainWindow w;
w.setWindowTitle("Main Widget");
w.show();
QWidget anotherWidget;
anotherWidget.setWindowTitle("Another Widget");
anotherWidget.show();
return a.exec();
}
To do that, you need to set the flag of the second widget to Qt::Dialog
QWidget anotherWidget;
anotherWidget.setWindowTitle("Another Widget");
anotherWidget.setWindowFlag(Qt::Dialog);
anotherWidget.show();
Open the anotherWidget as a QMainWindow type instead of QWidget.

Getting error : Must construct a QGUIApplication first

I am developing a GUI application, but whenever I am trying to close the application, it throws an error that "Must construct QGuiapplication first". My main is not returning exit code 0, so it's not exiting normally. I think some destructor is getting called twice but need some help here. I am attaching main.cpp code here for reference.
#include <QGuiApplication>
#include <QFontDatabase>
#include <QtWebEngine>
#include "ApplicationManager.h"
#include "AppLogger.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
QGuiApplication app(argc, argv);
QtWebEngine::initialize();
app.setApplicationName("MCS3.0");
QFontDatabase::addApplicationFont(":/Fonts/Roboto.ttf");
#ifdef VERSION
app.setApplicationVersion(QString("%1").arg(VERSION));
logInfoMessage(app.applicationName()+app.applicationVersion()+" Started");
#endif
ApplicationManager::instance().run();
return app.exec();
}
The relevant part of the problem is inside ApplicationManager.h which was not exposed by OP.
I bet that it makes another instance of QApplication (or QGUIApplication or QCoreApplication).
How can I know this? It's partly a guess (as the name looks like) and partly result of the following test:
testQApp.cc:
#include <QtWidgets>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
{ QApplication app(argc, argv);
QLabel qLbl("The app in app");
qLbl.show();
app.exec();
}
return app.exec();
}
testQApp.pro:
SOURCES = testQApp.cc
QT = widgets
Compiled and tested in cygwin64 on Windows 10:
$ qmake-qt5 testQApp.pro
$ make
$ ./testQApp
When I quit the application, the issue occurs:
QApplication::exec: Please instantiate the QApplication object first
Segmentation fault (core dumped)
$
To make this complete, the relevant paragraph of doc. about QApplication:
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.
Please, note that the emphasize is not done by me.

Qt hangs when calling QApplication

I just did a fresh install of Qt Creator so I have the latest version. I opened my project that I was working on from the previous install of Qt Creator. It complies without errors but seams to hang on this line:
QApplication a(argc, argv);
Here is the full main.cpp file:
#include "mainwindow.h"
#include <QApplication>
#include <QMouseEvent>
#include<QTextStream>
#include<QString>
int main(int argc, char *argv[]){
qDebug()<<1;
QApplication a(argc, argv);
qDebug()<<2;
MainWindow w;
w.show();
return a.exec();
}
When it runs my application will open displaying the window but nothing works.
Here is the applciation's output:
Starting /home/bandito/programming/build-etf-ipo-Unnamed-Debug/etf-ipo...
1
As you can see qDebug()<<1; is called but the application never makes it to qDebug()<<2;
I also tried replacing QApplication with QGuiApplication and QCoreApplication but both request the QApplication be called also.

QT QMainWindow from subroutine

I have problems to create a QMainWindow object from a subroutine. I need this to create a different number of windows on the user's request.
The problem lies in the fact that if I create it from a subroutine, it is created as a local variable and only shows for a split second. Usually I would need to return a pointer, but I have not managed to do it with this object. Here is the code:
#include <QApplication>
#include <QMainWindow>
QMainWindow runGUI(){
QMainWindow window;
window.show();
window.resize(340,260);
return window;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
/*
QMainWindow window;
window.show();
window.resize(340,260);
*/
runGUI();
return app.exec();
}
How do I do this correctly?
Returning your QMainWindow from runGUI() calls the copy constructor: Qt objects are not designed to be copied. You should indeed return a pointer, and dynamically allocate the window:
#include <QApplication>
#include <QMainWindow>
QMainWindow* runGUI(){
QMainWindow* window = new QMainWindow;
window->setAttribute(Qt::WA_DeleteOnClose);
window->show();
window->resize(340,260);
return window;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow* window = runGUI();
return app.exec();
}
Note the use of Qt::WA_DeleteOnClose to ensure the window will be deleted when it is closed, avoiding memory leaks.