Problem with getting data from QTableWidget via iteration

35 views Asked by At

I'm using PySide2/PyQt5 to create a QTableWidget as shown in the code below. I'm using a QItemDelegate to restrict the input of certain columns to integer only. If I set cell 0,0 to say 3, then in the get_model function, the value 3 is printed for each cell when iterating throught the table. It should only print out 3 once. Why is this happening?

import sys
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *


class MyIntDelegate(QItemDelegate):
    '''Returns a spin box for editing the value in the Model column.'''
    def createEditor(self, parent, option, index):
        sb = QSpinBox(parent)
        sb.setMaximum(100) 
        sb.setMinimum(-100) 
        return sb


class MyTable(QTableWidget):

    def __init__(self, nrow, ncol, parent=None):
        super().__init__(nrow, ncol, parent=parent)
        self.horizontalHeader().setMinimumSectionSize(0) 
        self.verticalHeader().hide()
        self.setStyleSheet('''
        QTableView {alternate-background-color: blue;background: #d6d9dc}
        ''')
        self.setIntegerCols([0,1,2])
        self.itemChanged.connect(self.update_model)


    def setIntegerCols(self, cols):
        delegate = MyIntDelegate() #spin box for integer input
        for col in cols:
            self.setItemDelegateForColumn(col, delegate)


    def update_model(self, item):

        if item.text() == '':
            return
        row = item.row()
        col = item.column()
        print("Updating cell: {},{}".format(row, col))

        if item.text() == '0': 
            item.setText('') #this causes update_model() to be called a second time
        else:
            val = item.data(Qt.UserRole)
            text = item.text()
            print('Value of item.data(): {}'.format(val))
            print('Value of item.text(): {}'.format(text))
            item.setData(Qt.UserRole, int(text))
    

    def get_model(self):
        for i in range(self.rowCount()):
            for j in range(self.columnCount()):
                item = self.itemAt(i, j)
                if item is not None:
                    data = item.data(Qt.UserRole)
                    val = item.text()
                    print(f'item text {i},{j}: {val}')
                    print(f'item data {i},{j}: {data}')

class DebugWindow(QMainWindow):

    def __init__(self):
        super(DebugWindow, self).__init__()
        self.table = MyTable(10, 5) 
        button = QPushButton('Show')
        button.clicked.connect(self.table.get_model)
        layout = QVBoxLayout()
        layout.addWidget(self.table)
        layout.addWidget(button)
        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = DebugWindow()
    window.show()
    sys.exit(app.exec())
0

There are 0 answers