Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qtablewidget display



  • 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)
    

  • Lifetime Qt Champion

    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.



  • @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



  • @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.



  • @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


  • Lifetime Qt Champion

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



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


Log in to reply