动态创建的tablewidget

时间:2016-07-15 20:02:22

标签: python-3.x pyqt5

我有一个动态创建stackedWidget的函数,并根据查询添加新页面。每个页面都会获得一个动态创建的tableWidget,它在代码循环之前加载。 stackedWidget由List控制,该List在创建每个页面的同时填充。到目前为止,一切都很好,但我在从tableWidgets中提取数据时遇到问题。在循环结束时有一个信号

ui.tableWidget.cellChanged[int,int].connect(saveLineItem)
我用于测试。 saveLineItem函数将打印行&从单击任何tableWidget的列,但最后一行,打印文本,将只从最后创建的tableWidget打印。显然,信号识别当前的tableWidget。如何将其传递给saveLineItem函数?

    def saveLineItem(row, col): 
       print('row = ' + str(row))
       print('column = ' + str(col))
       ui = uiDef.ui
       print(ui.tableWidget.item(row, col).text())

    def loadInventory(ui):
            conn = sqlite3.connect('DbLocal.sqlite')
            cur = conn.cursor() 
            SQL = "SELECT INVENTORY.Inventory FROM INVENTORY WHERE Description = '*' ORDER BY Display_No"
            cur.execute(SQL)
            rows = cur.fetchall()

            if ui.swInventory:
                ui.swInventory.close()
            ui.lstCatagory.clear()
            ui.swInventory = QtWidgets.QStackedWidget(ui.tab_Inventory)
            ui.swInventory.setGeometry(QtCore.QRect(255, 20, 876, 586))
            ui.swInventory.setFrameShape(QtWidgets.QFrame.Box)
            ui.swInventory.setObjectName("swInventory")
            lstInvHeader = ['Display_No','ITEM_NO', 'Trigger', 'Quantity', 'Description', 'Part_No', 'Price', 'Inventory', 'Spreadsheet', 'NoDisplay', 'T_S', 'Track', 'Msg', 'S_Hooks', 'P_Hooks' ]
            for row in rows:
                sCatName = row[0]
                page = sCatName
                ui.lstCatagory.addItem(sCatName)
                ui.page = QtWidgets.QWidget()
                ui.page.setObjectName(sCatName)
                ui.tableWidget = QtWidgets.QTableWidget(ui.page)
                ui.tableWidget.setGeometry(QtCore.QRect(20, 20, 831, 561))
                font = QtGui.QFont()
                font.setPointSize(14)
                ui.tableWidget.setFont(font)
                ui.tableWidget.setAlternatingRowColors(True)        
                ui.tableWidget.setObjectName("tableWidget")
                ui.tableWidget.setRowCount(1)
                ui.tableWidget.verticalHeader().setVisible(False)

                ui.tableWidget.setColumnCount(15)
                ui.tableWidget.setHorizontalHeaderLabels(lstInvHeader)
                ui.tableWidget.hideColumn(0)
                ui.tableWidget.hideColumn(1)
                ui.tableWidget.hideColumn(2)
                ui.tableWidget.hideColumn(5)
                ui.tableWidget.hideColumn(7)
                ui.tableWidget.hideColumn(8)
                ui.tableWidget.hideColumn(9)
                ui.tableWidget.hideColumn(10)
                ui.tableWidget.hideColumn(11)
                ui.tableWidget.hideColumn(12)
                ui.tableWidget.hideColumn(13)
                ui.tableWidget.hideColumn(14)
                ui.tableWidget.setColumnWidth(4, 611)

                SQL = "SELECT * FROM INVENTORY WHERE Inventory = '{}' ORDER BY Display_No".format(sCatName)
                cur.execute(SQL)
                InvRows = cur.fetchall()
                i = 0
                for row in InvRows:
                    if row[4] == '*':
                        pass
                    else:
                        rDisplay_No = str(row[0])
                        iITEM_NO = str(row[1])
                        iTRIGGER = str(row[2])
                        iQUANTITY = ""
                        if row[3] == None:
                            iQUANTITY = ""
                        else:
                            #iQUANTITY = round(row[6], 2)               
                            iQUANTITY = str(iQUANTITY)
                        sDESCRIPTION = " " + str(row[4])
                        sPART_NO = row[5]
                        rPRICE = str(row[6])
                        sINVENTORY = row[7]
                        sSPREADSHEET = row[8]
                        iNoDisplay = str(row[9])
                        iT_S = str(row[10])
                        iTRACK = str(row[11])
                        iMSG = str(row[12])
                        iS_HOOKS = str(row[13])
                        iP_HOOKS = str(row[14])

                        item = QTableWidgetItem(rDisplay_No)
                        item2 =  QTableWidgetItem(iITEM_NO)
                        item3 =  QTableWidgetItem(iTRIGGER)
                        item4 = QTableWidgetItem(iQUANTITY)
                        item5 =  QTableWidgetItem(sDESCRIPTION)
                        item6 =  QTableWidgetItem(sPART_NO)
                        item7 = QTableWidgetItem(rPRICE)
                        item8 =  QTableWidgetItem(sINVENTORY)
                        item9 =  QTableWidgetItem(sSPREADSHEET)
                        item10 = QTableWidgetItem(iNoDisplay)
                         item11 =  QTableWidgetItem(iT_S)
                        item12 =  QTableWidgetItem(iTRACK)
                        item13 = QTableWidgetItem(iMSG)
                        item14 =  QTableWidgetItem(iS_HOOKS)
                        item15 =  QTableWidgetItem(iP_HOOKS)

                        item4.setTextAlignment(Qt.AlignCenter)
                        item5.setFlags( Qt.NoItemFlags | Qt.ItemIsEnabled)
                        item7.setFlags( Qt.ItemIsSelectable | Qt.ItemIsEnabled)

                        ui.tableWidget.setItem(i,0, item)
                        ui.tableWidget.setItem(i,1, item2)
                        ui.tableWidget.setItem(i,2, item3)
                        ui.tableWidget.setItem(i,3, item4)
                        ui.tableWidget.setItem(i,4, item5)
                        ui.tableWidget.setItem(i,5, item6)
                        ui.tableWidget.setItem(i,6, item7)
                        ui.tableWidget.setItem(i,7, item8)
                        ui.tableWidget.setItem(i,8, item9)
                        ui.tableWidget.setItem(i,9, item10)
                        ui.tableWidget.setItem(i,10, item11)
                        ui.tableWidget.setItem(i,11, item12)
                        ui.tableWidget.setItem(i,12, item13)
                        ui.tableWidget.setItem(i,13, item14)
                        ui.tableWidget.setItem(i,14, item15)
                        i += 1
                        ui.tableWidget.setRowCount(i+2)

                setattr(uiDef, "ui", ui)    
                ui.tableWidget.cellChanged[int,int].connect(saveLineItem)
                ui.label = QtWidgets.QLabel(ui.page)
                ui.label.setGeometry(QtCore.QRect(308, 0, 151, 20))
                ui.label.setAlignment(QtCore.Qt.AlignCenter)
                ui.label.setObjectName("label")
                ui.label.setText(sCatName)
                ui.swInventory.addWidget(ui.page)
            iNumPages = ui.swInventory.count()

1 个答案:

答案 0 :(得分:0)

解决此类问题的常用方法是将信号连接到缓存对当前对象的引用的lambda(或部分函数):

def saveLineItem(table, row, col):
   print('row = ' + str(row))
   print('column = ' + str(col))
   print(table.item(row, col).text())

def loadInventory(ui):
    ...
    ui.tableWidget.cellChanged[int,int].connect(
        lambda row, colum, table=ui.tableWidget:
            saveLineItem(table, row, column))