Qt close a running window through code - c++

I have a program that opens another window and i want the old window to close. Is there some function or something that would close the window through the code but keep the other window running?

this->close();
or
close();
Ref: http://doc.qt.digia.com/4.7/qwidget.html#close

void Dialog::on_Close_clicked() // Condition for Main thread program stopping
Running Thread->closethread = true;
this->close();
void Dialog::on_Stop_clicked() // Condition For Stop Button for your particular task
{
this->close();
}

MyWidget *newform = new MyWidget;
newform->show();
this->hide();

Use signal and slot mechanism.
Whenever you want to close that window or screen emit a signal and connect this signal to the close slot of that window. This should work fine.

Related

What is the signal emitted when we close a Window in qt

I have my MainWindow that open a second Window.
When I click on a button in my second window, a thread is launched and I want my thread to end when I quit my second window.
What is the signal emitted when my SecondWindow is closed ?
Thank you for your futures answers
There is no signal emitted when widgets (including QMainWindows) are closed. If a widget is set to be deleted when it is closed then you could use the following QObject signal to detect when the widget is about to be destroyed...
void destroyed(QObject *obj = Q_NULLPTR)
However this will only work if your window has the Qt::WA_DeleteOnClose flag enabled (it is not enabled by default).
Alternatively and probably more preferably you can implement the standard widget close event and emit your own signal to indicate that the window was closed:
void MainWindow::closeEvent( QCloseEvent* event )
{
emit MySignalToIndicateThatTheWindowIsClosing();
event->accept();
}

Qt: How to close a dialog window opened with exec()?

I'm making a c++ application in Qt, and need to programmatically close a dialog window (opened with this->exec();) via code after a certain function finishes executing.
I'm using Qt 5.6.
Thanks in advance!
Here is an example of my code, that doesn't work (Worker is the dialog Class):
void MainWindow::on_pushButton_2_clicked()
{
//When Start button clicked:
Worker worker;
worker.exec();
//worker.run(1);
worker.accept();
}
So when pushButton_2 is clicked, I want a dialog to open that gives out the current progress, and when that is done, I want it to close.
Edit:
Now you posted more code....
worker.exec();
worker.accept(); // or worker.close();
exec() starts QDialog events processing loop and will return only when completed (after accept(), reject() or done(int) is called). So worker.accept() will not be reached (you should see that if using your debugger). It must be called by worker itself after a user action (button click by instance).
What you meant to do is:
worker.show();
QThread::sleep(2); // wait for 2 seconds
worker.accept();
Then, worker.accept() will be executed at some point. Dialog is shown, but it's modal.
Old post (before edit):
You can call accept() to do as if user clicked OK or reject() to do as if user clicked Cancel.
Note that those are slots, so you can fire them by connecting a signal to them (signal to be emitted when you function finishes executing for instance).
Example:
void MyDialog::doSomethingAndClose()
{
// do your stuff here
accept(); // will close the dialog
}
or:
void MyDialog::doSomethingAndClose()
{
// do your stuff here
emit weAreDone();
}
If you earlier connected (in MyDialog constructor for instance):
connect( this, SIGNAL(weAreDone()), this, SLOT(accept()) );
Just connect your custom signal with QDialog::done(int) and emit signal after your function finishes executing.
As I've just learned, the issue is caused by the gui not updating automatically.
Here is a link to a SO question that fixes this issue.

How to keep the application running when the last window is closed?

I'm working on a very basic GUI project in Qt (using c++) and want to be able to close the main window in my program without the program quitting all the way. By default, it will exit when the main window is closed. How to prevent that?
Set the QApplication::quitOnLastWindowClosed property to false:
qApp->setQuitOnLastWindowClosed(false);
If you still need your window to exist, you would probably like to reimplement your closeEvent method like this:
void MainWindow::closeEvent(QCloseEvent *event)
{
hide();
event->ignore();
}
or use QGuiApplication::setQuitOnLastWindowClosed(false)
If you want to perform some pre-exit operation as saving settings, connect some slot doing what you want to QCoreApplication::aboutToQuit()

How to call a slot on quit

I want to update my database just before my Qt application closes.
I want something like connect(this, SIGNAL(quit()), this, SLOT(updateDatabase()))
One way could be to introduce a quit button, but is it possible to achieve this functionality if user presses Alt+F4?
Use signal aboutToQuit() instead.
This signal is emitted when the application is about to quit the main
event loop, e.g. when the event loop level drops to zero. This may
happen either after a call to quit() from inside the application or
when the users shuts down the entire desktop session.
The signal is particularly useful if your application has to do some
last-second cleanup. Note that no user interaction is possible in this
state.
For example :
connect(this, SIGNAL(aboutToQuit()), this, SLOT(updateDatabase()));
There is another way to do it, not aboutToQuit() signal, but to re-implement the closeEvent(QCloseEvent *event). You can call you slot before the statement event->accept();
like this:
void MainWindow::closeEvent(QCloseEvent *event)
{
call_your_slot_here();
// accept close event
event->accept();
}

How to catch the QSystemTrayIcon quit event?

I have a QSystemTrayIcon subclass. Inside it, I have:
quitAction = new QAction(tr("&Quit"), m_parent);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
CloseEvent doesn't work here. How can I catch this quit event? I want to save data before closing.
You should connect your cleanup/save code to the QCoreApplication::aboutToQuit() signal.
This signal is emitted when the application is about to quit the main event loop, e.g. when the event loop level drops to zero. This may happen either after a call to quit() from inside the application or when the users shuts down the entire desktop session.