PDA

View Full Version : dynamically created tablewidget name problem



nlgootee
15th July 2016, 20:51
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


ui.tableWidget.cellChanged[int,int].connect(saveLineItem)

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?


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

anda_skoa
16th July 2016, 11:43
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,
_

nlgootee
17th July 2016, 16:19
The connect is inside the loop, but I am not clear on where the reference to the last tableWidget is being stored.

anda_skoa
17th July 2016, 19:20
You always assign to ui.tableWidget, so that holds the reference to the last table widget that got created.

Cheers,
_

nlgootee
19th July 2016, 18:40
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?

anda_skoa
19th July 2016, 22:34
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,
_

nlgootee
21st July 2016, 00:52
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?

anda_skoa
21st July 2016, 08:59
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,
_

nlgootee
21st July 2016, 16:33
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?

anda_skoa
21st July 2016, 17:28
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,
_