Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QHBoxLayout does not resize when a child does, also other children keep their positions



  • Hi,
    I want to put 2 or 3 labels inside a QHBoxLayout, then resize the first label using an animation.
    I succeeded to do that, but sadly not as I expected.
    The problem is that the layout doesn't auto resize to content when I change the first label width. Also, the other labels are not pushed or shifted to right.

    This is the result (Failed result):
    Qt 01.gif
    Qt 02.gif

    I can do that in Java using JavaFX quite easily. So, I get that (Expected result):
    JavaFX 01.gif
    JavaFX 02.gif

    This is my animation code:

    QPropertyAnimation *animation = new QPropertyAnimation(ui->label_1, "size"); 
    animation->setDuration(10000);
    animation->setStartValue(QSize(55, ui->label_1->size().height()));
    animation->setEndValue(QSize(600, ui->label_1->size().height()));
    animation->setEasingCurve(QEasingCurve::InCurve);
    animation->start();
    

    NB: I'm using:

    • Linux Mint
    • Qt 5.15
    • Qt ==> QHBoxLayout, QLabel
    • JavaFX ==> HBox, Label

    Thank you.


  • Lifetime Qt Champion

    @Doros Please show how you're using layouts.



  • Yes, please show more. I guess you are not using layouts in the right way.
    From what you posted, changing the "size" property of widgets in a layout is already wrong, that should be handled by the layout.


  • Lifetime Qt Champion

    @Doros As @Bonnie pointed out setting size of a widget which is in a layout is wrong. What you should do is to not to use layouts for this widgets, but size and position them manually.



  • @Bonnie
    @jsulm

    I found a convenient solution, that I'm testing.
    I handled the size of widgets by using strech property to one of them, then I put the layout it self inside an empty widget to let me to set width, because layout doesn't have a resizing property.

    Sans nom 1.jpg

    I'm struggling now to continue my application.

    Thanks both of you.


Log in to reply