我工作的应用程序GUI需要一个组合框供用户选择项目。当应用程序启动时,组合框将显示类似“请选择”的提示文本,而不是显示组合框的第一项。我在http://doc.qt.io/qt-5/qcombobox.html#currentText-prop中找不到任何设置提示文字的方法。
提前感谢!
答案 0 :(得分:4)
如果QComboBox
为editable
,则an elegant solution:
myQComboBox->lineEdit()->setPlaceHolderText("Please select");
QComboBox
不是editable
其中没有QLineEdit
个,所以这对那些不起作用。
答案 1 :(得分:1)
无法为QComboBox设置占位符文本。但是你可以解决这个问题。使用setEditText( const QString& )
插槽设置文字。如果用户在comboBox中选择一个项目,则将设置项目的文本。但是,如果用户选择文本,删除它,并选择其他控制元素(组合框失去其焦点),您的文本将不再存在。可以通过继承QComboBox
并重新实现focusOutEvent(...)
来解决问题:if ( currentIndex() == -1 ) setEditText( tr( "Please select" ) );
。别忘了先叫QComboBox::focusOutEvent(...)
。
答案 2 :(得分:1)
对于较新版本的Qt,请尝试QComboBox::setPlaceholderText()。
我碰巧在Raspberry Pi上使用旧版本的Qt(5.11.3),并且需要其他解决方案。
这是一个有效的pyqt5示例,它使用proxy model来调整添加为占位符的多余项。 (贷记this answer):
import sys
from PyQt5.QtCore import Qt, QT_VERSION_STR, QAbstractProxyModel, QModelIndex, QItemSelection
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import (QApplication, QGridLayout, QWidget, QComboBox)
from typing import Any
class Main(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(50,50,320,200)
self.setWindowTitle(f"Qt Version {QT_VERSION_STR}")
layout = QGridLayout()
cmbox = QComboBox()
model = QStandardItemModel()
for i in range(1, 11):
model.appendRow(QStandardItem(f"Item {i}"))
cmbox.setModel(ProxyModel(model, '---PlaceholderText---'))
cmbox.setCurrentIndex(0)
layout.addWidget(cmbox, 0, 0)
self.setLayout(layout)
self.show()
class ProxyModel(QAbstractProxyModel):
def __init__(self, model, placeholderText='---', parent=None):
super().__init__(parent)
self._placeholderText = placeholderText
self.setSourceModel(model)
def index(self, row: int, column: int, parent: QModelIndex = ...) -> QModelIndex:
return self.createIndex(row, column)
def parent(self, index: QModelIndex = ...) -> QModelIndex:
return QModelIndex()
def rowCount(self, parent: QModelIndex = ...) -> int:
return self.sourceModel().rowCount()+1 if self.sourceModel() else 0
def columnCount(self, parent: QModelIndex = ...) -> int:
return self.sourceModel().columnCount() if self.sourceModel() else 0
def data(self, index: QModelIndex, role: int = Qt.DisplayRole) -> Any:
if index.row() == 0 and role == Qt.DisplayRole:
return self._placeholderText
elif index.row() == 0 and role == Qt.EditRole:
return None
else:
return super().data(index, role)
def mapFromSource(self, sourceIndex: QModelIndex):
return self.index(sourceIndex.row()+1, sourceIndex.column())
def mapToSource(self, proxyIndex: QModelIndex):
return self.sourceModel().index(proxyIndex.row()-1, proxyIndex.column())
def mapSelectionFromSource(self, sourceSelection: QItemSelection):
return super().mapSelection(sourceSelection)
def mapSelectionToSource(self, proxySelection: QItemSelection):
return super().mapSelectionToSource(proxySelection)
def headerData(self, section: int, orientation: Qt.Orientation, role: int = Qt.DisplayRole):
if not self.sourceModel():
return None
if orientation == Qt.Vertical:
return self.sourceModel().headerData(section-1, orientation, role)
else:
return self.sourceModel().headerData(section, orientation, role)
def removeRows(self, row: int, count: int, parent: QModelIndex = ...) -> bool:
return self.sourceModel().removeRows(row, count -1)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
sys.exit(app.exec_())