Search code examples
pyqtpopuppysideword-wrapqcombobox

How to add Multiline text(textWrap) items in QComboBox


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


Solution

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