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.
Related
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();
}
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.
I've made LONG research on the web but can't find anything clear.
I think the answer is obvious but i'm beginer in Qt.
Why this code doesn't work ? My windows just popup realy fast.
Main.cpp
#include <QApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test test;
return a.exec();
}
Test.cpp
#include "test.h"
Test::Test()
{
MainWindow w;
w.show();
}
And this one work (the window keep open) :
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Thank you !
The window closes because it is local variable of the Test constructor, and when the constructor exits, its destructor gets called, which closes it. You need to make the window object a member variable of the Test class.
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.
I would like to ask if you could help me with my app.
I have an application in C++ using Qt (and Qwt) in MCVS 2010. I want to open QDialog window with QwtPlot on button click from Main Window (QMainWindow). Here is some code:
MainWindow.cpp
void MainWindow::on_pushButton_1_clicked ()
{
Dialog_plot dp;
dp.setModal(true);
dp.exec();
}
Dialog_plot.cpp:
#include <qwt_plot.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_curve.h>
#include "Dialog_plot.h"
Dialog_plot::Dialog_plot(QWidget *parent)
{
plot = new QwtPlot();
//more code...
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
This code is compiling, but when I click on that pushbutton_1 in my application I'm getting error:
Must construct a QApplication before a QPaintDevice
I know that such error was discussed many times and I was reading a lot about it but I can't see solution for my problem.
One more thing I would like to mention - I have similar application with Qwt plot written by somebody else and his application compiles and works without any problems in my MCVS. I was trying to compare Linker/libraries included but it seems to be the same. So I guess there is a problem with my application, I just can't solve it. I really need some help!