Important: Please read the Qt Code of Conduct -

QDialog auto fit content

  • Hi everyone,

    I have a Dialog with a QListWidget and two buttons.
    As I add dynamically the QListWidgetItem, I would like that the window adapts in real time to the size of what I post.
    it's possible to do it?
    The QListWidgetItem are simple texts with a small icon and Dialog is created with QtCreator.

    I have already tried to put SizePolicy Exspandig, or use adjusteSize in the code, or resize, but nothing.
    Thank you

  • Lifetime Qt Champion

    @Gianluca86 That would be quite unusual design! Why should the whole dialog resize if you put more values in the list? The list shows a scrollbar if not all values are visible. It should be up to the user to resize the dialog if he/she wants to.
    At least I wouldn't like such UI (and never seen so far)...

  • It may be possible but difficult/clunky (that is maybe why no one answer to...) and i think a strange design.
    Because when your list will grow, at the same time the QDialog bow will have his height grow, and when his height will become bigger than the screen height, so... you will loose the access of the full QDialog box.
    I think a "good" idea is maybe to set a default minimumSize for the QListWidget, and then if more items will be include, automatically it should have a scroll vertical bar that appears.
    If you really want to adapt size of the QDialogBox you have to link event of new item added to a slot you made for resize the QDialogBox window (somethink like Height-of-Window = the items number*height/item-height-size).
    Like that, each event time of add/remove item in your list, the slot linked with member function for resize the dialog box window will be call.
    Look at QObject::connect in the Qt5 API for learn how to connect SIGNAL with SLOT... a SLOT is a member function you can link with signal, a signal is... a signal emit from event (or you can call signal yourself from "emit" function), this is the concept mechanism with Qt to make things dynamic.

  • I get it. Thanks so much