PyQt & Python : QSqlRelationalTableModel display more than one column



  • Hello everyone;
    I'm using two tables in an app and table 1 contains a foreign key referencing table 2, my aim is to be able to display two columns of table 2 using QSqlRelationalTableModel. Here is what I've done with displaying one column:

    @
    self.model = QSqlRelationalTableModel(self)
    self.model.setTable("RISTOURNE")
    self.model.setRelation(PERSONNEID,
    QSqlRelation("PERSONNE", "idPersonne", "nom"))
    self.model.setSort(NOM, Qt.AscendingOrder)
    self.model.select()
    @

    To display this kind of data in a GUI, we need to use the model with a widget, so is it possible to set a model for a QLineEdit? I usualy do this with a QCombobox.

    Thanks.


  • Lifetime Qt Champion

    Hi,

    You can do that using a QDataWidgetMapper but QLineEdit is not meant to show several lines. Since you want to show two columns, why note use a QTableView ?



  • thanks for the reply, I said I want to know if it is possible to obtain two columns using QSqlRelationalTableModel? In my example, I'm obtaining just one column. Thanks for the hint about QDataWidgetMapper, it has been helpful, now I want to use the QSqlRelationalTableModel to obtain two columns of my second table and then display them in two different QLineEdits, a hint?


  • Lifetime Qt Champion

    It depends on the content of your database. Do your tables contain more than a column ?

    If you want to show all the rows from your tables in one widget then QLineEdit is not the way to go.

    Can you describe more in details what you want to show and how you want to show it ?



  • ok, suppose I have a table
    @
    CREATE TABLE PERSON(
    idPers INT PRIMARY KEY NOT NULL,
    name VARCHAR NOT NULL,
    familyName VARCHAR NOT NULL,
    borned DATE NOT NULL,
    idGoods INT NOT NULL,
    CONSTRAINT PERSON_GOODS_FK FOREIGN KEY (idGoods) REFERENCES GOODS(idGoods)
    @

    And this one
    @
    CREATE TABLE GOODS (
    idGoods INT PRIMARY KEY NOT NULL,
    designation VARCHAR NOT NULL,
    quantity INT NOT NULL
    @

    Now I want to display "name", "familyName", "designation", "quantity" in four differents QLineEdits. I hope this is helpful for your understanding.


  • Lifetime Qt Champion

    Then QDataWidgetMapper is your friend


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.