Hello Jerome

While I waited for your code, I just implemented a hello world application with classical ModelViewController structure that does what you want. Have a look at the NewPersonDialog class to see how it is implemented...

I will have a look at your code now...

Have a nice evening!
Aaron


##################################import sys
from PySide.QtGui import *
from PySide.QtCore import *


class Person(object):
    '''Model for a person'''

    def __init__(self, name, age):
        self.name = name
        self.age = age


class PersonTableModel(QStandardItemModel):
    '''TableModel for multiple persons.'''

    def __init__(self, persons, parent = None):
        super(PersonTableModel, self).__init__(parent)
        self.persons = persons
        for i in xrange(len(self.persons)):
            person = self.persons[i]
            self.setItem(i, 0, QStandardItem(person.name))
            self.setItem(i, 1, QStandardItem(str(person.age)))

        self.setHeaderData(0, Qt.Horizontal, "Name")
        self.setHeaderData(1, Qt.Horizontal, "Age")

    def add_person(self, person):
        self.persons.append(person)
self.appendRow([QStandardItem(person.name), QStandardItem(str(person.age))])


class PersonTableView(QTableView):

    def __init__(self, model, parent = None):
        super(PersonTableView, self).__init__(parent)
        self.setModel(model)


class MainWindow(QWidget):
    '''UI definition of the main window.'''

    def __init__(self, model):
        super(MainWindow, self).__init__()
        self.model = model
        self.setupUi()

    def setupUi(self):
        '''Initialize the GUI with different types of clickable labels'''

        self.layoutGeneral = QVBoxLayout(self)
        self.table_view = PersonTableView(self.model, self)
        self.pb_add = QPushButton("Add person")
        self.pb_delete = QPushButton("Delete person")

        self.layoutGeneral.addWidget(self.table_view)
        self.layoutGeneral.addWidget(self.pb_add)
        self.layoutGeneral.addWidget(self.pb_delete)


class NewPersonDialog(QDialog):
    '''Dialog to show a form for a new person.'''

    def __init__(self):
        super(NewPersonDialog, self).__init__()
        self.setupUi()
        self.name = None
        self.age = None
        self.connectSignals()

    def setupUi(self):
        self.grid = QGridLayout(self)
        self.lbName = QLabel('Name:', self)
        self.lbAge = QLabel('Age:', self)
        self.leName = QLineEdit(self)
        self.leAge = QLineEdit(self)
        self.pbCancel = QPushButton("Cancel", self)
        self.pbOk = QPushButton("OK", self)
        self.pbOk.setDefault(True)

        self.grid.addWidget(self.lbName, 0, 0)
        self.grid.addWidget(self.lbAge, 1, 0)
        self.grid.addWidget(self.leName, 0, 1)
        self.grid.addWidget(self.leAge, 1, 1)
        self.grid.addWidget(self.pbCancel, 2, 0)
        self.grid.addWidget(self.pbOk, 2, 1)

    def connectSignals(self):
        self.pbOk.clicked.connect(self.on_ok)
        self.pbCancel.clicked.connect(self.reject)

    def on_ok(self):
        self.name = self.leName.text()
        self.age = int(self.leAge.text())
        self.accept()


class Controller(QObject):
    '''Controls interaction between model and view'''

    def __init__(self):
        super(Controller, self).__init__()
self.model = PersonTableModel([Person("Guido", 42), Person("Bill", 55)])
        self.view = MainWindow(self.model)
        self.connectSignals()

    def start(self):
        '''Start application'''
        self.view.show()

    def connectSignals(self):
        '''Connect the buttons with the corresponding slot'''
        self.view.pb_add.clicked.connect(self.on_add_person)
        self.view.pb_delete.clicked.connect(self.on_delete_person)

    def on_add_person(self):
'''A label was clicked, show the text of the label in the display'''
        dialog = NewPersonDialog()
        if dialog.exec_():
            self.model.add_person(Person(dialog.name, dialog.age))

    def on_delete_person(self):
        '''To be implemented.'''
        pass



def main():
    app = QApplication(sys.argv)
    controller = Controller()
    controller.start()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

###################################################






Am 28.02.2014 18:47, schrieb Jérôme:
Le 2014-02-28 18:37, Aaron Richiger a écrit :
Hello Jerome!
Hi.
Welcome in the PySide world...
Thank you !

Would it be possible, to send us the code
(source files). It's always great to run the code, a UI or a error
message often says more than many words...
I deliberately stripped the relevant parts in my former message, as the
rest might be confusing. Here are the files I'm running. This is just a
test, so don't be surprised to see stupid or hardcoded stuff.



_______________________________________________
PySide mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/pyside

_______________________________________________
PySide mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/pyside

Reply via email to