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.
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();
}
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.
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'm started studying QT. When I create a MainWindow and placing some widgets on it in ane layout, there is a gap between edge of window and widgets, like that:
How can I switch off this gaps?
layout()->setContentsMargins(0,0,0,0);
and editing stylesheets of window, but there was no effect. What should I do?
A QMainWindow is slightly different than a QDialog or QWidget in that it has the concept of a "central widget". The window has predefined areas to handle stuff like toolbars and menus and docks, and defines the central widget as the main content for the window. The window itself is not usually assigned a layout. But what I assume you are doing is setting the values on the windows layout (which will not have an effect).
The widget you set as the central widget will most likely have its own layout. By default the central widget can expand to the edges already. Consider this example first:
#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QListWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *window = new QMainWindow;
window->resize(800,600);
QListWidget *listWidget = new QListWidget;
window->setCentralWidget(listWidget);
window->show();
return a.exec();
}
You will see the list widget fully expanded to the edges. But in a more realistic example:
#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QListWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow *window = new QMainWindow;
window->resize(800,600);
QWidget *central = new QWidget;
QListWidget *listWidget = new QListWidget;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(listWidget);
//Uncomment this following line to remove margins
//layout->setContentsMargins(0,0,0,0);
central->setLayout(layout);
window->setCentralWidget(central);
window->show();
return a.exec();
}
You have a container widget, which is then composed with a layout, and the list widget. The layout of this central widget is the one that introduces the margins.