yes. it does. but the problem is when I close the gui window, the process still runs. any help on that?
yes. it does. but the problem is when I close the gui window, the process still runs. any help on that?
You are creating a new QTMainForm, what is this for. (I mean you already have one)void QTMainForm::OnBtnstrt()
{
pm = new QTMainForm();
btn.isHidden();
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
sazzad08 (31st December 2012)
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.
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.
Are you able to see progress bar?Qt 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(); // <<<<<<<<<<<<<<<<<< } }To copy to clipboard, switch view to plain text mode
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)
Last edited by Santosh Reddy; 31st December 2012 at 08:37.
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
sazzad08 (31st December 2012)
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:
Qt Code:
{ 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); }To copy to clipboard, switch view to plain text mode
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.
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?
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
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
sazzad08 (2nd January 2013)
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();
}
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
Qt Code:
void QTMainForm::OnBtnstrt() { double i=0; while(i<=10000000) { i++; qApp->processEvents(); progress.setValue(i); if(!isVisible()) // <<<<<<<<<<<<<<<<<<<<< break; } }To copy to clipboard, switch view to plain text mode
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
Qt Code:
{ ... }To copy to clipboard, switch view to plain text mode
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
sazzad08 (2nd January 2013)
it works great. thanks a lot.![]()
Bookmarks