Important: Please read the Qt Code of Conduct -

[PySide6] How to shorten a Qlabel containing text when resizing?

  • I've built an interface where I have a QLabel which contains a list of selected files by the user.
    When the user selects some files, I build a string with all the names of the files, then I pass the string to a function to elide it if necessary to prevent the label to resize the main window and then I print the string to the label.
    When I drag the right side of the window to widen it, the label resizes accordingly and the text is updated to fit the new width of the label.
    The problem comes when I shorten the window, in that case, the label prevents the resizing because of the text I suppose.
    In QMainWindow's resizeEvent() I set the label text to an empty string and then I print it back, I thought this could work, but it doesn't, the window shrinks a bit, then it doesn't anymore, I have to stop the dragging and to start it again to be able to shrink a few more pixels and so on.

    How can I fix this problem?

    You can read the full code here:

    Involved code is inside these methods
    where the text is set for the first time

    resizeEvent(self, event)
    which should continuously resize the label and elide its content accordingly

    elideText(self, label, text)
    which generates the elided text

    If you want to test it, create some new empty text files with a long name and .PGR extension, then load them using the bottom left button.

  • I've solved setting the Horizontal Policy to "ignore".

  • I've solved setting the Horizontal Policy to "ignore".

Log in to reply