2016-07-15 104 views
0

我有一個函數可以根據查詢動態創建一個stackedWidget並添加新頁面。每個頁面都會獲得一個動態創建的tableWidget,該代碼在代碼再次循環之前加載。 stackedWidget由一個列表控制,該列表在創建每個頁面的同時填充。到目前爲止,一切都很好,但我從tableWidgets提取數據時遇到了問題。在循環的末端有一個信號動態創建tablewidget

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() 

回答

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)) 
+0

saveLineItem()的調用信號:ui.tableWidget.cellChanged [int,int] .connect(saveLineItem),它沒有表參數,所以如何工作? – nlgootee

+0

我的評論不清楚。當我最初設置這個時,我嘗試添加(row,col)參數「ui.tableWidget.cellChanged [int,int] .connect(saveLineItem(row,col)),但它不接受任何參數,對我來說沒有任何意義,但是刪除參數可以打印行和列,我會試試這個。謝謝@ekhumoro – nlgootee