15

Is there anyway to add like a button in qtablewidget? But the date within the cell would stil have to be displaying, for example if an user double clicked a cell, could i send a signal like a button? Thanks!

edititem():

def editItem(self,clicked):
    if clicked.row() == 0:
        #go to tab1
    if clicked.row() == 1:
        #go to tab1
    if clicked.row() == 2:
        #go to tab1
    if clicked.row() == 3:
        #go to tab1

table trigger:

self.table1.itemDoubleClicked.connect(self.editItem)

2 Answers2

33

You have a couple of questions rolled into one...short answer, yes, you can add a button to a QTableWidget - you can add any widget to the table widget by calling setCellWidget:

# initialize a table somehow
table = QTableWidget(parent)
table.setRowCount(1)
table.setColumnCount(1)

# create an cell widget
btn = QPushButton(table)
btn.setText('12/1/12')
table.setCellWidget(0, 0, btn)

But that doesn't sound like what you actually want.

It sounds like you want to react to a user double-clicking one of your cells, as though they clicked a button, presumably to bring up a dialog or editor or something.

If that is the case, all you really need to do is connect to the itemDoubleClicked signal from the QTableWidget, like so:

def editItem(item):
    print 'editing', item.text()    

# initialize a table widget somehow
table = QTableWidget(parent)
table.setRowCount(1)
table.setColumnCount(1)

# create an item
item = QTableWidgetItem('12/1/12')
table.setItem(0, 0, item)

# if you don't want to allow in-table editing, either disable the table like:
table.setEditTriggers( QTableWidget.NoEditTriggers )

# or specifically for this item
item.setFlags( item.flags() ^ Qt.ItemIsEditable)

# create a connection to the double click event
table.itemDoubleClicked.connect(editItem)
Eric Hulser
  • 3,912
  • 21
  • 20
  • How can I make the row so that when the user clicks the row is highlighted and not the individual cell. –  Aug 17 '12 at 17:22
  • 4
    do: table.setSelectionBehavior( QTableWidget.SelectRows ) – Eric Hulser Aug 17 '12 at 17:27
  • Also, is there anyway that i can redirect to a different tab when a certain row is clicked? My question is can I basically redirect to a different tab, sort of like a hyperlink on a html webpage. –  Aug 17 '12 at 17:53
  • Redirect or move to a tab when a button is clicked? Any ideas. –  Aug 17 '12 at 20:17
  • Sure, you can do whatever you want really. Link to the itemClicked signal and then modify your tab in the connected slot. I'd need more code to go for a better answer than that. – Eric Hulser Aug 17 '12 at 23:17
  • I have added some code, for example when double clicked the signal is taken to the function editItem(), how then do i redirct to a tabe? –  Aug 20 '12 at 13:58
  • Well, assuming you have a tab widget somewhere with corresponding indexes, it would be something like: self.tabWidget.setCurrentIndex(clicked.row()) – Eric Hulser Aug 20 '12 at 17:38
  • it gives me an error, i dont believe my tab has the correct indexes, `AttributeError: 'QWidget' object has no attribute 'setCurrentIndex'` –  Aug 20 '12 at 17:47
  • 2
    You're setting it on the wrong widget - QWidget's don't have that property. I'm assuming if you are trying to set a tab, then somewhere you have a Tab Widget - that's where you have to set the index value. http://qt-project.org/doc/qt-4.8/qtabwidget.html No offense, but I've answered your question, you're now on a new question - I'd post that if you're still having trouble with more code examples and not keep pushing this one into a new topic. – Eric Hulser Aug 20 '12 at 18:21
  • When I executed the first example to add a button, I couldn't not set the button to row and column that I want. It was just in (0.0). Anyone help?? – Kim Mar 26 '18 at 01:04
  • @EricHulser I used your method, integrated with a for loop (Which is working fine and is used to load all data from a json file). But button gets added to only the last row? i don't know what the problem is, because the loading of data is working fine, so that means the for loop is not at fault. Can you help ? – Mehul May 16 '21 at 08:12
2

In PyQt4 add button to qtablewidget :

btn= QtGui.QPushButton('Hello')
qtable_name.setCellWidget(0,0, btn) # qtable_name is your qtablewidget name