[PySide] Form dialog subclassing QDialog

Aaron Richiger a.richi at bluewin.ch
Fri Feb 28 20:20:02 CET 2014


Hello Jerome

Find the answers to your questions below:

Am 28.02.2014 18:32, schrieb Jérôme:
> Hi everyone.
>
> I know Python and have been playing a bit with PyGTK. Now, I am
> discovering Qt and PySide and I'm having a hard time trying to implement
> a form dialog.
>
> I have a main window displaying a list of items with a few attributes.
> Aside from the TableView, I added three buttons : Add, Edit, Remove.
> When clicking Add, I want a new window to popup, containing a form to
> enter the values of the new item. Basically, I want the dialog to have
> an OK and a Cancel button. When the user clicks OK, the dialog closes,
> and in the main window, the new item is added to the list.
>
> (I realize this may be a subcase of Edit with default values, but I'm
> beginning simple with Add, and I'll figure out Edit afterwards.)
>
> I'm therefore creating a dialog subclassing QDialog. Both main window
> and dialog window are designed with QtDesigner.
>
> I need a design hint and the examples I found didn't help.
>
> Here's my dialog class (the items happen to be buildings in this case,
> hence the name), with only an __init__() function:
>
> class buildingDialog(QtGui.QDialog):
>
>      def __init__(self, parent=None):
>          super(buildingDialog, self).__init__(parent)
>          self.ui = dialogBuilding.Ui_Dialog()
>          self.ui.setupUi(self)
>
> And here is the call:
>
>      def addPushButtonClicked(self):
>
>          dialog = bui.buildingDialog(self)
>          print(dialog.exec_())
>
> The print is here for debug purposes and prints 0 or 1 depending on
> which button was pressed.
>
> 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:
>
> if the return value is made of item attributes (i.e. the user pressed
> OK)
>      add_item(attributes = return value)
> else (the user pressed cancel)
>      nothing...
>
> I could modify the return value of the dialog, to return a list of item
> attributes. But I suppose it's better to keep 0 and 1 as return values
> and return the values another way. Besides I don't know how to do this
> anyway.
>
> I could return a couple like [values, returnvalue], with returnvalue
> being 0 or 1, but again, the return values would not be 0 or 1 anymore.
>
> 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.
>
> 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()
>
> if color.isValid():
>      do stuff
>
> But I don't know how to handle this from my custom dialog.
>
> I'm pretty sure this is a very common design problem.
>
> My questions are
>
> - Am I right about trying to copy QColorDialog ?
I would say no. You can try to implement the usage comfort (the exampe 
you gave above), but copying QColorDialog would mean subclass and remove 
all the color stuff, add your input widgets, etc, which is much more 
work than just subclass QDialog and implement your dialog there...
>
> - If yes, do you have a hint about how to do that ?
>
> - If no, then what would you suggest ?
Implement your own Dialog class which is a subclass of QDialog. If you 
want to work with QtDesigner, you could also subclass the autogenerated 
dialog class to add some additional sugar to it (e.g. usage comfort like 
in ColorDialog). You could do this in your class buildingDialog. 
Alternatively if you don't like sugar, this line shows how to access 
some inputs after your dialog.exec_()

print dialog.ui.nameLineEdit.text()


>
> - The program I'm working on is an interface to enter data about a set
> of buildings. This mechanism will be reproduced all along: a projet has
> buildings with attributes (size, age,...), buildings have boiler rooms
> with attributes (name, manager,...), boiler rooms have boilers with
> attributes (age, efficiency,...), etc. Do you think I'm going in the
> wrong direction ? Is subclassing QDialog a bad idea in the first place ?
>
> - Most PySide examples I found were about using common dialogs. Should I
> seek C++ examples and try to adapt them ?
This is always a good possibility if you really can't find python 
examples, because the Qt-C++ community is larger and therefore, there 
are more examples in C++ flying around... Translating C++ is usually 
quite easy, but takes some time...
>
> Thank you very much for any pointer.
>
> Have a nice week-end.
>
Hope this helps.
Aaron



More information about the PySide mailing list