为什么文本在ComboBox视图中与其图标重叠?

时间:2014-09-17 15:59:00

标签: python qt pyside model-view

我正在学习使用PySide的视图模型设计:

##start
# python 3, simple view-model example with Qt

from PySide import QtGui, QtCore
import sys

# QAbstractListModel is an abstract class, it must be subclassed
class PaletteListModel(QtCore.QAbstractListModel):
    def __init__(self, colors=[]):
        super().__init__()
        self.__colors = colors

    # rowCount must be implemented
    # here we just return the number of elements in self.__colors
    def rowCount(self, *args, **kwargs):
        return len(self.__colors)

    # data must be implemented
    # index tells you the position of the selected item
    # role tells you in what way the data is going to be used
    def data(self, index, role):
        row = index.row()
        value = self.__colors[row]

        if role == QtCore.Qt.DecorationRole:
            pixmap = QtGui.QPixmap(10, 10)
            pixmap.fill(value)
            icon = QtGui.QIcon(pixmap)
            return icon

        if role == QtCore.Qt.DisplayRole:
            return value.name()

# Use our new model
class Md(QtGui.QDialog):
    def __init__(self):
        super().__init__()
        self.red = QtGui.QColor(155, 0, 0)
        self.green = QtGui.QColor(0, 155, 0)
        self.blue = QtGui.QColor(0, 0, 155)
        self.model = PaletteListModel([self.red, self.green, self.blue])

        self.listview = QtGui.QListView()
        self.listview.setModel(self.model)

        self.combo = QtGui.QComboBox()
        self.combo.setModel(self.model)

        self.tab = QtGui.QTableView()
        self.tab.setModel(self.model)

        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.listview)
        layout.addWidget(self.combo)
        layout.addWidget(self.tab)
        self.setLayout(layout)



app = QtGui.QApplication(sys.argv)
md = Md()
md.show()
sys.exit(app.exec_())

启动应用程序,我得到了:

enter image description here

看起来不错,但是当我点击组合框时,我得到了:

enter image description here

正如您所看到的,文字与图标重叠,为什么会发生这种情况以及如何纠正?

0 个答案:

没有答案