Search code examples
pythonpython-3.xpyqt5qtablewidgetqtablewidgetitem

Pyqt5: QtableWidgetItem "None"


I have created a table in PyQt5 and and retrieve the data in the cell by this method. But when i try the cell in QTableWidgetItem becomes None. So, how i can solve this problem and how i can insert other determined items in a specifice cell for exemple item 2, item3,... Thank you

import sys
from PyQt5.QtWidgets import (QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem)
from PyQt5.QtGui import QBrush, QColor 
from PyQt5 import QtCore

data = {'111':['Title 1','121','94565','','','','684651','','','44651','','',''], '112':['Title 2','65115','','466149','46645','555641','','','','412045','98416','',''], '113':['Title 3','','','','466149','46645','555641','98656','','','412045','98416','','']} 

class Table(QWidget):
    def __init__(self, *args, parent=None):
        super().__init__()
        self.data = data
        self.setuptUI()
        k = 'str'
        v = int
        n = int
        m = int
        self.setdata(k, v, n, m)

def setuptUI(self):
  self.setWindowTitle("QTableWidgetItem")
                self.resize(1200, 800)
                conLayout = QHBoxLayout()
    self.tableWidget =QTableWidget(self)
    self.tableWidget.setRowCount(55)
    self.tableWidget.setColumnCount(14)
    conLayout.addWidget(self.tableWidget)

def setdata(self, k, v, n, m):
    global item
    item = str(self.data.get(k))
    for key in self.data:
        if k in key:
            item = self.data.get(k)[v]
            print(item)
    newItem = QTableWidgetItem(str(item))
    newItem.setForeground(QBrush(QColor(255, 0, 0)))
    self.tableWidget.setItem(23, 4, newItem)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    windows = Table(data)
    item1= Table()
    item1.setdata(k="113", v=5, n=24, m=4)
    #item2= Table()
    #item2.setdata(k="113", v=5, n=25, m=4
    windows.show()
    sys.exit(app.exec_())

Solution

  • Try it:

    import sys
    from PyQt5.QtWidgets import (QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem)
    from PyQt5.QtGui import QBrush, QColor 
    from PyQt5 import QtCore
    
    data = {'111':['Title 1','121','94565','','','','684651','','','44651','','',''], 
            '112':['Title 2','65115','','466149','46645','555641','','','','412045','98416','',''], 
            '113':['Title 3','','','','466149','46645','555641','98656','','','412045','98416','','']} 
    
    class Table(QWidget):
        def __init__(self, data):                              # data
            super().__init__()
            self.data = data
            self.setuptUI()
    
    #        k = 'str'
    #        v = int
    #        n = int
    #        m = int
    #        self.setdata(k, v, n, m)
    
        def setuptUI(self):
            self.setWindowTitle("QTableWidgetItem")
            self.resize(1200, 600)
    
            conLayout        = QHBoxLayout(self)                 # + self
            self.tableWidget = QTableWidget(self)
            self.tableWidget.setRowCount(55)
            self.tableWidget.setColumnCount(14)
            conLayout.addWidget(self.tableWidget)
    
        def setdata(self, k, v, n, m):
    #        global item                                          # ---
    #        item = str(self.data.get(k))
    #        for key in self.data:
    #            if k in key:
            item = self.data.get(k)[v]
            print(item)
    
            newItem = QTableWidgetItem(str(item))
            newItem.setForeground(QBrush(QColor(255, 0, 0)))
    #        self.tableWidget.setItem(23, 4, newItem)
            self.tableWidget.setItem(n, m, newItem)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
    
        windows = Table(data)
    #-    item1= Table()
    #-    item1.setdata(k="113", v=5, n=24, m=4)
        windows.setdata(k="113", v=5, n=24, m=4)
        windows.setdata(k="113", v=6, n=24, m=5)         #  v=6, m=5
    
        windows.show()
        sys.exit(app.exec_())
    

    enter image description here