How can i stop QT Widget from closing? - c++

I have a class (Window) that inherits from QWidget (Not QMainWindow).
This is the important part of my main function.
QApplication app (argc, argv);
QMainWindow *window = new QMainWindow;
QMenuBar *tool_bar = new QMenuBar (window);
Window *graph_area = new Window (arguments);
window->setMenuBar (tool_bar);
window->setCentralWidget (graph_area);
window->show ();
app.exec ();
I thought that i would just override "closeEvent", but for some reason it doesn't get called when pressing the closing button.
I want to stop the user from closing the application if some process is still working.
Any ideas about how i might achieve that?

You can either:
Subclass QMainWindow, instead of QWidget
Override QMainWindow::closeEvent, instead of QWidget::closeEvent
Allocate your QMainWindow subclass on the stack, instead of on the heap, i.e.:
MyMainWindowSubclass window;
or:
Change your code to:
QApplication app(argc, argv);
Window w;
window->show();
app.exec();
and Window::closeEvent will be called.

Make your Window class an event filter and install it on the main window object. You’ll reimplement filterEvent method in Window. Do not forget to install it on the main window! And make sure that the filter filters the event out - the boolean result indicates whether you let the result through or filter it out. Look up the exact value needed to indicate either one - it’s a bad api and I never remember whether true or false does what I intend. It’s a classical case where an enumeration return value would work better, like Qt::Accept/Qt::Reject or some such.

I actually found another solution to my problem, that I find more pleasant. I can create a custom class mainwindow that inherits from QMainWindow and use it instead of QMainWindow. It might be more appropriate, because I will probably need some other changes to QMainWindow as well.

Related

Application with multiple windows in Qt

What is a good way to create application with multiple windows in Qt? Something like GIMP. I have already read this answer and it is not what I need.
I have two windows, one with controls and one with OpenGL inside. Right now I have something like this:
int main()
{
// some code
MainWindow mw;
mw.show();
GLWindow gw;
gw.show();
// ...
}
I don't like this for two reasons. First reason is when I start my application, window with OpenGL will be on top (because it is last to call show()) but MainWindow will be buried somewhere under all opened windows. What I need is both windows in front of everything (like GIMP), preferably with focus on MainWindow (I guess I can bring them to front, so that is minor issue). Second reason I don't like this is that my application will be closed completely only when I close both windows.
So I was thinking of having a reference to GLWindow inside MainWindow, and creating it from MainWindow.
Would that be a good way to create application with several windows?
EDIT: GLWindow inherits from QOpenGLWindow.
You are doing right, but with the following simple tricks, you can resolve both issues that cause you do not like your right method:
To activate both windows, just do as follows:
MainWindow mw;
mw.show();
mw.activateWindow();
GLWindow gw;
gw.show();
gw.activateWindow();
To resolve the quit problem, you have to override the closeEvent in both windows. To do that, add the following code into the header file of your both windows:
protected:
void closeEvent(QCloseEvent *event) override;
and in the implementation of the closeEvent, just write qApp->quit();. This way once you close either window, your application will terminate completely.
MainWindow.cpp
void MainWindow::closeEvent(QCloseEvent *event)
{
qApp->quit();
}
and
GLWindow.cpp
void GLWindow::closeEvent(QCloseEvent *event)
{
qApp->quit();
}
I'm not sure I completely understand the situation but, typically, you can make the 'secondary' widgets child dialogs of the QMainWindow. Given your example code that would be something like...
int main ()
{
// some code
MainWindow mw;
mw.show();
/*
* Secondary windows should be created with the QMainWindow mw
* as their parent.
*/
GLWindow gw(&mw);
/*
* Set the Qt::Dlaog window flag to `encourage' the QMainWindow
* and its children to behave as a group.
*/
gw.setWindowFlags(gw.windowFlags() | Qt::Dialog);
gw.show();
// ...
}
Now all widgets behave as a group (possibly subject to the window manager being used) and closing the QMainWindow will, by default, close the application.

Non-modal QWidget dialog that stays on top of the window

