Search code examples
pythonuser-interfacepyqtsignals-slotsqtablewidget

cellDoubleClicked text python


Im dealing with a problem when using PyQt5, i already made a QTableWidget which displays a DataFrame i prevously made in pandas (from a spreadsheet).

What im trying unsuccesfully to do is: Get the text from the cell that the user double-clicks so i can use that information to build up a new Widget based on choice made.

What i've learned is that i first need to connect the double-click to a function, but i cant continue afterwards:

my connection on my QTableWidget goes as follows:

(Under my MainWindow Widget)

self.tableView.cellDoubleClicked.connect(self.expandShipments)

"Afterwards i declare the function to which this signal connects (expandShipments)"

def expandShipments(self):

 *** code i need to get doubleClicked cell text ***

I'd really appreciate a hand with this case.

Thanks. Kudos

EDIT: This is the way my table is populated.

QTableWidget.

def loadFile(self):
    fileName='C:/Users/310287757/Desktop/JLG/Programming/tstBIGDF.xlsx'
    df = pandas.read_excel(fileName, sheetname='MAIN', header=0)  # read file and set header row
    df= df.loc[df['Name Opp'] == self.comboProy.currentText()]
    self.tableView.setColumnCount(len(df.columns))
    self.tableView.setRowCount(len(df.index))
    tags=[]
    for ele in list(df.columns.values):
        tags.append(ele)

    self.tableView.setHorizontalHeaderLabels(tags)
    for i in range(len(df.index)):
        for j in range(len(df.columns)):
            self.tableView.setItem(i, j, QTableWidgetItem(str(df.iat[i, j])))

    self.tableView.resizeColumnsToContents()
    self.tableView.resizeRowsToContents()
    PPen = df['Pending amount'].values.sum()
    PTot = df['total item amount'].values.sum()
    PP=("Pendiente por facturar: U$S %.2f"% PPen)
    PT=("Total: U$S %.2f" % PTot)
    self.lblPEND.setText(PP)
    self.lblTOTAL.setText(PT)

    del df,fileName,tags

Solution

  • The cellDoubleClicked signal sends the row and column. So your code should look like this:

    self.tableView.cellDoubleClicked.connect(self.expandShipments)
    ...
    
    def expandShipments(self, row, column):
        item = self.tableView.item(row, column)
        print(item.text())