Python 如何使用QComboBox.setPlaceholder文本?

Python 如何使用QComboBox.setPlaceholder文本?,python,pyqt,pyqt5,qcombobox,Python,Pyqt,Pyqt5,Qcombobox,在Qt5.15中,引入了占位符文本属性- 但是使用setplaceholder text对我没有任何帮助。运行下面的代码时,QComboBox中没有任何文本(当然,除非我从三项中选择一项) 这是一个错误还是我遗漏了什么?我怎样才能做到这一点 import sys from PyQt5 import QtWidgets from PyQt5 import QtCore class MainWindow(QtWidgets.QMainWindow): def __init__(self

在Qt5.15中,引入了
占位符文本
属性-

但是使用
setplaceholder text
对我没有任何帮助。运行下面的代码时,
QComboBox
中没有任何文本(当然,除非我从三项中选择一项)

这是一个错误还是我遗漏了什么?我怎样才能做到这一点

import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        central_w = QtWidgets.QWidget()
        self.setCentralWidget(central_w)
        vbox = QtWidgets.QVBoxLayout()
        central_w.setLayout(vbox)

        self.combo_box = QtWidgets.QComboBox()
        self.combo_box.addItems(["one", "two", "three"])
        self.combo_box.setPlaceholderText("Some placeholder text here")
        self.combo_box.setCurrentIndex(-1)
        vbox.addWidget(self.combo_box)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    app.exec_()
我找不到显示占位符文本的方法。我尝试过在组合框中没有任何项目,但即使这样也不会显示占位符文本


以下是我正在运行的版本:

  • Qt:5.15.2
  • PyQt(Python模块)版本:5.15.2(与Qt版本相同,但有时可能略有不同)
  • Python:“3.8.5(默认值,2020年7月28日,12:59:40)\n[GCC 9.3.0]”
  • 操作系统:Ubuntu 20.04.1 LTS(带Xfce)


PS:如果你想达到类似的效果(即:在可点击区域上有一个文本,点击后显示一个带有不同选项的下拉列表),你可以使用
QPushButton
setMenu
功能。文档:

查看对Qt源代码的修改,发现要在Qt 5.15.0中添加占位符文本的功能,currentText()已被修改:

//https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qcombobox.cpp?h=5.15.0#n2344
QString QComboBox::currentText()常量
{
Q_D(常数QCOMBOX);
如果(d->lineEdit)
返回d->lineEdit->text();
else如果(d->currentIndex.isValid())
返回d->itemText(d->currentIndex);
其他的
返回d->占位符文本;
}
但这会产生中报告的不良效果,因此功能在Qt 5.15.2中被删除:

QString QComboBox::currentText()常量
{
Q_D(常数QCOMBOX);
如果(d->lineEdit)
返回d->lineEdit->text();
如果(d->currentIndex.isValid())
返回d->itemText(d->currentIndex);
返回{};
}
忘记纠正占位符不再可见的副作用。我已经报告了错误:

考虑到上述情况,有以下备选方案:

  • 使用PyQt5/PySide2 5.15.0或5.15.1

  • 重写paintEvent方法以将占位符文本设置为currentText:

导入系统 从PyQt5导入QtCore、QtGui、QtWidgets #或 #从PySide2导入QtCore、QtGui、QtWidgets 类组合框(qtwidts.QComboBox): # https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qcombobox.cpp?h=5.15.2#n3173 def paintEvent(自身,事件): painter=qtwidts.QStylePainter(self) painter.setPen(self.palete().color(QtGui.qpalete.Text)) #绘制组合框框架、focusrect和selected等。 opt=qtwidts.QStyleOptionComboBox() self.initStyleOption(opt) painter.drawComplexControl(qtwidts.QStyle.CC_组合框,可选) 如果self.currentIndex()小于0: 选项板( QtGui.qpalete.ButtonText, opt.palete.brush(QtGui.qpalete.ButtonText).color().lighter(), ) 如果self.placeholder text(): opt.currentText=self.placeholderText() #绘制图标和文本 painter.drawControl(qtwidts.QStyle.CE_ComboBoxLabel,可选) 类MainWindow(QtWidgets.QMainWindow): 定义初始化(自): super()。\uuuu init\uuuuu() self.combo\u box=ComboBox() self.combo_box.addItems([“一”、“二”、“三”]) self.combo\u box.setplaceholder文本(“此处的一些占位符文本”) self.combo_box.setCurrentIndex(-1) central_w=qtwidts.QWidget() self.setCentralWidget(中央) vbox=qtwidts.QVBoxLayout(中心) vbox.addWidget(self.combo_框) 如果名称=“\uuuuu main\uuuuuuuu”: app=qtwidts.QApplication(sys.argv) 主窗口=主窗口() 主窗口。显示() app.exec()
print(app.style().objectName())的输出是什么?如果不是“fusion”,请尝试
app.setStyle(qtwidts.QStyleFactory.create('fusion'))
@musicamante我得到了
app.style().objectName()='fusion'
。顺便说一句,我不知道这是否重要,但我正在运行Xfce(我会在问题中更新这个)嗯。您可以尝试其他可用的样式吗?您可以看到静态
QStyleFactory.keys()
的完整列表。此外,您可以通过显式设置
placeholder text
角色(确保其具有足够的对比度且没有alpha)来尝试更改调色板吗?尝试
PySide2
而不是
PyQt5
。我刚刚在Windows上测试了它,它运行正常。