如何在 QComboBox 中添加多行文本(textWrap)项目
How to add Multiline text(textWrap) items in QComboBox
我在 QComboBox
中有很长的文本项目,我想在多行中显示项目的完整文本。我应该怎么办。谢谢你。目前它将 ...
放在文本的开头和结尾之间。
很遗憾,您提供了一个示例来说明问题。
您可以使用 sizeAdjustPolicy
和 sizePolicy
属性实现您的想法。
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class Main(QtWidgets.QWidget):
def __init__(self):
super(Main, self).__init__()
sheets = [str(i) for i in ("item1",
"item2 item2",
"item3_item3_item3",
"The combobox will always adjust to the contents")]
self.combo = QtWidgets.QComboBox()
self.combo.addItems(sheets)
self.lineEdit = QtWidgets.QLineEdit("Here type a new value for the current setItemText")
self.combo.setSizeAdjustPolicy(self.combo.AdjustToContents)
self.combo.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
QtWidgets.QSizePolicy.Fixed)
self.shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return),
self.combo,
activated=self.onActivated)
layout = QtWidgets.QHBoxLayout()
layout.addWidget(QtWidgets.QLabel("ComboBox:"))
layout.addWidget(self.combo)
layout.addWidget(self.lineEdit)
self.setLayout(layout)
def onActivated(self):
index = self.combo.currentIndex()
self.combo.setEditable(True)
self.combo.setItemText(index, self.lineEdit.text())
self.combo.setEditable(False)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main = Main()
main.resize(600, 200)
main.show()
sys.exit(app.exec_())
要在图像上得到类似的东西:
我需要 QListView (with its method setWordWrap), QStringListModel (for example only, you can use any model) and QComboBox。
示例:
import sys
from PyQt5.QtWidget import QComboBox, QListView, QApplication
from PyQt5.QtCore import QStringListModel
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
combo = QComboBox()
combo.setMaximumWidth(150)
# For the popup items data we use QStringListModel
combo.setModel(QStringListModel([
'1. Lo',
'2. Lorem',
'3. Lorem ipsum dolor sit amet, consectetur',
'4. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut',
'5. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco labor'
]))
# The popup widget is QListView
listView = QListView()
# Turn On the word wrap
listView.setWordWrap(True)
# set popup view widget into the combo box
combo.setView(listView)
combo.show()
sys.exit(app.exec_())
我在 QComboBox
中有很长的文本项目,我想在多行中显示项目的完整文本。我应该怎么办。谢谢你。目前它将 ...
放在文本的开头和结尾之间。
很遗憾,您提供了一个示例来说明问题。
您可以使用 sizeAdjustPolicy
和 sizePolicy
属性实现您的想法。
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class Main(QtWidgets.QWidget):
def __init__(self):
super(Main, self).__init__()
sheets = [str(i) for i in ("item1",
"item2 item2",
"item3_item3_item3",
"The combobox will always adjust to the contents")]
self.combo = QtWidgets.QComboBox()
self.combo.addItems(sheets)
self.lineEdit = QtWidgets.QLineEdit("Here type a new value for the current setItemText")
self.combo.setSizeAdjustPolicy(self.combo.AdjustToContents)
self.combo.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
QtWidgets.QSizePolicy.Fixed)
self.shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return),
self.combo,
activated=self.onActivated)
layout = QtWidgets.QHBoxLayout()
layout.addWidget(QtWidgets.QLabel("ComboBox:"))
layout.addWidget(self.combo)
layout.addWidget(self.lineEdit)
self.setLayout(layout)
def onActivated(self):
index = self.combo.currentIndex()
self.combo.setEditable(True)
self.combo.setItemText(index, self.lineEdit.text())
self.combo.setEditable(False)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main = Main()
main.resize(600, 200)
main.show()
sys.exit(app.exec_())
要在图像上得到类似的东西:
我需要 QListView (with its method setWordWrap), QStringListModel (for example only, you can use any model) and QComboBox。
示例:
import sys
from PyQt5.QtWidget import QComboBox, QListView, QApplication
from PyQt5.QtCore import QStringListModel
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
combo = QComboBox()
combo.setMaximumWidth(150)
# For the popup items data we use QStringListModel
combo.setModel(QStringListModel([
'1. Lo',
'2. Lorem',
'3. Lorem ipsum dolor sit amet, consectetur',
'4. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut',
'5. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco labor'
]))
# The popup widget is QListView
listView = QListView()
# Turn On the word wrap
listView.setWordWrap(True)
# set popup view widget into the combo box
combo.setView(listView)
combo.show()
sys.exit(app.exec_())