gpt4 book ai didi

python - 改变QCompleter的风格?

转载 作者:行者123 更新时间:2023-11-28 22:13:54 27 4
gpt4 key购买 nike

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

在 QtDesigner 中尝试:

enter image description here

代码:

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_()

最佳答案

您必须使用委托(delegate):

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_())

关于python - 改变QCompleter的风格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53663091/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com