I want a dialog which stays on top of my main window and not other windows. I derived a class and added some flags. If I call the dialog now with show() the dialog appears and is staying on top as long as I don't press a button or whatever. Then the dialog goes to background again.
Dial::Dial(QWidget *parent) : QWidget(parent)
{
this->setWindowFlags(Qt::Tool | Qt::Dialog);
// ...
Consequently, I looked into the docu and found this:
Indicates that the widget is a tool window. A tool window is often a
small window with a smaller than usual title bar and decoration,
typically used for collections of tool buttons. If there is a parent,
the tool window will always be kept on top of it.
Happily, I added this line into my singleton creating the dialog.
d->mainWindow = new Foo();
d->dial->setParent(d->mainWindow);
Now the dialog is just embedded into my central widget (QOpenGlWidget) and is not a dialog anymore. Somehow, I seem to lack understanding what the docu is telling me? How can I get the dialog stay on top of my application and what does the docu mean?
I'm not able to reproduce your problem. The following code will generate a QWidget that will allways stay on top of the QMainWindow:
#include "QApplication"
#include "QMainWindow"
#include "QLineEdit"
int main(int argc, char * argv[])
{
QApplication a(argc, argv);
QMainWindow w;
w.show ();
QWidget *pLineEdit = new QWidget(&w);
pLineEdit->setWindowFlags(Qt::Tool | Qt::Dialog);
pLineEdit->show ();
a.exec ();
}
Tested with Qt 5.9.
Not sure if you've already solved this by now but you can try the WindowStaysOnTopHint flag when you construct the dialog:
Qt::WindowFlags flags = this->windowFlags();
flags |= Qt::WindowStaysOnTopHint;
this->setWindowFlags(flags);
Then use show() instead of exec() to make it non-modal:
dlg->show();
You need to set the modality (documentation) of the widget, like this:
QWidget *dialog = new QWidget(window, Qt::Dialog);
dialog->setWindowModality(Qt::ApplicationModal);
dialog->show();
However, I'd recommend to use the pre-configured QDialog class, which handles all that stuff for you:
QDialog *dialog = new QDialog(window);
dialog->exec();
Use QDialog instead of QWidget, and pass the parent widget in its constructor function.
QDialog* pDlg = new QDialog(this);
pDlg->show();

How to put osgEarth's ViewerWidget into an tabbed MdiArea?

Is there something special about putting osgEarth's ViewerViewer into a QMdiArea? I created a QMdiArea as central Widget (called setCentralWidget) instead of taking osgEarth's viewer directly as central widget.
QMdiArea *mdiArea = new QMdiArea(this);
setCentralWidget(mdiArea); // call to QMainWindows method, snippet is taken from app's MainWindow
mdiArea->addSubWindow(viewerWidget); // this doesn't work, globe is not drawn
Everything I tried didn't worked... except osgEarth's ViewerWidget is set as central widget of my MainWindow. Also tried MultiViewerWidget without any success but because I need only one view the ViewerWidget should be ok, or not?
I had a look into the examples but didn't succed to use one of them as starting point.
Any hints? Thank's in advance.
you can try this, where Form1 is a QDialog
in main.cpp
int main()
{
QApplication a(argc, argv);
Form1 w=new Form1();//qdialog
.................//do something to initial the map
w.loadWidget(viewerWidget);
w.show();//the order of the loadwiget() and show() is important!!!!!
a.exec();
}
in Form1.cpp
void Form1::loadWidget(QWidget *qwidget)
{
qwidget->setMinimumSize( ui.mdiArea->width(),ui.mdiArea->height());
QMdiSubWindow * subW=ui.mdiArea->addSubWindow(qwidget);
subW->setWindowFlags(Qt::SubWindow | Qt::FramelessWindowHint);
subW->maximumSize();
}
This works well with qt 4.8.4+osgearth 2.3
Try setting the subwindow's geometry before starting the UI.
QMdiSubWindow* sw = mdiArea->addSubWindow(viewerWidget);
sw->setGeometry(...);
Otherwise OSG will probably become confused.
Got this answer by Gwaldron in the osgEarth forum here and it worked.
Also setting a minimum size for the viewerWidget will help (e.g. for positioning on TabWidget). See my question and answer here.

Qt opening another window when first one has closed

I´ve beeing programming Java for some time right now...Now that I got into C++ and Qt I am a bit lost about GUI Thread (EDT Thread) and Worker Thread
I am trying to make the main window of my application open only when the configuration window is closed.
I dont want to put the code for creating the main window in the OK button of my configuration window.
I tryed to make them modal but the main window still opens.....
Afther configuration is complete I still have to see if there is an application update...So its something like
EDIT: This is my main:
ConfigurationWindow *cw = new ConfigurationWindow();
//if there is no text file - configuration
cw->show();
//**I need to stop here until user fills the configuration
MainWindow *mw = new MainWindow();
ApplicationUpdateThread *t = new ApplicationUpdateThread();
//connect app update thread with main window and starts it
mw->show();
Try something like this:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog *dialog = new QDialog;
QSlider *slider = new QSlider(dialog);
QHBoxLayout *layout = new QHBoxLayout(dialog);
layout->addWidget(slider);
dialog->setLayout(layout);
dialog->exec();
qDebug() << slider->value(); // prints the slider's value when dialog is closed
QMainWindow mw; // in your version this could be MainWindow mw(slider->value());
w.show();
return a.exec();
}
The idea is that your main window's constructor could accept parameters from the QDialog. In this contrived example I'm just using qDebug() to print the value of the slider in the QDialog when it's closed, not passing it as a parameter, but you get the point.
EDIT: You might also want to "delete" the dialog before creating the main window in order to save memory. In that case you would need to store the parameters for the main window constructor as separate variables before deleting the dialog.
You have to learn about signals and slots. The basic idea is that you would send a signal when you configuration is finished. You put your QMainWindow in a member variable and call mw->show() in a slot of your main programm that is connected with the configurationFinished signal.
If your ConfigurationWindow is a QDialog, you could connect the finished(int) signal to the MainWindow's show() slot (and omit the show() call from main).

