So I have a model and one of the columns contains a country. However because I want to display a combo box to choose the country from a list of options, I don't store the country name in the model directly. Instead I store an index value into a list of allowable countries. This allows me to use a QComboBox in my form view as recommended in the Qt docs. The problem is I also have a table view, and the table view displays the index integer, not the country name. I have set up a QStyledItemDelegate and implemented createEditor so if you click in the world cell it does pop up the ComboBox, but when you're not editing the country you see the index value.
I'm part way to a solution. I have implemented a paint method to do the work, but it's displaying the value offset to it's proper position and I can't figure out how to get it to display correctly. I think option.rect.topLeft() in the render method is wrong, but I can't figure out how to set the drawing correctly. Any help appreciated, I do remember to up-tick helpful answers.
def paint(self, painter, option, index):
if index.column() == COUNTRY:
painter.save()
countryRef, ok = inex.data().toInt()
countryStr = country_list[countryRef]
widget = QLineEdit()
widget.setGeometry(option.rect)
widget.setText(countryStr)
widget.render(painter, option.rect.topLeft())
painter.store()
else:
QStylyedItemDelegate.paint(self, painter, option, index)