Using QT; QWidget: Must construct a QApplication before a QWidget - c++

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

Related

Using a Designer .ui File in Your Application example not working

I'm trying to build a UI form and interface to it using the example from:
https://doc.qt.io/qt-5/designer-using-a-ui-file.html
#include "ui_item.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget widget;
Ui::Item ui;
ui.setupUi(&widget);
widget.show();
return app.exec();
}
However, it just doesn't work. In the line
ui.setupUi(&widget); I get the error:
invalid conversion from ‘QWidget*’ to ‘QFrame*’
Is there something I'm missing? The top-level widget in Ui::Item is a QFrame. I'm using Qt 5.12 and Qt-Creator 4.12.4.
The issue was that my UI top level was a QFrame and it needed to be a QWidget. For whatever reason, the UI file generated as a QFrame.

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.

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.

Cannot create a QWidget without QApplication

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.