Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling the popup of a QCompleter in PyQt

Is is possible to apply a stylesheet to the popup portion of a QCompleter tied to a QCombobox? If not, does it require delegate magic? If so, how might that even work as they do tend to confuse the hell out of me. Here is my widget code:

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)

Would the popup styling take place in the combobox class, or would it happen outside of it where the data is input via addItems? Thanks in advance.

like image 765
Cryptite Avatar asked Nov 18 '11 17:11

Cryptite


1 Answers

Set the stylesheet of the popup of the completer, which will be a QListView object. Here is a runnable example (the background of the popup should be yellow):

#!/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_())
like image 161
Gary van der Merwe Avatar answered Sep 21 '22 13:09

Gary van der Merwe