0

I'm trying to insert a hyperlink into a table cell. How can I do this?

what i do:

self.tableWidget.setItem(0, 0, QtWidgets.QTableWidgetItem('<a href="https://google.com">google</a>'))

but it doesn't work

re1ner
  • 13
  • 3

1 Answers1

0

add this to your code:

self.tableWidget.itemDoubleClicked.connect(self.OpenLink)

def OpenLink(self,item):
    if item.column() == 1:
        webbrowser.open('www.google.com')

you can change it based on your needs like that: item.text() == the text in the cell, item.row() == row number and item.column() == column number.

Guy Nachshon
  • 2,411
  • 4
  • 16
  • Thanks! I tried this method, but I had an error with the condition "item.column() == 0:" because of which the method did not work. Now I decided to try without it and everything worked. – re1ner Jan 19 '22 at 08:14
  • so you just removed the conditioning? (im asking so I can edit the answer :) ) – Guy Nachshon Jan 19 '22 at 08:17
  • 1
    By default, double-click cell editing was enabled. I disabled editing and added your condition. And it worked. Your answer is fine, you don't need to change it – re1ner Jan 19 '22 at 08:41