0

I have 2 classes: one maintains some loop (at leas for 2-3 minutes; and is inherited from QObject) and another shows up a progress dialog (inherited from QDialog). I want to start the loop as soon as the dialog is shown. My first solution was:

 int DialogClass::exec()
 {
     QTimer::singleShot(0, LoopClassPointer, SLOT(start()));
     return __super::exec();
 }

There is a problem with throwing exceptions from slots. so I considered a possibility to make public slot start() just a public function. But now I don't know how to make it works well. Things like this:

int DialogClass::exec()
{
     LoopClassPointer->start();
     QApplication::processEvents();
     return __super::exec();
}

don't help. The dialog doesn't appears. Is there a common approach to this kind of situations?

some details, according to questions:

  1. I have to work with system with its own styles, so we have a common approach in creating any dialogs: to inherit them from stytle class, which is inherited from QDialog.

  2. my 'LoopClassPointer' is an exported class from separate dll (there is no UI support in it).

  3. I have a 'start' button in main app, which connected with a slot, which creates progress dialog and 'LoopClassPointer'. at the moment I send 'LoopClassPointer' instance in the dialog and don't whant to make significant changes in the architecture.

Illia Levandovskyi
  • 1,228
  • 1
  • 11
  • 20
  • What does __super mean? Are you trying to access object created and reserved by compiler? – Valentin H Dec 16 '13 at 08:50
  • Shouldn't it be other way around? The Progress dialog should be displayed as soon as the process starts? – Valentin H Dec 16 '13 at 08:54
  • Are these objects all running in the same thread? In your code it is not clear what you are trying to do. The way to communicate between different objects is buy connecting a signal to a slot. So you would connect some signal of obj1 to a slot in obj2 and emit the signal from obj1 when it starts... is that what you are trying to do? – code_fodder Dec 16 '13 at 08:54

2 Answers2

0

Take a look at QtDemo->Concurrent Programming->Run function

e.g. in Qt 4.8: http://qt-project.org/doc/qt-4.8/qtconcurrent-runfunction.html

Valentin H
  • 7,240
  • 12
  • 61
  • 111
0

In this situation, I recommend you separate the logic of the loop from the dialog. Gui elements should always be kept separate.

It's great that your worker class is derived from QObject because that means you can start it running on a separate thread: -

QThread* m_pWorkerThread = new QThread;
Worker* m_pWorkerObject  = new Worker; // assuming this object runs the loop mentioned

// Qt 5 connect syntax
connect(m_pWorkerThread, &QThread::started, m_pWorkerObject, &WorkerObject::start);
connect(m_pWorkerThread, &QThread::finished, m_pWorkerThread, &QThread::deleteThis);

m_pWorkerObject->moveToThread(m_pWorkerThread);
m_pWorkerThread->start();

If you're not familiar with using QThread, then start by reading this.

The only other thing you require is to periodically send signals from your worker object with progress of its work and connect that to a slot in the dialog, which updates its display of the progress.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85