如何在QComboBox中添加多行文本(textWrap)项目

3
我在QComboBox中有一些很长的文本项,我想要在多行中显示完整的文本项。我该怎么做?谢谢。目前它会在文本开头和结尾之间放置“...”。

enter image description here


我没有观察到那个问题,你可以展示一张图片。 - eyllanesc
2个回答

2
要实现如图所示的效果:

word wrap image

需要使用 QListView(通过其方法 setWordWrap)、QStringListModel(此处只是一个示例,您可以使用任何模型)和 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_())

0

很遗憾,您提供了一个展示问题的示例。您可以使用sizeAdjustPolicysizePolicy属性来实现您的想法。

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_())

enter image description here

enter image description here


如果在 QComboBox 的构造函数之后添加 self.combo.setMaximumWidth(200),并且您有一个项目长度超过 200 的选项,您将会在文本中看到像我展示的那样的 ... - Oli
maximumWidth - 此属性对应于 maximumSize 属性所持有的宽度。 maximumSize - 小部件不能被调整为比最大小部件尺寸更大的尺寸。 - S. Nick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接