Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PyQt & Python : QSqlRelationalTableModel display more than one column
Forum Updated to NodeBB v4.3 + New Features

PyQt & Python : QSqlRelationalTableModel display more than one column

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kekule
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kekule
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kekule
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Then QDataWidgetMapper is your friend

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved