K. Mather
K. Mather

Reputation: 93

Dynamically Set contents of QTableWidget from array

edited to add full GUI code in case it is relevant

I am trying to generate the contents of a QTableWidget from a numpy array. This code runs, but the values are not showing up in the cells, and I can't figure out why. Can any experts help? I am a beginner at Qt/pyqt5.

import all the modules etc.

class CenterPanel(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self)
        self.Parent = parent

        self.data = QTableWidget() #construct TableWidget
        self.data.setColumnCount(2) #set columns and column labels
        self.data.setHorizontalHeaderLabels(('P','T'))
        self.btn = QPushButton('Plot') #button to populate QTableBox
        self.btn.clicked.connect(self.plot) 

        vbx_Data = QVBoxLayout() #Create vertical box layout to hold table
        vbx_Data.setTitle('Data')
        vbx_Data.addWidget(self.data) #Add table to box layout
        vbx_Data.addWidget(self.btn) #Add button to box layout
        self.setLayout(vbx_Data)

    def plot(self):
        # dataframe with 2 columns and n rows        
        df = pd.DataFrame((some_Data),('z','T'))
        self.data.setRowCount(len(df))

        # Enter data onto Table
        for n, value in enumerate(df['z']):  # loop over items in first column
            self.data.setItem(n, 0, QTableWidgetItem(value))
            print(n)
            print(value)

        for n, value in enumerate(df['T']):  # loop over items in second column
            self.data.setItem(n, 1, QTableWidgetItem(value))
            print(n)
            print(value)


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.setWindowTitle('Plotting')
        WinLeft = 150
        WinTop = 150
        WinWidth = 1100
        WinHight = 800
        self.setGeometry(WinLeft, WinTop, WinWidth, WinHight)

        self.CenterPane = CenterPanel(self)
        self.setCentralWidget(self.CenterPane)
        self.setStyle(QStyleFactory.create('Cleanlooks'))


if __name__ == "__main__":
    MainThred = QApplication([])

    MainGui = MainWindow()
    MainGui.setFocus()
    MainGui.show()

    sys.exit(MainThred.exec_())

The terminal output from the prints show that I am getting the values out correctly, and in the correct format, from the dataframe series. But why is the QTableWidget still blank? It correctly adjusts the row count, but doesn't populate. I can't see where I'm going wrong, please help!

Upvotes: 1

Views: 2640

Answers (1)

K. Mather
K. Mather

Reputation: 93

Edit! Solved - needed the values to add to the table to be strings, not floats. Hope that helps someone with the same problem!

    # Enter data onto Table
    for n, value in enumerate(df['z']):  # loop over items in first column
        self.data.setItem(n, 0, QTableWidgetItem(str(value)))
        print(n)
        print(value)

    for n, value in enumerate(df['T']):  # loop over items in second column
        self.data.setItem(n, 1, QTableWidgetItem(str(value)))

Upvotes: 1

Related Questions