我试图在QLineEdit中复制一个样式表,特别是在QCompleter的弹出窗口中。
在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_()
最佳答案
必须使用委托:
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/