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

how to auto-size dialog depending on which "stack" is front?



  • i have a dialog with a QStackedWidget, i switch between them with a QListWidget. I'd like the dialog to auto-size itself (animated wooshing a bonus!) based on which stack is "front". I've put vertical Spacers at the bottom of each stack, to top-justify each stack, but i'd rather resize the dialog. here's a couple stacks:

    1_1560968653916_Screen Shot 2019-06-19 at 11.22.42 AM.png

    0_1560968653915_Screen Shot 2019-06-19 at 11.22.36 AM.png

    2_1560968653916_Screen Shot 2019-06-19 at 11.22.48 AM.png

    as you can see, the LAST one is the tallest one, which currently defines the minimum size of the dialog.

    Is there a way to, when the user causes a stack to come to front, tell the dialog to minimize it's vertical size based on the front stack?

    -dave



  • this is what worked:

    QWidget		*pageP;
    
    for (int i = 0; i < stackP->count (); ++i) {
    	pageP = stackP->widget(i);
    	pageP->setSizePolicy(
    		QSizePolicy::Preferred, 
    		QSizePolicy::Ignored);
    }
    
    pageP = stackP->widget(stackIndexL);
    pageP->setSizePolicy(
    	QSizePolicy::Preferred, 
    	QSizePolicy::Preferred);
    
    stackP->adjustSize();
    adjustSize();
    

  • Qt Champions 2017

    @davecotter said in how to auto-size dialog depending on which "stack" is front?:

    Is there a way to, when the user causes a stack to come to front, tell the dialog to minimize it's vertical size based on the front stack?

    If I'm not wrong, setting the resize policy of the dialog to QSizePolicy::MinimumExpanding and calling QWidget::adjustSize after you switched the stack widget might do the trick.



  • i've set the size policy on the QDialog widget like this:
    0_1560978050927_Screen Shot 2019-06-19 at 2.00.34 PM.png

    and here is my code:

    0_1560977966865_Screen Shot 2019-06-19 at 1.56.31 PM.png

    if I un-comment that adjustSize(); line, then my dialog gets much WIDER, and does not get shorter.

    it seems that it is finding the minimum size of ALL stack panels, and using that, instead of only finding the minimum size of the CURRENT (front most) stack panel?


  • Qt Champions 2017

    Eh, I may be wrong, that's tricky. What I'd suggest you try is to remove the vertical stretches and set the policy of each of the stacked widgets to Minimum. If that doesn't do it, I'm out of ideas.



  • yep nope. seems the minimum size of the dialog is determined by the stack panel with the maximum size, not the stack panel that is in front. i do not see a way around it.



  • can anyone contradict that? i don't yet have access to debugging the source code so i can't tell if there's a way out of this


  • Qt Champions 2017

    If you prepare a simplistic compileable example that reproduces it, I can run it through on Linux.


  • Lifetime Qt Champion

    Hi,

    Take a look at this stack overflow thread. It shows a technique to achieve what you want.



  • this is what worked:

    QWidget		*pageP;
    
    for (int i = 0; i < stackP->count (); ++i) {
    	pageP = stackP->widget(i);
    	pageP->setSizePolicy(
    		QSizePolicy::Preferred, 
    		QSizePolicy::Ignored);
    }
    
    pageP = stackP->widget(stackIndexL);
    pageP->setSizePolicy(
    	QSizePolicy::Preferred, 
    	QSizePolicy::Preferred);
    
    stackP->adjustSize();
    adjustSize();
    

Log in to reply