PDA

View Full Version : qt window close but process still running



sazzad08
31st December 2012, 07:15
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.

Santosh Reddy
31st December 2012, 07:59
this is a big number 10000000 for loop are you sure of this

sazzad08
31st December 2012, 08:48
yes. it does. but the problem is when I close the gui window, the process still runs. any help on that?

Santosh Reddy
31st December 2012, 08:52
void QTMainForm::OnBtnstrt()
{

pm = new QTMainForm();
btn.isHidden();
You are creating a new QTMainForm, what is this for. (I mean you already have one)

sazzad08
31st December 2012, 09:03
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.

boudie
31st December 2012, 09:28
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.

Santosh Reddy
31st December 2012, 09:32
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)

d_stranz
1st January 2013, 19:00
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:



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.

sazzad08
2nd January 2013, 04:39
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?

Santosh Reddy
2nd January 2013, 05:09
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

sazzad08
2nd January 2013, 07:30
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();
}

Santosh Reddy
2nd January 2013, 07:56
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

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


QTMainForm::QTMainForm(QWidget* parent)
: QWidget(parent) // <<<<<<<<<<<<<<<<<<<<<<<<<<<
{
...
}

sazzad08
2nd January 2013, 08:35
it works great. thanks a lot.;)