[PySide] Form dialog subclassing QDialog

Jérôme jerome at jolimont.fr
Fri Feb 28 18:32:27 CET 2014


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 ?

- If yes, do you have a hint about how to do that ?

- If no, then what would you suggest ?

- 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 ?

Thank you very much for any pointer.

Have a nice week-end.

-- 
Jérôme




More information about the PySide mailing list