How do i use QComboBox.setPlaceholderText?(如何使用 QComboBox.setPlaceholderText?)
问题描述
在 Qt 5.15 中引入了 placeholderText
属性 -
这是一个错误还是我错过了什么?我怎样才能做到这一点?
导入系统从 PyQt5 导入 QtWidgets从 PyQt5 导入 QtCore类 MainWindow(QtWidgets.QMainWindow):def __init__(self):超级().__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([一"、二"、三"])self.combo_box.setPlaceholderText("这里有一些占位符文本")self.combo_box.setCurrentIndex(-1)vbox.addWidget(self.combo_box)如果 __name__ == __main__":应用程序 = QtWidgets.QApplication(sys.argv)主窗口 = 主窗口()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) [GCC 9.3.0]"
- 操作系统:Ubuntu 20.04.1 LTS(带 Xfce)
PS:如果你想达到类似的效果(即:在可点击区域上有一个文本,点击后显示带有不同选项的下拉菜单)你可以使用 QPushButton
setMenu
函数.文档:https://doc.qt.io/qt-5/qpushbutton.html#setMenu
查看Qt源码的修改,发现Qt 5.15.0增加placeHolderText的功能,修改了currentText():
//https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qcombobox.cpp?h=5.15.0#n2344QString QComboBox::currentText() 常量{Q_D(const QComboBox);if (d->lineEdit)return d->lineEdit->text();else if (d->currentIndex.isValid())返回 d->itemText(d->currentIndex);别的return d->placeholderText;}
但这会产生不良影响,QTBUG-86580在 Qt 5.15.2 中被删除:
QString QComboBox::currentText() const{Q_D(const QComboBox);if (d->lineEdit)return d->lineEdit->text();if (d->currentIndex.isValid())返回 d->itemText(d->currentIndex);返回 {};}
忘记纠正 placeHolder 不再可见的副作用.我已经报告了这个错误:QTBUG-90522.
考虑到上述情况,有以下备选方案:
使用 PyQt5/PySide2 5.15.0 或 5.15.1.
重写paintEvent方法将placeHolderText设置为currentText:
导入系统从 PyQt5 导入 QtCore、QtGui、QtWidgets# 或者# 从 PySide2 导入 QtCore、QtGui、QtWidgets类组合框(QtWidgets.QComboBox):# https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qcombobox.cpp?h=5.15.2#n3173defpaintEvent(自我,事件):画家 = QtWidgets.QStylePainter(self)Painter.setPen(self.palette().color(QtGui.QPalette.Text))# 绘制组合框框、focusrect 和 selected 等.opt = QtWidgets.QStyleOptionComboBox()self.initStyleOption(opt)Painter.drawComplexControl(QtWidgets.QStyle.CC_ComboBox,选择)如果 self.currentIndex() <0:opt.palette.setBrush(QtGui.QPalette.ButtonText,opt.palette.brush(QtGui.QPalette.ButtonText).color().lighter(),)如果 self.placeholderText():opt.currentText = self.placeholderText()# 绘制图标和文本Painter.drawControl(QtWidgets.QStyle.CE_ComboBoxLabel,选择)类 MainWindow(QtWidgets.QMainWindow):def __init__(self):超级().__init__()self.combo_box = 组合框()self.combo_box.addItems([一"、二"、三"])self.combo_box.setPlaceholderText("这里有一些占位符文本")self.combo_box.setCurrentIndex(-1)central_w = QtWidgets.QWidget()self.setCentralWidget(central_w)vbox = QtWidgets.QVBoxLayout(central_w)vbox.addWidget(self.combo_box)如果 __name__ == __main__":应用程序 = QtWidgets.QApplication(sys.argv)主窗口 = 主窗口()main_window.show()app.exec_()
In Qt 5.15 the placeholderText
property was introduced - link to documentation
However using setPlaceholderText
doesn't do anything for me. When running the code below i don't get any text in the QComboBox
(unless of course i select one of the three items)
Is this a bug or am i missing something? How can i make this work?
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_()
I have found no way of showing the placeholder text. I've tried having no items at all in the combo box but even this doesn't show the placeholder text
These are the versions i am running:
- Qt: 5.15.2
- PyQt (Python module) version: 5.15.2 (this happens to be the same as the Qt version but can sometimes differ slightly)
- Python: "3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]"
- OS: Ubuntu 20.04.1 LTS (with Xfce)
PS: If you want to achieve a similar effect (that is: having a text on a clickable area which shows a drop-down with different options after being clicked) you can use a QPushButton
with the setMenu
function. Documentation: https://doc.qt.io/qt-5/qpushbutton.html#setMenu
Reviewing the modifications of the Qt source code, it is observed that to add the functionality of the placeHolderText in Qt 5.15.0, the currentText() was modified:
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qcombobox.cpp?h=5.15.0#n2344
QString QComboBox::currentText() const
{
Q_D(const QComboBox);
if (d->lineEdit)
return d->lineEdit->text();
else if (d->currentIndex.isValid())
return d->itemText(d->currentIndex);
else
return d->placeholderText;
}
But that generates an undesirable effect that was reported in QTBUG-86580 so that functionality was removed in Qt 5.15.2:
QString QComboBox::currentText() const
{
Q_D(const QComboBox);
if (d->lineEdit)
return d->lineEdit->text();
if (d->currentIndex.isValid())
return d->itemText(d->currentIndex);
return {};
}
Forgetting to correct the side effect which is that the placeHolder is no longer visible. I have already reported the bug: QTBUG-90522.
Considering the above, there are the following alternatives:
Use PyQt5/PySide2 5.15.0 or 5.15.1.
Override the paintEvent method to set the placeHolderText as currentText:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
# or
# from PySide2 import QtCore, QtGui, QtWidgets
class ComboBox(QtWidgets.QComboBox):
# https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qcombobox.cpp?h=5.15.2#n3173
def paintEvent(self, event):
painter = QtWidgets.QStylePainter(self)
painter.setPen(self.palette().color(QtGui.QPalette.Text))
# draw the combobox frame, focusrect and selected etc.
opt = QtWidgets.QStyleOptionComboBox()
self.initStyleOption(opt)
painter.drawComplexControl(QtWidgets.QStyle.CC_ComboBox, opt)
if self.currentIndex() < 0:
opt.palette.setBrush(
QtGui.QPalette.ButtonText,
opt.palette.brush(QtGui.QPalette.ButtonText).color().lighter(),
)
if self.placeholderText():
opt.currentText = self.placeholderText()
# draw the icon and text
painter.drawControl(QtWidgets.QStyle.CE_ComboBoxLabel, opt)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.combo_box = ComboBox()
self.combo_box.addItems(["one", "two", "three"])
self.combo_box.setPlaceholderText("Some placeholder text here")
self.combo_box.setCurrentIndex(-1)
central_w = QtWidgets.QWidget()
self.setCentralWidget(central_w)
vbox = QtWidgets.QVBoxLayout(central_w)
vbox.addWidget(self.combo_box)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
app.exec_()
这篇关于如何使用 QComboBox.setPlaceholderText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 QComboBox.setPlaceholderText?
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01