2

我尝试在 QCompleter 的弹出窗口中以特定的方式在 QLineEdit 中应用样式表。

在 QtDesigner 中尝试:

在此处输入图像描述

代码:

QLineEdit#lineEdit::popup{
background:red;
}

但它不起作用

我正在寻找的是更改字母的颜色、背景颜色和出现在带有建议的框中的字母的对齐方式

也试试 QtDesigner

QAbstractItemView {}
QAbstractItemView :: item {}

更改 QLineEdit 中显示的建议列表的视觉属性,但它们不起作用

在我的代码中尝试:

from PyQt5.QtWidgets import QMainWindow,QApplication, QCompleter
from PyQt5 import QtCore
from PyQt5 import uic



class Pricnipal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("uno.ui",self)

        completer = QCompleter()

        self.lineEdit.setCompleter(completer)
        model = QtCore.QStringListModel()
        completer.setModel(model)
        self.get_data(model)
    def get_data(self,model):
        model.setStringList(["uno","dos","tres","cuatro","este es mi nombre"])


app  = QApplication([])
p = Pricnipal()
p.show()
app.exec_()
4

1 回答 1

4

您必须使用委托:

from PyQt5 import QtCore, QtGui, QtWidgets, uic

class CompleterDelegate(QtWidgets.QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super(CompleterDelegate, self).initStyleOption(option, index)
        option.backgroundBrush = QtGui.QColor("red")
        option.palette.setBrush(QtGui.QPalette.Text, QtGui.QColor("blue"))
        option.displayAlignment = QtCore.Qt.AlignCenter

class Principal(QtWidgets.QMainWindow):
    def __init__(self):
        super(Principal, self).__init__()
        uic.loadUi("uno.ui",self)
        completer = QtWidgets.QCompleter(self)
        self.lineEdit.setCompleter(completer)
        model = QtCore.QStringListModel()
        completer.setModel(model)
        delegate = CompleterDelegate(self.lineEdit)
        completer.popup().setStyleSheet("background-color:red;")
        completer.popup().setItemDelegate(delegate)
        self.get_data(model)

    def get_data(self,model):
        model.setStringList(["uno","dos","tres","cuatro","este es mi nombre"])

if __name__ == '__main__':
    import sys
    app  = QtWidgets.QApplication(sys.argv)
    p = Principal()
    p.show()
    sys.exit(app.exec_())
于 2018-12-08T00:13:30.633 回答