3

I have long text items in QComboBox, and I want to display complete text of items in multiple lines. What should I do. Thank you. Currently it puts ... between the start and end of text.

enter image description here

Alexander Chernin
  • 446
  • 2
  • 8
  • 17
Oli
  • 1,313
  • 14
  • 31

2 Answers2

2

To get something like on the image:

word wrap image

I'll need QListView (with its method setWordWrap), QStringListModel (for example only, you can use any model) and QComboBox.

Example:

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_())
Alexander Chernin
  • 446
  • 2
  • 8
  • 17
0

Unfortunately, you have provided an example demonstrating the problem. You can implement your idea using the sizeAdjustPolicy andsizePolicy properties.

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

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • if you add `self.combo.setMaximumWidth(200)` after constructor of `QComboBox`, and you have item with much larger length, you will see `...` in text like I showed in the image. – Oli Oct 22 '18 at 14:03
  • `maximumWidth` - This property corresponds to the width held by the `maximumSize` property. `maximumSize` - The widget cannot be resized to a larger size than the maximum widget size. – S. Nick Oct 22 '18 at 15:39