qt window close but process still running
I have tried with the lastWindowClosed() but still the while loop is running although the window is close.
here is some parts of my code;
QTMainForm::QTMainForm(QWidget* parent)
{
btn.setText("start");
btn2.setText("stop");
connect(&btn, SIGNAL(clicked()), this, SLOT(OnBtnstrt()));
progress.setValue(0);
progress.setRange(0, 1000000);
progress.setWindowTitle(tr("Find Files"));
vb.addWidget(&progress);
vb.addWidget(&btn);
vb.addWidget(&btn2);
setLayout(&vb);
}
void QTMainForm::OnBtnstrt()
{
for (int i = 0; i < 10000000; i++)
{
progress.setValue(i);
qApp->processEvents();
timer.setInterval(1000);
if(btn2.isDown())
{
break;
}
progress.show();
}
}
int main(int argc, char *argv[])
{
QTMainForm* pMainForm = 0;
QApplication app(argc, argv);
app.setObjectName("client");
app.setQuitOnLastWindowClosed(true);
pMainForm = new QTMainForm();
pMainForm->resize(300, 300);
pMainForm->show();
pMainForm->setAttribute(Qt::WA_QuitOnClose);
QObject::connect( qApp, SIGNAL( lastWindowClosed() ),&app, SLOT( setLastWinClosed() ) );
return app.exec();
}
any help will be really appreciated.
Re: qt window close but process still running
this is a big number 10000000 for loop are you sure of this
Re: qt window close but process still running
yes. it does. but the problem is when I close the gui window, the process still runs. any help on that?
Re: qt window close but process still running
Quote:
void QTMainForm::OnBtnstrt()
{
pm = new QTMainForm();
btn.isHidden();
You are creating a new QTMainForm, what is this for. (I mean you already have one)
Re: qt window close but process still running
previously, I was trying some code to stop the "while" loop when the window is close. but it didnt work. but i have already taken out them and now i have QTMaiinForm inside the int main(). I complied but still the same problem.
Re: qt window close but process still running
You shouldn't use isDown() on a button. Connect a slot to the button and in that slot, you set a stop condition to true.
Something like pleaseStop = true;
Your loop will become something like
while (!pleaseStop)
{
Whatever..
}.
Don't forget to initialize the stop condition before starting the loop.
Re: qt window close but process still running
Quote:
Code:
void QTMainForm::OnBtnstrt()
{
for (int i = 0; i < 10000000; i++)
{
progress.setValue(i);
qApp->processEvents();
timer.setInterval(1000);
if(btn2.isDown()) // <<<<<<<<<<<<<<<<<<
{
break;
}
progress.show(); // <<<<<<<<<<<<<<<<<<
}
}
Are you able to see progress bar?
Where is timer code?
I think it is not good to check for btn2.isDown()
I think you are complicating things, here are few tips which you can try.
Comment out the button click slot and see app exits
Remove the timer stuff, see it helps
also the last window closed signal and slot connetion is redudant (it is default behaviour)
Re: qt window close but process still running
In your constructor, you aren't calling the base class constructor. If your QTMainForm has a parent, this means that the proper parent-child relationship is not being set up.
Since you don't give a header file, it is impossible to know what class QTMainForm is derived from, so I will assume it is QDialog for the purpose of the example:
Code:
QTMainForm
::QTMainForm(QWidget* parent
): QDialog( parent
) // <---- you should always do this, even if "parent" is NULL {
btn.setText("start");
btn2.setText("stop");
connect(&btn, SIGNAL(clicked()), this, SLOT(OnBtnstrt()));
progress.setValue(0);
progress.setRange(0, 1000000);
progress.setWindowTitle(tr("Find Files"));
vb.addWidget(&progress);
vb.addWidget(&btn);
vb.addWidget(&btn2);
setLayout(&vb);
}
Please learn about "CODE" tags and use them next time you post source code. (Click "Go Advanced" then click the "#" icon on the toolbar. This inserts a pair of CODE tags. Paste your source code in between them). This will also preserve any indentation in your source file, which will make your code easier to read.
Re: qt window close but process still running
ok, I have changed my code and it is like this now;
QTMainForm::QTMainForm(QWidget* parent)
{
btn.setText("Start");
connect(&btn, SIGNAL(clicked()), this, SLOT(OnBtnstrt()));
progress.setValue(0);
progress.setRange(0, 1000000);
vb.addWidget(&progress);
vb.addWidget(&btn);
setLayout(&vb);
}
void QTMainForm::OnBtnstrt()
{
double i=0;
while(i<=1000000)
{
i++;
qApp->processEvents();
progress.setValue(i);
}
}
still the same problem occurs. As I was observing if I put the value of "i" 1000000 the process stop when I close the window but if I put the value of "i" >1000000 then the problem occurs. any suggestions?
Re: qt window close but process still running
Can you post a compilable code...
1. I am still guessing QTMainForm is QWidget/QMainWindow ?
2. How and where you are creating QTMainForm ?
3. Did to follow suggestion by d_stranz above
Re: qt window close but process still running
yes. you are right. this is my full code
class QTMainForm : public QWidget
{
Q_OBJECT
public:
QTMainForm(QWidget* parent=0);
~QTMainForm();
private slots:
void OnBtnstrt();
// void closer();
// DWORD WINAPI SocketHandler(void*);
private:
QProgressBar progress;
QPushButton btn;
//QVBoxLayout vboxMainLayout;
QVBoxLayout vb;
};
QTMainForm::QTMainForm(QWidget* parent)
{
btn.setText("Start");
connect(&btn, SIGNAL(clicked()), this, SLOT(OnBtnstrt()));
progress.setValue(0);
progress.setRange(0, 10000000);
vb.addWidget(&progress);
vb.addWidget(&btn);
setLayout(&vb);
}
void QTMainForm::OnBtnstrt()
{
double i=0;
while(i<=10000000)
{
i++;
qApp->processEvents();
progress.setValue(i);
}
}
QTMainForm::~QTMainForm()
{
}
int main(int argc, char *argv[])
{
QTMainForm* pMainForm = 0;
QApplication app(argc, argv);
pMainForm = new QTMainForm();
pMainForm->resize(300, 300);
pMainForm->show();
return app.exec();
}
Re: qt window close but process still running
Not sure what is your long term goal, whatever it be using such a for loop(> 10000000) in button click (or any other slot) will land in to various issues.\
For now to solve the problem, just modify the button click slot
Code:
void QTMainForm::OnBtnstrt()
{
double i=0;
while(i<=10000000)
{
i++;
qApp->processEvents();
progress.setValue(i);
if(!isVisible()) // <<<<<<<<<<<<<<<<<<<<<
break;
}
}
One more observation in the main, pMainForm is not being deleted, this is a leak in memory (it will not be a concern now, as the app exits anyway). As a good practice (in Qt) always pass the parent handle to the base classs QWidget/QObject
Code:
QTMainForm
::QTMainForm(QWidget* parent
) : QWidget(parent
) // <<<<<<<<<<<<<<<<<<<<<<<<<<< {
...
}
Re: qt window close but process still running
it works great. thanks a lot.;)