Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make columns and rows dynamically in PyQt?

I am making a GUI desktop application using python3, PyQt5 in windows 7.

What I'm trying to do is:

  1. when my application runs, it makes an empty table using QTableWidget. There're also four buttons.

  2. When a user clicks the first button, the empty table gets 2 rows and 4 columns.

  3. In this situation, if the user clicks another button, the previous columns and rows are removed, and the table gets 10 columns and 20 rows.

I made a empty table, but I don't know how to make columns and rows dynamically.

Here is my code:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QTableWidget, QPushButton

class mainClass(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.tableWidget = tableManager()
        self.returnedTableWidget = self.tableWidget.makeTable(self)

        btnMaker = buttonManager()
        btnMaker.makeTestBtn(self)

        self.setGeometry(100, 100, 700, 600)
        self.show()


class buttonManager(QWidget):
    def __init__(self):
        super().__init__()

    def makeTestBtn(self, parent):
        testBtn01 = QPushButton("2 X 4", parent)
        testBtn02 = QPushButton("4 X 8", parent)
        testBtn03 = QPushButton("8 X 16", parent)
        testBtn04 = QPushButton("16 X 32", parent)

        testBtn01.move(50, 450)
        testBtn02.move(200, 450)
        testBtn03.move(350, 450)
        testBtn04.move(500, 450)


class tableManager(QWidget):
    def __init__(self):
        super().__init__()

    def makeTable(self, parent):
        self.tableMaker = QTableWidget(parent)
        self.tableMaker.setGeometry(50, 50, 600, 400)

        return self.tableMaker


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mc = mainClass()
    sys.exit(app.exec_())


I know there are setRowCount() and setColumnCount() methods in QTableWidget class. But I don't know how and where to use those methods in my code.

like image 446
passion053 Avatar asked Oct 21 '25 04:10

passion053


1 Answers

to create rows and cols:
1 -fetch all your data from db
2 -use for statement as follows :

all_data = db.fetch
tbl = QtGui.QTableWidget(len(all_data),X) # X is The number of columns that you need  
header_labels = ['Column 1', 'Column 2', 'Column 3', 'Column 4',...]  
tbl.setHorizontalHeaderLabels(header_labels)
for row in all_data:
    inx = all_data.index(row)
    tbl.insertRow(inx)
    tbl.setItem(inx,Y,QTableWidgetItem(your data)) # Y is the column that you want to insert data  

for example:

all_data = [[1,2,3,4],[5,6,7,8]]
tbl = QtGui.QTableWidget(len(all_data),4)
header_labels = ['Column 1', 'Column 2', 'Column 3', 'Column 4']
tbl.setHorizontalHeaderLabels(header_labels)
for row in all_data:
    inx = all_data.index(row)
    tbl.insertRow(inx)
    tbl.setItem(inx,0,QTableWidgetItem(str(row[0])))
    tbl.setItem(inx,0,QTableWidgetItem(str(row[0])))
    tbl.setItem(inx,0,QTableWidgetItem(str(row[0])))

I hope it was useful
Of course, if I did not understand your question, I apologize

like image 200
masood Avatar answered Oct 24 '25 20:10

masood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!