(seemingly) simple Qt app: creating a progress bar

Page  
5

February 29, 2012

mzimmers mzimmers
Lab Rat
529 posts

OK, thanks, mlong. Any tips on putting other breakpoints or qDebug telltales anywhere for more information on what’s going on?

BTW, the one remaining line of code in main.cpp is a connect() that I commented out:

  1. // app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));

The presence or absence of this call isn’t relevant to the current problem, is it?

February 29, 2012

Volker Volker
Ant Farmer
5428 posts

Can you put your current code into a ZIP for downloading? Just replace the actual workload in the run method with some dummy loop. It’s hard to say what’s going on without seing the whole picture.

February 29, 2012

mzimmers mzimmers
Lab Rat
529 posts

Sure, I can do that. Where would you like me to put it? Or, I can email it to you.

Thanks, Volker.

EDIT: Just remembered this site. Here’s the zip:

http://www.mediafire.com/download.php?qp6lelkebbni20i

Thanks again.

March 1, 2012

Volker Volker
Ant Farmer
5428 posts

Ok, I made it run as expected. First, I had a mistake in my outline of the onQuitButtonClicked() method. The call to close() wasn’t conditional, it was always called, that’s why your app close, even if you clicked on “no”.

To make it run:

add a signal workDone() to WorkerThread:

  1. signals:
  2.     void percentChanged(int percent);
  3.     void workDone();

emit the signal at the end of the run method:

  1. void WorkerThread::run()
  2. {
  3.     qDebug() << "WorkerThread::run(): STARTED";
  4.    
  5.     // your code goes here.
  6.  
  7.     myFile.close();
  8.     qDebug() << "WorkerThread::run(): finished";
  9.     emit workDone();
  10. }

in the MainWindow constructor connect the signal:

  1. MainWindow::MainWindow(QWidget *parent) :
  2.  QMainWindow(parent)
  3. {
  4.     // ...
  5.  
  6.     connect(m_worker,
  7.             SIGNAL(workDone()),
  8.             m_thread,
  9.             SLOT(quit()));
  10.     // ...
  11. }

modify MainWindow::onQuitButtonClicked() to this version

  1. void MainWindow::onQuitButtonClicked()
  2. {
  3.     QMessageBox::StandardButton  answer = QMessageBox::question(
  4.                 this,
  5.                 tr("Quit applicaton?"),
  6.                 tr("Do you really want to quit the application?"),
  7.                 QMessageBox::Yes | QMessageBox::No,
  8.                 QMessageBox::No
  9.                 );
  10.  
  11.     if (answer != QMessageBox::No)
  12.     {
  13.         if(m_thread->isRunning()) {
  14.             qDebug() << "thread is running - trying to terminate it";
  15.             emit requestWorkerToFinish(); // no direct call!
  16.             bool finishOk = m_thread->wait(10000);
  17.             if (!finishOk)
  18.             {
  19.                 qDebug() << "WARNING: request to finish thread timed out!";
  20.                 m_thread->terminate();
  21.                 m_thread->wait();
  22.             }
  23.         } else {
  24.             qDebug() << "Thread not running";
  25.         }
  26.         close();
  27.     }
  28. }

Some additional hints:

At the end of a method that has no (void) return type, you do not need to call return explicitly.

All the QObjects you create using new() should have a parent:

  1. MainWindow::MainWindow(QWidget *parent) :
  2.     QMainWindow(parent)
  3. {
  4.     m_bar = new QProgressBar (this);
  5.     m_button = new QPushButton (this);
  6.     m_worker = new WorkerThread (this);
  7.     m_thread = new QThread (this);
  8.    
  9.     setupWidgets();
  10.     // ...
  11. }

If they do have a parent, you do not need to delete them manually, so remove the deletes from the destructor:

  1. MainWindow::~MainWindow()
  2. {
  3. }

And to have a nice layout, you should put everything into a layout or use a Qt Designer based form.

March 1, 2012

mzimmers mzimmers
Lab Rat
529 posts

Hmm…it’s not working as I’d expect it to. Here’s the app output when I push the “yes” button:

WorkerThread::run(): STARTED
thread is running – trying to terminate it
WorkerThread::run(): finished
reached end of file.
WARNING: request to finish thread timed out!
The program has unexpectedly finished.

Based on my telltales in run(), it appears that the slot requestFinish() isn’t called until run() is finished. I added a telltale to requestFinish(), and here’s the output I got:

WorkerThread::run(): STARTED
thread is running – trying to terminate it
reached end of file.
WorkerThread::requestFinish() called.
WorkerThread::run(): finished
WARNING: request to finish thread timed out!
The program has unexpectedly finished.

Another issue: this line in mainwindow.cpp doesn’t compile:

  1.  m_worker = new WorkerThread (this);

Perhaps this is because my c’tor didn’t have an argument, so I modified its declaration to be this:

  1.  WorkerThread(QObject * parent = 0);

Now it compiles (though I get a compiler warning that parent is never used). Is this OK?

March 2, 2012

Volker Volker
Ant Farmer
5428 posts

Nope, you must also change the constructor implementation:

  1. WorkerThread::WorkerThread(QOjbect parent)
  2.     : QObject(parent)
  3. {
  4.     // ...
  5. }

It is always a good idea to always add the parent parameter to any QObject subclass. If you use the new class wizard in Qt Creator, it’s even done automatically, if you state that it is a QObject subclass :-)

Regarding the finish: I need to prepare a test case, my files were far too small to add reasonable workload to the worker thread. It was always finished before I had the chance to click the button :)

March 3, 2012

mzimmers mzimmers
Lab Rat
529 posts

Hmm, I now get a run-time warning/error:

QObject::moveToThread: Cannot move objects with a parent

The documentation on QObject confirms this.

Also:

And to have a nice layout, you should put everything into a layout or use a Qt Designer based form.

…meaning, that I don’t use Designer to create the UI, but I somehow move my existing stuff into Designer context? If you could point me at a page that talks more about this, I’ll get started on it.

Thanks.

March 4, 2012

Volker Volker
Ant Farmer
5428 posts

Ah, I’m the cause of the error. Just replace this one

  1. m_worker = new WorkerThread (this);

with this one:

  1. // The WorkerThread has no parent
  2. m_worker = new WorkerThread;

In this case you must delete the worker in the destructor to avoid a memory leak:

  1. MainWindow::~MainWindow()
  2. {
  3.     delete m_worker;
  4. }

For the layout: You can either use Qt Designer or just put everything into a layout manally, the QLayout docs and subclasses have examples.

March 5, 2012

mzimmers mzimmers
Lab Rat
529 posts

OK, fixed that…but I’m still having the same problem as described above: I hit “yes” and…it continues on its way. Only after the workerThread gets to the end of the file, is requestFinish() called.

Maybe I’m not understanding how this should work — once the requestWorkerToFinish signal is sent, the corresponding slot should execute immediately, right? So, my worker loop should terminate very quickly. That’s what I was expecting, anyway.

March 5, 2012

Volker Volker
Ant Farmer
5428 posts

Sorry, I didn’t have the time yet to prepare a test case. It’ll last some more time…

March 5, 2012

mzimmers mzimmers
Lab Rat
529 posts

No sweat, Volker…I appreciate your help. Would you like a fresh copy of the source code? Mine should be about the same as yours, but…you never know.

Page  
5

  ‹‹ Show/hide top-level submenu at runtime      [SOLVED] How to get readyRead signal for tcp socket on different thread SLOT ››

You must log in to post a reply. Not a member yet? Register here!