PDA

View Full Version : Tableview dialog as delegate



drmacro
25th October 2015, 18:05
I am trying to create a button delegate in the first column of a tableview that launches a dialog to edit the data in the row of the button.

I get the delegate button. But, since setEditorData is not called by the clicked event of the button, I don't see how to get the data to fill in on the dialog.

I can't see that the clicked event has access to the index or the model.

Can anyone give me some tips or point me to an example?

All the examples I've found are either just creating a button delegate or a spin box for a particular cell of the table.

Regards,
Mac

anda_skoa
26th October 2015, 08:15
Please verify that setEditorData() is not called at all, it should be called when the cell becomes editable and when the cell contents change while it is editable.

Cheers,
_

drmacro
26th October 2015, 16:50
Well I have print statements in setEditorData(). They only print when each of the delegate buttons gets created.

But, not when I click the button.

anda_skoa
26th October 2015, 19:28
Why would the setEditorData() method be called when you click the button?

Clicking the button doesn't change anything in the model, does it?
The data didn't change since the last time the delegate was informed about data change.
So why would the view need to call setEditorData() again?

How does your slot look like that you have connected to the button's clicked() signal?

Cheers,
_

drmacro
27th October 2015, 23:00
Don't know why my earlier reply didn't show up...:(

The clicked() code is like this at the moment.

This launches the dialog, but, I don't know how to get the row data from the delegate button.


def currentIndexChanged(self):
sndr = self.sender()
print("Button Clicked: " + str(sndr.text()))

self.editcuedlg = EditCue(str(sndr.text()))
self.editcuedlg.exec_()
#self.commitData.emit(self.sender())


Regards,
Mac

Added after 42 minutes:

I'm having a hard time getting the big picture of how the model/view/delegate process works...that may be most of my problem. :)

In the Qt doc example of delegates it says: "The createEditor() function is called when the user starts editing an item..."

I build the gui with:


if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()
ex.disptext()
sys.exit(app.exec_())


ex.disptext() does this:


def disptext(self):
self.get_table_data()
# set the table model
header = ['','Cue Number', 'Act', 'Scene', 'Page', 'ID', 'Title','Dialog/Prompt']
tablemodel = MyTableModel(self.tabledata, header, self.tv)
self.tv.setModel(tablemodel)
self.tv.resizeColumnsToContents()
for row in range(0, tablemodel.rowCount(tablemodel)):
self.tv.openPersistentEditor(tablemodel.index(row, 0))


So if if there are 106 rows in the data, createEditor() in the delegate code gets called 106 times, but not later when the user clicks the button delegate.:


class theDelegate(QStyledItemDelegate):
"""
"""
def __init__(self, parent):
# The parent is not an optional argument for the delegate as
# we need to reference it in the paint method (see below)
QStyledItemDelegate.__init__(self, parent)


def createEditor(self, parent, option, index):
editDlg = QPushButton("x" + str(index.row()), parent)
editDlg.blockSignals(True)
editDlg.clicked.connect(self.rowButtonClicked)
editDlg.blockSignals(False)
print("createEditor, index = " + str(index.row()))
return editDlg


Regards,
Mac

anda_skoa
28th October 2015, 07:45
Both in createEditor() as well as in setModelData() you get the model index of the cell.
Since you are only interested in the row, you just store the row, e.g. using setProperty() on the button or a QSignalMapper
The you either get the data for the row when you open the dialog or you pass the row and the model to the dialog and let it retrieve the data itself.

Cheers,
_