Reputation: 19339
The code below creates a single QComboBox
with QAbstractTableModel
model assigned. Strange, if app.setStyle("cleanlooks")
is commented out the QCombo does not pull its menu down when it is clicked. Any suggestion why that is happening?
from PyQt import QtGui, QtCore
class tableModel(QtCore.QAbstractTableModel):
def __init__(self, parent=None, *args):
QtCore.QAbstractTableModel.__init__(self, parent, *args)
self.items = [['Item_A000', '10'],['Item_B001', '20'],['Item_A002', '30'],['Item_B003', '40'],['Item_B004', '50']]
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.items)
def columnCount(self, parent=QtCore.QModelIndex()):
return 2
def data(self, index, role):
if not index.isValid(): return
row=index.row()
column=index.column()
return self.items[row][column]
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
# app.setStyle("cleanlooks")
tModel=tableModel()
combobox = QtGui.QComboBox()
combobox.setModel(tModel)
combobox.show()
sys.exit(app.exec_())
Upvotes: 2
Views: 706
Reputation: 3477
This sounds odd, but your problem is because your model data
method is returning incorrect values for other roles than DisplayRole
. Probably DecorationRole
but I'm not sure - you'd need to do some more testing to find out for sure but in any case your code is incorrect.
You need to change your data method first line test to this:
def data(self, index, role):
if not index.isValid() or role != QtCore.Qt.DisplayRole: return
I think you will find that fixes your immediate problem. Presumably it is working with CleanLooks because the decoration is not used, or handled differently.
Upvotes: 1
Reputation: 4286
On linux (ubuntu 14.04 lts) your code works in both cases. On my windows 7 it doesn't work in any case even when app.setStyle("cleanlooks")
is not commented out.
As QCombobox presents only 1-dimensional lists and no 2-dimensional tables, i suppose, the problem is caused by the 2-dimensional tablemodel or its index.
I tried QstandardItemModel and it works on linux as well as on windows 7. It gives access to further columns in items by userroles, third column added to show it.
class tableModel(QtGui.QStandardItemModel):
def __init__(self, parent=None, *args):
QtGui.QStandardItemModel.__init__(self, parent, *args)
self.items = [['Item_A000', '10','abcd'],['Item_B001', '20','efgh'],['Item_A002', '30','ijkl'],['Item_B003', '40','mnop'],['Item_B004', '50','qrst']]
for i in range(0,len(self.items)):
item = QtGui.QStandardItem()
item.setData(self.items[i][0],2) # displayrole
item.setData(self.items[i][1],256) # userrole
item.setData(self.items[i][2],257) # userrole
self.appendRow(item)
def currentChanged(self, index):
print('itemdata[0] :', self.data(self.index(index,0),2), '; itemdata[1] :', self.data(self.index(index,0), 256), '; itemdata[2]: ', self.data(self.index(index,0),257))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
# app.setStyle("cleanlooks")
tModel=tableModel()
combobox = QtGui.QComboBox() # widget)
combobox.setModel(tModel
combobox.currentIndexChanged.connect(combobox.model().currentChanged)
combobox.show()
sys.exit(app.exec_())
Upvotes: 2