Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python How to pass additional information with PyQt5's: QTableWidget::cellDoubleClicked(int row, int column)

AD_Sign

New Coder
I understand that the cellDoubleClicked method passes the row and the column to a specified function, but how can I send additional information as function arguments? For example the list the table was filled with.

if I have the following fuction (with functools.partial imported):
[CODE lang="python" title="Make Table Function"]
def makeTable(self, dataSet, layout):
cols = len(dataSet[0]) #columns
rows = len(dataSet) #rows

table = qtw.QTableWidget()

table.setRowCount(rows)
table.setColumnCount(cols)

for i in range(len(dataSet)):
for j in range(len(dataSet)):
table.setItem(i,j, QTableWidgetItem(dataSet[j]))

table.cellDoubleClicked.connect(partial(self.cellClick, i, j, dataSet))
layout.addWidget(table)
[/CODE]

The Line
Code:
 table.cellDoubleClicked.connect(partial(self.cellClick, i, j, dataSet))
always ends up passing the last iteration of i and j.
If I omit them, and send them a function like this for example:
[CODE lang="python" title="Example cell click function"] def cellClick(self, r, c, dataSet):
print('here: ', r, ' ', c, ' ', dataSet[r][c], '\n', dataSet)
[/CODE]
ends up with a "TypeError: 'int' object is not subscriptable" error.
Is there a way I can send the row and column of a cell double clicked, and the associated data set along with it?
 
On the off chance anyone ever see's this post, I created a solution to this problem:

if you go back to your data set/list declaration and declare it with "self.listname"
[CODE lang="python" title="Example list declaration"]self.listName = [] [/CODE]
You can then access that list anywhere else in your class without having to pass it as a variable.

Furthermore you can access the line you want with in your on-click function with:
[CODE lang="python" title="Example on-click function"]def cellClick(self, r, c):
myList = self.previouslyDeclaredList
item = self.table.item(r, c)
item = item.text()

# Do what you want with your list, table, rows, and columns

[/CODE]

This works, but any other solutions would be greatly accepted
 

New Threads

Buy us a coffee!

Back
Top Bottom