PDA

View Full Version : How to postpone a signal until present form is closed ?



marcvanriet
10th December 2010, 13:25
Hi,

I have a modal dialog started by a main window. When I press a certain button on this modal dialog, I want the dialog to close. After that, the main window should open a second modal dialog.

I tried to first close() the first modal dialog in the event handler of the button in that dialog, and send a signal to the mainwindow.

void CDekInfo::on_pbScan_clicked()
{
close();
emit startSecondDialog( "123456789" );
}

But of course the signal is handled immediately by the mainwindow and the second dialog is opened immediately, which is no good because the code after the exec() of my first modal dialog has to be executed before the second one is opened.

Is there a way to 'post' a signal so that it will be executed only after my modal form has finished closing ? Sort of like when my main window enters its event loop again ?

Regards,
Marc

SixDegrees
10th December 2010, 13:33
Why don't you simply examine the return value from the first dialog back in the mainwindow code, and use it to determine whether to launch the second dialog?

FelixB
10th December 2010, 13:58
I don't get the connection between both dialogs... if you want to close the first after user pressed on a button: connect thi button with "close"-slot. When you want to open a second dialog - open it after exec() has returned.


Dialog dlg;
dlg.exec();
Dialog dlg2;
dlg2.exec();

marcvanriet
10th December 2010, 14:23
Hi,

@SixDegrees : thought of that too. But the dialog may be started in different places in my application. And then I would have to duplicated code.

@FelixB : well, the situation is more complicated. The order in which dialogs are shown is not fixed. In fact, there are 3 or 4 dialogs and you may switch between the different dialogs at will.

Guess I'll have to create a 'dialog starter' routine that checks return conditions when a dialog is closed. Too bad, I thought there would be a cleaner approach using signals and slots.

Or maybe a single shot timer in my mainwindow and a flag that is set by a slot when a dialog has to be opened. Although maybe the timer may fire too soon, even before the dialog is closed, so I doubt that this is fool-proof.

Further suggestions are appreciated,
Best regards,
Marc

FelixB
10th December 2010, 14:30
what about QDialog::finished( int ) (http://doc.trolltech.com/4.7/qdialog.html#finished)-signal?

marcvanriet
13th December 2010, 13:06
Hi FelixB,

Tried that now. Unfortunately, it gives the same result. I send the signal to open the second dialog in the finished() slot of the first dialog. However, at that time the exec() function for the first dialog hasn't returned yet.

I think I'll try some timer solution now. QDialog doesn't seem to have a signal that fires when my main dialog gets the focus. There is the focusInEvent, but I'm not sure if it occurs for the dialog itself, or for the widget in the dialog that gets the focus.

Strange that there is no event or way to know that Qt has finished processing its signals. Maybe a signal could be sent when no more signals are pending ;)

Best regards,
Marc

marcvanriet
15th December 2010, 13:22
Hi,

In case anybody cares : got it to work in a clean way now.

I have a slot in my mainwindow. It has a parameter that says which dialog box to show. Any dialog can send a signal to this slot for showing another dialog.

The mainwindow slot closes any dialog boxes that are still open, and remembers the dialog box to show. It then starts a timer (with QTimer::singleShot(0, this, SLOT(slot2)) to start a second slot.

This second slot is executed after all windows are closed and any remaining signals are processed. It then finally shows the dialog box that was requested.

Regards,
Marc