Qt World Summit: Submit your Presentation

How to reposition a widget when its layout changed

  • I have written a widget A containing among others an horizontal layout containing in turn a widget B. When a particular field of A is clicked, a widget C is created and added to the horizontal layout next to B. Widget A is correctly resized and displayed in this case. But I want to move A if it goes beyond the screen limits because of its new size.
    For that, I wrote a calculatePosition() function returning a QPoint which find a correct position for A. I pass this QPoint to A->move(). This runs well when I call the move() in a dedicated slot, for example onClicked() for a button.
    But what I want is that this repositioning automatically occurs after widget C is added to the layout. And I cannot achieve that!
    You suggestions are welcome.

  • I have to precise that calculatePosition() is based on A->sizeHint() and that this value is not still updated just after horizontalLayout->addWidget(C)...

  • Lifetime Qt Champion


    In that case, why not install an event filter and catch the show event from your widget C and only then do your manipulations ?

  • Hi SGaist,

    Sorry for ma late reply. You are right! I used showEvent of my widget C to send a signal and called move(calculatePosition()) to a dedicated slot. And it runs!
    Before posting my question, I tried to use showEvent of widget A, but it is not called when widget C appears.
    Thanks for your help!
    Have a good day.

  • Lifetime Qt Champion

    Because A and C are independent. The fact that C is shown as no influence on the show event of A.

    In any case, since you have it working now, lease mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

Log in to reply