Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. [PyQt5 5.15] QListWidgetItem.setSizeHint not working
Forum Updated to NodeBB v4.3 + New Features

[PyQt5 5.15] QListWidgetItem.setSizeHint not working

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 4 Posters 2.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    flashk
    wrote on last edited by
    #1

    I just updated from PyQT5 5.14.2 to 5.15 and setting the height of list items using QListWidgetItem.setSizeHint appears to have stopped working. Here is a simple example that worked in 5.14.2 and does not work with 5.15:

    import sys
    from PyQt5.QtWidgets import QApplication, QListWidget, QListWidgetItem, QMainWindow, QLabel
    from PyQt5.QtCore import QSize
    
    class MainWindow(QMainWindow):
    
    	def __init__(self, *args, **kwargs):
    		super(MainWindow, self).__init__(*args, **kwargs)
    		self.setWindowTitle("List Widget Sample")
    
    		listWidget = QListWidget()
    		self.setCentralWidget(listWidget)
    
    		for x in range(5):
    			item = QListWidgetItem('item {}'.format(x+1))
    			item.setSizeHint(QSize(-1, 100))
    			listWidget.addItem(item)
    
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()
    

    Am I using the setSizeHint() method incorrectly or could this be a bug in the latest version? Is there a better way to set the height of list widget items?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      I find a negative value for a size hint surprising.

      Can you add a picture what you had before and now ?
      By the way, which OS are you on ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • F Offline
        F Offline
        flashk
        wrote on last edited by
        #3

        Hi, thanks for the reply.

        I'm running on Windows 10.

        Here is a before/after photo:

        list_widget_size_hint.png

        The default values for QListWidgetItem.sizeHint() is (-1, -1), so I assumed they are valid values. I just tried changing it to (0, 100), and it appears to work now. So I take it negative values are invalid, even though they are the default values? If I only want to apply the size hint to the height and keep the width stretched, is specifying 0 for the width considered the correct approach?

        JonBJ 1 Reply Last reply
        0
        • F flashk

          Hi, thanks for the reply.

          I'm running on Windows 10.

          Here is a before/after photo:

          list_widget_size_hint.png

          The default values for QListWidgetItem.sizeHint() is (-1, -1), so I assumed they are valid values. I just tried changing it to (0, 100), and it appears to work now. So I take it negative values are invalid, even though they are the default values? If I only want to apply the size hint to the height and keep the width stretched, is specifying 0 for the width considered the correct approach?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @flashk
          https://doc.qt.io/qt-5/qsize.html#QSize

          bool QSize::isValid() const

          Returns true if both the width and height is equal to or greater than 0; otherwise returns false.

          So technically QSize(-1, 100) could be treated as invalid. Perhaps behaviour now reflects this at 5.15.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            The default -1, -1 is explained in the constructor documentation. By default you create an invalid QSize if giving no parameters.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • F Offline
              F Offline
              flashk
              wrote on last edited by
              #6

              Great, thank you for the clarification!

              1 Reply Last reply
              0
              • jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote on last edited by
                #7

                The behavior is indeed new with 5.15.

                The change makes it possible to reset the background, foreground, or size hint by passing a default constructed QBrush or QSize.

                https://github.com/qt/qtbase/commit/96c27eb710a37780e79e09df2ebce1d5e4922c9d

                Asking a question about code? http://eel.is/iso-c++/testcase/

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved