QComboBox 和 app.setStyle(“cleanlooks") [英] QComboBox and app.setStyle("cleanlooks")
问题描述
下面的代码创建了一个带有 QAbstractTableModel
模型的单个 QComboBox
.奇怪的是,如果 app.setStyle("cleanlooks")
被注释掉,QCombo 在点击时不会拉下它的菜单.有什么建议为什么会这样吗?
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_())
推荐答案
在 linux (ubuntu 14.04 lts) 上,您的代码适用于两种情况.在我的 Windows 7 上,即使 app.setStyle("cleanlooks")
没有被注释掉,它在任何情况下也不起作用.
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.
由于 QCombobox 只显示一维列表而没有显示二维表,我想问题是由二维表模型或其索引引起的.
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.
我尝试了 QstandardItemModel,它适用于 linux 和 windows 7.它允许按用户角色访问项目中的更多列,添加了第三列以显示它.
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_())
这篇关于QComboBox 和 app.setStyle(“cleanlooks")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!