在PyQt中设置QCompleter的弹出窗口样式

时间:2011-11-18 17:58:22

标签: python qt4 pyqt styling

是否可以将样式表应用于与QCombobox绑定的QCompleter的弹出窗口部分?如果没有,它是否需要委托魔法?如果是这样的话,那甚至可能如何工作,往往会让我感到困惑。这是我的小部件代码:

class autoFillField(QComboBox):
    def __init__(self, parent=None):
        super(autoFillField, self).__init__(parent)

        self.setFocusPolicy(Qt.NoFocus)
        self.setEditable(True)

        self.addItem("")

        self.pFilterModel = QSortFilterProxyModel(self)
        self.pFilterModel.setFilterCaseSensitivity(Qt.CaseInsensitive)
        self.pFilterModel.setSourceModel(self.model())

        self.completer = QCompleter(self.pFilterModel, self)
        self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
        self.setCompleter(self.completer)
        self.setStyleSheet(STYLING FOR COMBOBOX HERE, BUT NOT POPUP)

        self.lineEdit().textEdited[unicode].connect(self.pFilterModel.setFilterFixedString)

    def on_completer_activated(self, text):
        if text:
            index = self.findText(text)
            self.setCurrentIndex(index)

    def setModel(self, model):
        super(autoFillField, self).setModel(model)
        self.pFilterModel.setSourceModel(model)
        self.completer.setModel(self.pFilterModel)

    def setModelColumn(self, column):
        self.completer.setCompletionColumn(column)
        self.pFilterModel.setFilterKeyColumn(column)
        super(autoFillField, self).setModelColumn(column)

弹出式样式是否会发生在组合框类中,还是会发生在通过addItems输入数据的外部?提前谢谢。

1 个答案:

答案 0 :(得分:15)

设置popup of the completer的样式表,它将是一个QListView对象。这是一个可运行的示例(弹出窗口的背景应为黄色):

#!/usr/bin/python

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)

w = QtGui.QComboBox()
w.setEditable(True)
c = QtGui.QCompleter(['Hello', 'World'])
c.setCompletionMode(QtGui.QCompleter.UnfilteredPopupCompletion)
c.popup().setStyleSheet("background-color: yellow")
w.setCompleter(c)
w.show()

sys.exit(app.exec_())