在 QtableWidget 中插入多项

Posted

技术标签:

【中文标题】在 QtableWidget 中插入多项【英文标题】:Insert multi items in QtableWidget 【发布时间】:2019-04-03 14:11:47 【问题描述】:

这是我项目的摘录。 我想通过这个方法 updateActifData() 从数据字典中的每个键中获取项目。通常,当我调用它时,它会传递第一个参数来生成第一个项目,第二个参数生成第二个项目....当我从第一个键获取项目时,我想将它插入到相应的 QTableWidget 的单元格中(n, m), K="234" for n=18 并对所有键执行此过程。但我的问题是,当我调用 updateActifData() 时,它会显示最后一个单元格中所有键的总和。如果您有任何建议,谢谢。

data = '2111':['Capital 1','145321','94565','','','','74651','','','24651','','',''], '23411':['Capital 2','65115','','6149','6645','555641','','','','41245','98416','',''], '23421':['Capital 3','54445','','45c4545','','555641','434556','','453453','','98416','','45453'], '3421':['Capital 4','714445','','34545','155485','','894556','','15645','26149','98416','',''], '3422':['Capital 5','87676','','54553','466149','','','95436','','','76745','','74568',''], '3451':['Capital 6','45645','','','47679','17345','555641','45256','','','412045','98416','',''], '2230':['Capital 7','453445','74345','','466149','44645','1','98656','12564','','412045','98416','',''], '3452':['Capital 8','97845c4','','','1326149','14553','555641','','45345','86945','412045','98416','','']

class Table(QWidget):
def __init__(self, *args, parent=None):
    super().__init__()

    self.data = data
    self.item_n, self.item_b_1, self.item_b_2 = 0, 0, 0
    self.r = BlData(self.data, self.item_n, self.item_b_1, self.item_b_2)
    self.setuptUI()

def setuptUI(self):

    self.setWindowTitle("QTableWidgetItem")
    self.resize(1200, 800)
    conLayout = QHBoxLayout()

    self.tableWidget_Bl =QTableWidget(self)
    self.tableWidget_Bl.setRowCount(55)
    self.tableWidget_Bl.setColumnCount(14)
    conLayout.addWidget(self.tableWidget_Bl)
    self.setLayout(conLayout)
    self.updateItems()

def updateItems(self):
    m = 5
    #if self.data:
    self.r.setData()
    self.tableWidget_Bl.setItem(int(self.r.getrow()), m, QTableWidgetItem(str(self.r.getnData())))
    self.tableWidget_Bl.setItem(int(self.r.getrow()), m-1, QTableWidgetItem(str(self.r.getb_2Data())))  
    self.tableWidget_Bl.setItem(int(self.r.getrow()), m-2, QTableWidgetItem(str(self.r.getb_1Data()))) 
    return self.r.setData()


class BalanceData():
data = 
def __init__(self, data):
    self.data = data

class BlData(BalanceData):
    def __init__ (self, data, item_n, item_b_1, item_b_2):
        BalanceData.__init__(self, data)
        self.item_n, self.item_b_1, self.item_b_2 = 0, 0, 0
        self.n = 0
        self.setData()

def setData(self):
    self.updateActifData(k="234", n=18)
    self.updateActifData(k="342", n=39)
    self.updateActifData(k="345", n=41)

def updateActifData(self, k, n):
    #the sum for one key 
    self.n = n
    item_n, item_b_1, item_b_2, item_1, item_2 = 0, 0, 0, 0, 0
    v=5
    for kd, vd in self.data.items():
        if kd[:len(k)] == k:
            s_1 = vd[v] 
            print(":<10 -> :10.2f".format(kd, float(s_1) if s_1 else 0))
            item_1 = item_1 + (float(s_1) if s_1 else 0)
            #print(item)    
            s_2 = vd[v+1]  # the next range
            print(":<10 -> :10.2f".format(kd, float(s_2) if s_2 else 0))
            item_2 = item_2 + (float(s_2) if s_2 else 0)

            self.item_b_1 = item_1 + item_2 

    for kd, vd in self.data.items():
        if kd[:len(k)] == k:
            s_1 = vd[v+6] 
            print(":<10 -> :10.2f".format(kd, float(s_1) if s_1 else 0))
            item_1 = item_1 + (float(s_1) if s_1 else 0)
            #print(item)    
            s_2 = vd[v+7]  # the next range
            print(":<10 -> :10.2f".format(kd, float(s_2) if s_2 else 0))
            item_2 = item_2 + (float(s_2) if s_2 else 0)

            self.item_b_2 = item_1 + item_2

    self.item_n = self.item_b_1 - self.item_b_2


def getrow (self):
    print ("getrow:", self.n)
    return self.n

def getb_1Data (self):
    return self.item_b_1

def getb_2Data (self):
    return self.item_b_2

def getnData (self):
    return self.item_n

