[PySide] Form dialog subclassing QDialog

Matthew Woehlke mw_triad at users.sourceforge.net
Fri Feb 28 19:03:14 CET 2014


On 2014-02-28 12:32, Jérôme wrote:
> What I want is to get the form values from the dialog and use them in
> the add_item() function in the caller, like this: [snip]
>
> I could let the caller read the values in an attribute of the dialog (or
> through a getter) before the dialog gets closed/destroyed, but I don't
> know how to do that.

That's the usual way in Qt/C++. Probably something like:

   # in your dialog class
   def value(self):
     return self._ui.widget.value()

...and at the call site:

   dlg = Dialog()
   if (dlg.exec_() == QDialog.Accepted)
     value = dlg.value()
   else:
     ...canceled...

> It looks like a nicer way would be to reproduce the behaviour of the
> QColorDialog, for instance.
>
> The caller only does
>
> color = QtGui.QColorDialog.getColor()

This is still implemented in terms of the above.

To do something like this, you could add a class method to your dialog 
class, e.g.:

   @classmethod
   def getValue(cls):
     dlg = cls()
     if dlg.exec_() == QDialog.Accepted:
       return dlg.value()
     # implicit return None if canceled

(As a bonus, because it is Python, you can return None on cancel rather 
than an invalid value.)

-- 
Matthew




More information about the PySide mailing list