Ben Pinhas
Ben Pinhas

Reputation: 63

Displaying one cell from a selected row using pyqt5

im trying to create an UI with PyQt5 which has a tableWidget and a label that will display the text in every 4th column of the table, by order while the user scrolls through. I cant seem to get the text in the selected cell from the table.. closest i got is this:

def open_csv_in_table (self):

    f = open ("test.csv")

    fData = csv.reader(f)

    csvTable = list(fData)

    self.tableWidget.setRowCount(len(csvTable))

    self.tableWidget.setColumnCount(len(csvTable[0])-4)

    for line in range( len(csvTable)):

        for row in range(len(csvTable[0])):

            self.tableWidget.setItem(line, row,QtWidgets.QTableWidgetItem(csvTable[line][row]))

    self.tableWidget.setColumnWidth(0 , 10) # ID

    self.tableWidget.setColumnWidth(1 , 150) # TEST NAME

    self.tableWidget.setColumnWidth(2 , 50) # STATUS

    self.tableWidget.setColumnWidth(3 , 300) # REMARKS

    self.tableWidget.setColumnWidth(4 , 737) # LONG DESCRIPTION

def label_display(self):

    self.label.setText(str(self.tableWidget.itemClicked))

    print(str(self.tableWidget.itemClicked))

And im calling the display function with:

self.open_csv_in_table()          
self.tableWidget.itemClicked.connect (lambda: self.label_display())

Upvotes: 0

Views: 171

Answers (1)

eyllanesc
eyllanesc

Reputation: 243907

itemClicked is a signal that does not have the information of the item clicked so that is not the way to get that value. The signals what to do is pass the data through the arguments of the slot, in your case you must change to:

def label_display(self, item):
    self.label.setText(item.text())

and

self.open_csv_in_table()          
self.tableWidget.itemClicked.connect(self.label_display)

Upvotes: 1

Related Questions