Results 1 to 10 of 10

Thread: dynamically created tablewidget name problem

  1. #1
    Join Date
    May 2016
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products

    Default dynamically created tablewidget name problem

    I have a function that dynamically creates a stackedWidget and adds new pages based on a query. Each page gets a dynamically created tableWidget that is loaded before the code loops again. The stackedWidget is controlled by a List that is filled at the same time that each page is created. Up to this point, everything works great but I am having a problem extracting data from the tableWidgets. At the end of the loops there is a signal

    Qt Code:
    1. ui.tableWidget.cellChanged[int,int].connect(saveLineItem)
    To copy to clipboard, switch view to plain text mode 

    that I used for testing. The function saveLineItem will print the row & column from whichever tableWidget is clicked on, but the last line, to print the text, will only print from the last tableWidget created. Obviously the signal recognizes the current tableWidget. How can I pass that on to the saveLineItem function?

    Qt Code:
    1. def saveLineItem(row, col):
    2. print('row = ' + str(row))
    3. print('column = ' + str(col))
    4. ui = uiDef.ui
    5. print(ui.tableWidget.item(row, col).text())
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. def loadInventory(ui):
    2. conn = sqlite3.connect('DbLocal.sqlite')
    3. cur = conn.cursor()
    4. SQL = "SELECT INVENTORY.Inventory FROM INVENTORY WHERE Description = '*' ORDER BY Display_No"
    5. cur.execute(SQL)
    6. rows = cur.fetchall()
    7.  
    8. if ui.swInventory:
    9. ui.swInventory.close()
    10. ui.lstCatagory.clear()
    11. ui.swInventory = QtWidgets.QStackedWidget(ui.tab_Inventory)
    12. ui.swInventory.setGeometry(QtCore.QRect(255, 20, 876, 586))
    13. ui.swInventory.setFrameShape(QtWidgets.QFrame.Box)
    14. ui.swInventory.setObjectName("swInventory")
    15. lstInvHeader = ['Display_No','ITEM_NO', 'Trigger', 'Quantity', 'Description', 'Part_No', 'Price', 'Inventory', 'Spreadsheet', 'NoDisplay', 'T_S', 'Track', 'Msg', 'S_Hooks', 'P_Hooks' ]
    16. for row in rows:
    17. sCatName = row[0]
    18. page = sCatName
    19. ui.lstCatagory.addItem(sCatName)
    20. ui.page = QtWidgets.QWidget()
    21. ui.page.setObjectName(sCatName)
    22. ui.tableWidget = QtWidgets.QTableWidget(ui.page)
    23. ui.tableWidget.setGeometry(QtCore.QRect(20, 20, 831, 561))
    24. font = QtGui.QFont()
    25. font.setPointSize(14)
    26. ui.tableWidget.setFont(font)
    27. ui.tableWidget.setAlternatingRowColors(True)
    28. ui.tableWidget.setObjectName("tableWidget")
    29. ui.tableWidget.setRowCount(1)
    30. ui.tableWidget.verticalHeader().setVisible(False)
    31.  
    32. ui.tableWidget.setColumnCount(15)
    33. ui.tableWidget.setHorizontalHeaderLabels(lstInvHeader)
    34. ui.tableWidget.hideColumn(0)
    35. ui.tableWidget.hideColumn(1)
    36. ui.tableWidget.hideColumn(2)
    37. ui.tableWidget.hideColumn(5)
    38. ui.tableWidget.hideColumn(7)
    39. ui.tableWidget.hideColumn(8)
    40. ui.tableWidget.hideColumn(9)
    41. ui.tableWidget.hideColumn(10)
    42. ui.tableWidget.hideColumn(11)
    43. ui.tableWidget.hideColumn(12)
    44. ui.tableWidget.hideColumn(13)
    45. ui.tableWidget.hideColumn(14)
    46. ui.tableWidget.setColumnWidth(4, 611)
    47.  
    48. SQL = "SELECT * FROM INVENTORY WHERE Inventory = '{}' ORDER BY Display_No".format(sCatName)
    49. cur.execute(SQL)
    50. InvRows = cur.fetchall()
    51. i = 0
    52. for row in InvRows:
    53. if row[4] == '*':
    54. pass
    55. else:
    56. rDisplay_No = str(row[0])
    57. iITEM_NO = str(row[1])
    58. iTRIGGER = str(row[2])
    59. iQUANTITY = ""
    60. if row[3] == None:
    61. iQUANTITY = ""
    62. else:
    63. #iQUANTITY = round(row[6], 2)
    64. iQUANTITY = str(iQUANTITY)
    65. sDESCRIPTION = " " + str(row[4])
    66. sPART_NO = row[5]
    67. rPRICE = str(row[6])
    68. sINVENTORY = row[7]
    69. sSPREADSHEET = row[8]
    70. iNoDisplay = str(row[9])
    71. iT_S = str(row[10])
    72. iTRACK = str(row[11])
    73. iMSG = str(row[12])
    74. iS_HOOKS = str(row[13])
    75. iP_HOOKS = str(row[14])
    76.  
    77. item = QTableWidgetItem(rDisplay_No)
    78. item2 = QTableWidgetItem(iITEM_NO)
    79. item3 = QTableWidgetItem(iTRIGGER)
    80. item4 = QTableWidgetItem(iQUANTITY)
    81. item5 = QTableWidgetItem(sDESCRIPTION)
    82. item6 = QTableWidgetItem(sPART_NO)
    83. item7 = QTableWidgetItem(rPRICE)
    84. item8 = QTableWidgetItem(sINVENTORY)
    85. item9 = QTableWidgetItem(sSPREADSHEET)
    86. item10 = QTableWidgetItem(iNoDisplay)
    87. item11 = QTableWidgetItem(iT_S)
    88. item12 = QTableWidgetItem(iTRACK)
    89. item13 = QTableWidgetItem(iMSG)
    90. item14 = QTableWidgetItem(iS_HOOKS)
    91. item15 = QTableWidgetItem(iP_HOOKS)
    92.  
    93. item4.setTextAlignment(Qt.AlignCenter)
    94. item5.setFlags( Qt.NoItemFlags | Qt.ItemIsEnabled)
    95. item7.setFlags( Qt.ItemIsSelectable | Qt.ItemIsEnabled)
    96.  
    97. ui.tableWidget.setItem(i,0, item)
    98. ui.tableWidget.setItem(i,1, item2)
    99. ui.tableWidget.setItem(i,2, item3)
    100. ui.tableWidget.setItem(i,3, item4)
    101. ui.tableWidget.setItem(i,4, item5)
    102. ui.tableWidget.setItem(i,5, item6)
    103. ui.tableWidget.setItem(i,6, item7)
    104. ui.tableWidget.setItem(i,7, item8)
    105. ui.tableWidget.setItem(i,8, item9)
    106. ui.tableWidget.setItem(i,9, item10)
    107. ui.tableWidget.setItem(i,10, item11)
    108. ui.tableWidget.setItem(i,11, item12)
    109. ui.tableWidget.setItem(i,12, item13)
    110. ui.tableWidget.setItem(i,13, item14)
    111. ui.tableWidget.setItem(i,14, item15)
    112. i += 1
    113. ui.tableWidget.setRowCount(i+2)
    114.  
    115. setattr(uiDef, "ui", ui)
    116. ui.tableWidget.cellChanged[int,int].connect(saveLineItem)
    117. ui.label = QtWidgets.QLabel(ui.page)
    118. ui.label.setGeometry(QtCore.QRect(308, 0, 151, 20))
    119. ui.label.setAlignment(QtCore.Qt.AlignCenter)
    120. ui.label.setObjectName("label")
    121. ui.label.setText(sCatName)
    122. ui.swInventory.addWidget(ui.page)
    123. iNumPages = ui.swInventory.count()
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamically created tablewidget name problem

    It is hard to tell frmo the code since the indentation is so small, but make sure the connect is inside the loop.

    You might also want to store all table widget references, not just the last one.

    Cheers,
    _

  3. #3
    Join Date
    May 2016
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products

    Default Re: dynamically created tablewidget name problem

    The connect is inside the loop, but I am not clear on where the reference to the last tableWidget is being stored.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamically created tablewidget name problem

    You always assign to ui.tableWidget, so that holds the reference to the last table widget that got created.

    Cheers,
    _

  5. #5
    Join Date
    May 2016
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products

    Default Re: dynamically created tablewidget name problem

    I see. So each tableWidget as it is created is assigned to ui.tablewidget and overwrites the previous one. I need to save each one to a list as it is created, the only problem that I have with that is how do I get them out of the list?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamically created tablewidget name problem

    A list is usually addressed via index.
    The first item is at index 0, the second at index 1 and so on.

    You can also store the references in an associative container, where the key is not a number but e.g. a string.

    Cheers,
    _

  7. #7
    Join Date
    May 2016
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products

    Default Re: dynamically created tablewidget name problem

    I understand that the list is addressed by index, but I don't see how the signal can know what the index is. When the signal is created in the loop and the tableWidget reference is saved to the list, is there some connection that happens so the signal knows what listWidget reference to use?

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamically created tablewidget name problem

    The signal does not know anything about an index, but the slot can get the sender of the signal that caused the slot to be called.

    Alternatively you can create a "signal handler" class, i.e. a class derived from QObject to handle all signals for one single table view, and then create one instance of that class for each table view.

    Cheers,
    _

  9. #9
    Join Date
    May 2016
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products

    Default Re: dynamically created tablewidget name problem

    Quote Originally Posted by anda_skoa View Post
    but the slot can get the sender of the signal that caused the slot to be called._
    Could you expand on that a bit? If the slot got the sender of the signal, wouldn't that be the actual reference to the tableWidget so you wouldn't need to save it to a list in the first place?

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: dynamically created tablewidget name problem

    Yes, that would be the actual reference to the tableWidget, whether it is saved somewhere else or not doesn't matter.

    See QObject::sender().

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 18th June 2015, 07:58
  2. use value from dynamically created widget in a slot
    By Cyrebo in forum Qt Programming
    Replies: 55
    Last Post: 23rd April 2013, 15:51
  3. Dynamically created buttons.
    By Tomasz in forum Newbie
    Replies: 26
    Last Post: 2nd December 2010, 10:40
  4. Replies: 7
    Last Post: 2nd August 2010, 13:28
  5. tableWidget created in Qt Creator not updated
    By binaural in forum Qt Programming
    Replies: 2
    Last Post: 10th July 2009, 22:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.