問題描述
在 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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!