[Qt-interest] Simple dialog question

Paul Colby qt at colby.id.au
Wed Nov 4 08:59:19 CET 2009


QDialog::exec runs the dialog in a modal fashion... thus, it halts the
calling function until the dialog is closed / dismissed.

So for example, in your code...

> void Configuration::showDialog()
> {
>  Configuration configDlg;
>  const int result=configDlg.exec();
/* At this point, the dialog is shown to the user.
   Processing does not continue within this function
   until the uses clicks "ok" or close etc.  Once the
   user clicks "ok" etc "result" will contain a code indicating
   whether the user clicked "ok" or "cancel".
*/
>  label->setText("Text"); // At this point, the dialog has already been dismissed, so changing the text does not have much affect.
> }

If you do indeed want the dialog to be model, then simply set the text
*before* you execute the dialog, eg:

> void Configuration::showDialog()
> {
>  Configuration configDlg;
>  label->setText("Text"); // First set the text.
>  configDlg.exec(); // Then show the dialog.
> }

On the other hand, if you don't want the dialog to be modal, then use
QDialog::open instead of QDialog::exec, eg:

> void Configuration::showDialog()
> {
>  Configuration configDlg;
>  configDlg.open(); // Open the dialog non-modally.
>  label->setText("Text");
> }

However, even in that case, it usually makes sense to set the text
before the dialog is shown (though it this simple case, it wouldn't
really make any difference).

Compare:
* http://doc.trolltech.com/4.5/qdialog.html#exec
* http://doc.trolltech.com/4.5/qdialog.html#open

pc.

On Wed, Nov 4, 2009 at 5:31 PM, Phil <phillor at telstra.com> wrote:
> Thank you for reading this.
>
> This is another one of those long-standing problems that I have been unable
> to solve.
>
> As an example, say a menu item on the main window is connected to a slot
> which in turn calls a function in my configuration class to display a
> configuration dialog. The dialog has been created with Designer.
>
> void Configuration::showDialog()
> {
>  Configuration configDlg;
>  configDlg.exec();
>
>  label->setText("Text");
> }
>
> The above code displays the dialog but the label remains unchanged.
>
> The only way that I have been able to access or set items on a dialog is to
> put the code in the Configuration class constructor. This is not possible in my
> current application because the constructor is called before the item to be
> displayed is available.
>
> I've studied the Qt examples but, again, I cannot relate the example code to
> what I'm trying to achieve.
>
> Can anyone point me in the right direction?
>
> --
> Regards,
> Phil
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>



-- 
http://colby.id.au




More information about the Qt-interest-old mailing list