if __name__ == '__main__':
    app = QApplication(sys.argv)
    windows = Table()
    windows.show()
    sys.exit(app.exec_())

【问题讨论】:

【参考方案1】:

试试看:

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

data = '2111' :['Capital 1','1','1', '', '', '',  '1', '', '', '1','', '',  ''], 
        '23411':['Capital 2','2', '', '2','2','21','21','', '', '2','2','23','24'], 
        '23421':['Capital 3','3', '', '3','', '31','32','', '3','', '3','33','34'], 
        '3421' :['Capital 4','4', '', '4','4','41','42','', '4','4','4','43','44'], 
        '3422' :['Capital 5','5', '', '5','5','51','52','5','', '', '5','53','54'], 
        '3451' :['Capital 6','6', '', '', '6','61','62','6','', '', '6','63','64'], 
        '2230' :['Capital 7','7', '7','', '7','7', '7', '7','7','', '7','7', ''], 
        '3452' :['Capital 8','8', '', '', '8','81','82','', '8','8','8','83','84']

class Table(QWidget):
    def __init__(self, *args, parent=None):
        super().__init__()

        self.data = data
        self.item_n, self.item_b_1, self.item_b_2 = 0, 0, 0
        self.r = BlData(self.data, self.item_n, self.item_b_1, self.item_b_2)

        self.setuptUI()

    def setuptUI(self):
        self.tableWidget_Bl = QTableWidget(55, 14, self)

        conLayout = QHBoxLayout()
        conLayout.addWidget(self.tableWidget_Bl)
        self.setLayout(conLayout)

        self.updateItems()

    def updateItems(self):
        m = 5
        runItem = (("234", 18), ("342", 39), ("345", 41))          # <-----------
        for k, n in runItem:                                       # <-----------
            self.r.updateActifData( k, n )
            self.tableWidget_Bl.setItem(int(self.r.getrow()), m, QTableWidgetItem(str(self.r.getnData())))
            m -= 1


class BalanceData():
    data = 
    def __init__(self, data):
        self.data = data

class BlData(BalanceData):
    def __init__ (self, data, item_n, item_b_1, item_b_2):
        BalanceData.__init__(self, data)

    def updateActifData(self, k, n):
        #the sum for one key 
        self.n = n
        item_n, item_b_1, item_b_2, item_1, item_2 = 0, 0, 0, 0, 0
        item_3, item_4 = 0, 0                        # +++
        v = 5
        for kd, vd in self.data.items():
            if kd[:len(k)] == k:
                s_1 = vd[v] 
                print("\n1 :<10 -> :10.2f".format(kd, float(s_1) if s_1 else 0))
                item_1 = item_1 + (float(s_1) if s_1 else 0)
                #print(item)    
                s_2 = vd[v+1]  # the next range
                print("2 :<10 -> :10.2f".format(kd, float(s_2) if s_2 else 0))
                item_2 = item_2 + (float(s_2) if s_2 else 0)

                self.item_b_1 = item_1 + item_2 

#        for kd, vd in self.data.items():
#            if kd[:len(k)] == k:
                s_1 = vd[v+6] 
                print("3 :<10 -> :10.2f".format(kd, float(s_1) if s_1 else 0))
                item_3 = item_3 + (float(s_1) if s_1 else 0)
                #print(item)    
                s_2 = vd[v+7]  # the next range
                print("4 :<10 -> :10.2f".format(kd, float(s_2) if s_2 else 0))
                item_4 = item_4 + (float(s_2) if s_2 else 0)

                self.item_b_2 = item_3 + item_4

        self.item_n = self.item_b_1 - self.item_b_2

        print("item_n= ``, item_b_1= ``, item_b_2= ``"
              "".format(self.item_n, self.item_b_1, self.item_b_2))

    def getrow (self):
        print ("getrow:", self.n)
        return self.n

    def getnData (self):
        return self.item_n

if __name__ == '__main__':
    app = QApplication(sys.argv)
    windows = Table()
    windows.setWindowTitle("QTableWidgetItem")
    windows.resize(1200, 800)
    windows.show()
    sys.exit(app.exec_())

【讨论】:

兄弟有问题。如果你给 runItem 一些元素,例如 ("238", 19),并且这个元素在数据字典中不存在,通常它应该是一个 "0" 但它是对以前值的引用。我的程序的 runItem 是一个静态资产,包含假设在数据字典中的所有键。但是字典会根据数据而改变。 这个方法getnData(),当它返回“self.item_n”的时候还是记忆中的值,我想销毁它并返回到0。

以上是关于在 QtableWidget 中插入多项的主要内容,如果未能解决你的问题,请参考以下文章

只允许 QTableWidget 列中的值

在qtable中打开csv文件时程序关闭

Python - PyQt - QTable 小部件 - 添加行

qt 中Qtable widget中如何加一个表

Qt QTableWidget 列调整大小

如何更改水平标题的高度(QTableWidget)