I know there has been a question with the same goal in C++, but I didn't succeed implementing a button delegate in a treeview. So I ask a new question here, even if I know there was a previous question with the same goal. I just ask for further explanation.
So since a few days, I manage to implement a multi-node tree model within a treeview thanks to this video tutorial with the code source here and here but now I would like to add a delegate pushbutton next to each child of the last level of the tree model. After consulting Qt documentation about model/view programming, searching around and reading chapter 14, 15 and 16 from the book of Mark Summerfield Rapid GUI Programming with Python and QT and searching how to add Qwidget delegate by using createEditor()
, setEditordata()
and setModelData()
I still struggle to display "The" button…
So I started to create the button class delegate:
class ButtonDelegate(QtGui.QItemDelegate):
"""
A delegate that places a fully functioning Button in every
cell of the column to which it's applied
"""
def __init__(self, parent):
QtGui.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
button = QtGui.QPushButton("Simple button", parent)
self.connect(button, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("handle_button()"))
return button
@QtCore.pyqtSlot()
def handle_button(self):
sender = self.sender()
print("Button pushed: {}".format(self.sender()))
Here you see that I didn't implement setEditordata()
and setModelData()
, because I have connected the button to def handle_button(self)
and I just want to print the name of the child for the moment (just to make it simple).
I use setItemDelegateForColumn(int, AbstractItemDelegate)
for setting up the delegate I also know that for displaying the button you have to specify this method to the treeview QAbstractItemView.openPersistentEditor(QModelIndex)
. But I don't know what to put as argument...
Again I am sorry to "repost" a question but at the moment I am in a dead end...I don't know where to start.
I write my code in Python, but if you answer in C++ I will figure it out, don't worry.
Thanks again for the help.