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

时间:2018-10-22 09:08:16

标签: python pyqt

我在QComboBox中有长文本项,并且我想在多行中显示项的完整文本。我该怎么办。谢谢。目前,它在文本的开头和结尾之间放置了...

enter image description here

1 个答案:

答案 0 :(得分: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