Set QLineEdit focus in Qt

I am having a qt question. I want the QLineEdit widget to have the focus at application startup. Take the following code for example:
#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLineEdit>
#include <QtGui/QFont>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
window->setWindowIcon(QIcon("qtest16.ico"));
window->setWindowTitle("QtTest");
QHBoxLayout *layout = new QHBoxLayout(window);
// Add some widgets.
QLineEdit *line = new QLineEdit();
QPushButton *hello = new QPushButton(window);
hello->setText("Select all");
hello->resize(150, 25);
hello->setFont(QFont("Droid Sans Mono", 12, QFont::Normal));
// Add the widgets to the layout.
layout->addWidget(line);
layout->addWidget(hello);
line->setFocus();
QObject::connect(hello, SIGNAL(clicked()), line, SLOT(selectAll()));
QObject::connect(line, SIGNAL(returnPressed()), line, SLOT(selectAll()));
window->show();
return app.exec();
}
Why does line->setFocus() sets the focus on the line widget #app startup only if it is placed after laying out the widgets and if used before it's not working?
Keyboard focus is related to widget tab order, and the default tab order is based on the order in which widgets are constructed. Therefore, creating more widgets changes the keyboard focus. That is why you must make the QWidget::setFocus call last.
I would consider using a sub-class of QWidget for your main window that overrides the showEvent virtual function and then sets keyboard focus to the lineEdit. This will have the effect of always giving the lineEdit focus when the window is shown.
Another trick that might work is by using the singleshot timer:
QTimer::singleShot(0, line, SLOT(setFocus()));
Effectively, this invokes the setFocus() slot of the QLineEdit instance right after the event system is "free" to do so, i.e. sometime after the widget is completely constructed.
Perhaps this is an update as the last answer was in 2012 and the OP last edited the question in 2014. They way I got this to work was to change the policy and then set the focus.
line->setFocusPolicy(Qt::StrongFocus);
line->setFocus();
In Qt setFocus() is a slot, you can try other overloaded method which takes a Qt::FocusReason parameter like the line shown below:
line->setFocus(Qt::OtherFocusReason);
You can read about focus reason options in the following link:
http://doc.trolltech.com/4.4/qt.html#FocusReason-enum