PDA

View Full Version : How to Add Text to Editor in QTableView



klahey
2nd January 2010, 08:08
Hi,

I've created an app that uses QTableView to display data (version 4.5.x). It is connected to a class derived from QAbstractTableModel. It saves and displays data, allows the data to be edited, uses signals and slots, etc. Everything works fine, but I noticed that if you double click on a cell the editor will start, but any data that was in the cell is gone. It is still in the model until you save it, but the cell itself is blank.

I thought it would be nice to display the current data in the editor so if you only wanted to change it a little you wouldn't have to retype everything. But I can not figure out how to do this.

I added a signal function to pick up the double click on the table cell, got the data from the model and... have no way to enter it into the editor so it is visible. I can find no way to set the value of the cell.

I thought I would have to use a custom delegate, but I can't figure out how that would work. setEditorData sounded like it could help, but no. It is not designed to help me with this task.

Just to be clear: I have no trouble typing in text and saving it and displaying it later. I just want to double click on a cell and have the current text be displayed in the editor before I start making changes.

I hope someone can help me figure out how to do this, because I don't have any idea.

Thanks

psih128
2nd January 2010, 12:53
Does your model return data for Qt::EditRole? This data is used in editors as the default when you start edition. Just return the same what you return for Qt::DisplayRole and it will work fine.

klahey
2nd January 2010, 16:05
Of course. Thank you.