Windows cannot terminate process when i'm shutting down or restarting computer
-
I have a clear project. There is only one slot onbuttonclick and there is just one command: this->hide(); When i push on it and form is hiding i cannot shutdown or restart my windows until i close the app or show form. Can you help me with it, please? Qt 5.2.1 Static
Sorry for my bad english.
-
When you say you can't shutdown... do you mean the desktop buttons / icons / windows are non-responsive? or what is happening?
Please show the code as well so we can see it :)
-
mainwindow.cpp
@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
this->hide();
}@mainwindow.h
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void on_pushButton_clicked();private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H@
if application was started and hidded (this->hide) and i'm trying to shutdown or restart windows, nothing just happens. if i don't hide form and i'm trying to shutdown or restart windows, windows successful shutting down.
-
well, if you hide the only one form in a GUI application then there's no way left for you to close the application, so the process keeps running and windows will complain about this when you try to shut down (it should eventually ask you to terminate the process manually when you shut down). Try looking in your windows task manager and you'll probably find your application running there (and you can force-kill the process from there).
The bottom line is, you should never close all the windows of an application if you want to be able to shut down its associated process(es); this is however a way to have a window-less application running as a background process.
-
so, if i hide form. there is no way to catch WM_CLOSE event from windows?
-
You can catch the events of course (hiding does not stop your program/app from running.
But you may have to code it up! - you have to write some sort of event handler to catch the close event.
In your mainwindow you can implement the function:
@
bool QObject::event(QEvent * e) [virtual]
@And handle all events manually - i.e. look for the window close event in there. Look at the documentation for some examples...
-
Had a quick look around, and I think you need to catch the
@
void QCoreApplication::aboutToQuit() [signal]
@signal, so make a connection with this to some slot of your own in MainWindow to close your app.
-
@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(Test()));
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
this->hide();
}void MainWindow::Test()
{
qApp->exit();
}@i've changed my mainwindow class like that, but my Windows still don't want to shutdown.. all programs are closing, excluding my application
-
I don't know how to catch events in Qt, but WM_CLOSE will never reach an invisible window. I googled a little for WM_CLOSE, WM_QUIT, etc, and apparently there's a PeekMessage function which allows you to see if there's a WM_QUIT message sent to your app, see here http://stackoverflow.com/questions/3155782/what-is-the-difference-between-wm-quit-wm-close-and-wm-destroy-in-a-windows-pr . However, using these functions will obviously make your application not directly cross-platform, so you should probably wrap windows' PeekMessage in a function of your own which should have different platform-dependent implementations.
-
hmmm....well if windows never gives the close signal/event to a hidden window/app then there is not much Qt can do about that :o
To test when events you do get, you can implement
@
bool QObject::event(QEvent * e) [virtual]
@In your MainWindow, and just print out ALL event types, somthing like (not tested code):
@
bool MainWindow::event(QEvent * e)
{
qDebug() << "Event type:" << e->type();// I think this means the event has now been "consumed" and no further processing is required. So false means, carry on... return false;
}
@Then you can check if there is ANY event that comes when you shutdown windows
-
code_fodder, it is not good, because there are only two events when i'm trying to shutdown Windows.. and these events are also using in application initialization. QEvent::ActivationChange, QEvent::WindowActivate . now i'm trying to catch windows messages with winapi function PeekMessage