Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Qtablewidget display
Qt 6.11 is out! See what's new in the release blog

Qtablewidget display

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 3 Posters 1.2k 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.
  • Y Offline
    Y Offline
    YassineKira
    wrote on last edited by YassineKira
    #1

    Hello,

    I have this small problem . I want do some changes in my QTablewidget, in the first it show like in the photo Capture.PNG
    but when i do a query to have some data from my database would change, i know the reason why it because of i limite the column in 5, i don't know how to fix it,
    Can someone help me modify my code to create qtablewidger without a limite of #5 columns

    cursor=db.cursor()
                row = int(QObject.sender(self).objectName())
                id = self.ui.tableWidget_2.item(row, 0).text()
                print(id)
                #y=(str(id))
                #result = self.c.execute("SELECT * from prix2 WHERE ID_pu="+str(y))
                result= cursor.execute("SELECT PU.id_pu, PU.ref_pu, PU.desig_pu, PU.unite, PU.prix FROM PU WHERE id_pu ="+str(y))
                #result= cursor.execute(command,y)
                #self.conn.commit()                    
                x = result.fetchone()
                print(x)
                numcols = len(x)
                self.ui.tableWidget_3.setColumnCount(numcols)
                self.ui.tableWidget_3.insertRow(0)
                self.ui.tableWidget_3.insertColumn(5)
                rowPosition = self.ui.tableWidget_3.rowCount()
                print(rowPosition)
                #for row_ in range(rowPosition):
                for column in range(numcols):
                    self.ui.tableWidget_3.setItem(0, column, QTableWidgetItem((str(x[column]))))
                    self.ui.tableWidget_3.setSelectionBehavior(QAbstractItemView.SelectRows)
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi,

      Your select query does work and it contains references to 5 fields thus you will create that many headers for your model.

      The same is going to happen the other way around.

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

      Y 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        Your select query does work and it contains references to 5 fields thus you will create that many headers for your model.

        The same is going to happen the other way around.

        Y Offline
        Y Offline
        YassineKira
        wrote on last edited by YassineKira
        #3

        @SGaist can I display a query with its rows in QtableWidget without limiting the number of columns to fill.Sorry i start programming in PyQt

        JonBJ 1 Reply Last reply
        0
        • Y YassineKira

          @SGaist can I display a query with its rows in QtableWidget without limiting the number of columns to fill.Sorry i start programming in PyQt

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @YassineKira
          Hard to understand what you actually want. In your example you choose to go self.ui.tableWidget_3.setColumnCount(numcols), you could go self.ui.tableWidget_3.setColumnCount(numcols + 3) to allow for the 3 computed columns you're going to calculate.

          Y 1 Reply Last reply
          0
          • JonBJ JonB

            @YassineKira
            Hard to understand what you actually want. In your example you choose to go self.ui.tableWidget_3.setColumnCount(numcols), you could go self.ui.tableWidget_3.setColumnCount(numcols + 3) to allow for the 3 computed columns you're going to calculate.

            Y Offline
            Y Offline
            YassineKira
            wrote on last edited by YassineKira
            #5

            @JonB Thanks for your respond but ita aint working.

            I can explain it again, i have a table that i edit in QDesigner, the columns to have some headers when i display the query the headers modify themselves because i limite the loop for.
            Now, i want find a way to display my query without a limite ! in the loop so they would change the headers the 3 leasts columns

            i try this

                        result= cursor.execute("SELECT PU.id_pu, PU.ref_pu, PU.desig_pu, PU.unite, PU.prix FROM PU WHERE id_pu ="+str(y))              
                        x = result.fetchone()
                        numcols = len(x)
                        self.ui.tableWidget_3.insertRow(0)
                        for column, data in enumerate(x):
                                self.ui.tableWidget_3.setItem(0, column, QTableWidgetItem(str(x)))
            

            with this code the table doesnt change but, the data of the query is in each column someone can help me to fixe it the fill in each culumn a item

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

              The next question is going to be: why not use Qt's SQL module with a QSqlQueryModel ?

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

              Y 1 Reply Last reply
              2
              • SGaistS SGaist

                The next question is going to be: why not use Qt's SQL module with a QSqlQueryModel ?

                Y Offline
                Y Offline
                YassineKira
                wrote on last edited by YassineKira
                #7

                @SGaist i start doing with sqlite3, so i continue with it

                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