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

adjustSize() or resize() seemingly having to be called twice in a given situation.



  • Warning: I'm new with Qt, so I may be going about this the wrong way -

    Scenario: UI designer has a frame->horizontalspacer->frame->horizontalspacer-> frame (with button), i.e.:

    0_1527409612740_UI-Frames.png

    The button on the right there is set up so that when pressed, the second frame in the middle is hidden. The intention is so that the button on the right be moved--buttressed against the first frame--and the window resized upon the middle frame's being hidden.

    This works. On initial running, the second frame is set to hidden with an updateSize call based on minimumWidth:

    0_1527410219477_frames2.png

    Now with the button, the pseudo-code: OnClick: if frame is invisible, show it, call window's adjustSize() to update minimumWidth, and then call resize with minimumWidth() and minimumHeight(). This works and results like so (the middle frame was given a minimum size):

    0_1527410356199_frames3.png

    Now the problem is on clicking the button again, the same code for hiding doesn't update the window's minimumWidth appropriately. : Onclick: if frame is visible, hide the frame, call adjust size (here is expected minimumWidth to be updated, yet it isn't upon testing), calling resize afterwards with minimumWIdth() doesn't update the window (but it DOES update the minimumWidth)... the minimumWidth is the same value as the expanded width after adjustSize(). This gives, instead of the first window, something like:

    0_1527410524116_frames4.png

    I can get this to work fine if adjustSize() is called twice before calling resize(), or call resize() twice after one adjustSize(). This seems to be abnormal behavior. Is there something obvious that I'm doing wrong? Not calling a function or something? Any answers are appreciated.


  • Lifetime Qt Champion

    Hi
    Your idea/Design seems ok.
    But without code its hard to guess why window do not contract again.
    You do manually adjust the Window ?

    Maybe you just need to adjust policy
    http://doc.qt.io/qt-5/qsizepolicy.html#details
    to make it want to be as small as possible



  • Thanks for trying to help. Yeah, even with SizePolicy set to minimum, it doesn't change the fact that minimumSize() returns a non-updated size after hiding the middle form. No manual adjustment is performed. Yet, again, if updateSize() is called twice after hiding the middle form, the minimumSize() returns the appropriate size and will contract with a resize() call with minimumWidth() and minimumHeight() as arguments. It doesn't contract because the minimumSize() value isn't being updated after one updateSize() call, but it does contract if updateSize() is called twice in a row. Doesn't make any sense. Hoping the 'pseudo-code' should be reasonable enough to understand what's happening.


  • Lifetime Qt Champion

    Hi
    Well when a widget is hidden its resize(ing) might be deferred .
    Try move show() to very to top before rest of code.
    I think its something like that.



  • Likewise.
    Calling the window's show(), update(), updateGeometry(), repaint(), all these before anything else after clicking doesn't fix the issue. Merely calling updateSize() twice in a row updates the value got upon calling minimumWidth().


  • Lifetime Qt Champion

    @worldwideweary

    Ok. something else then :)



  • Anyone else more knowledgeable on the issue willing to share some information?


  • Lifetime Qt Champion

    @worldwideweary
    Maybe you should show the actual code ?



  • It is as the pseudo-code mentioned earlier, but okay:

    MainWindow::MainWindow(QWidget *parent) :
    	QMainWindow(parent),
    	ui(new Ui::MainWindow)
    {
    	ui->setupUi(this);
    	ui->frame_Secondary->setHidden(true);
    	adjustSize();
    	resize(minimumWidth(), minimumHeight());
            // this works fine
    }
    
    void MainWindow::on_pb_ExtendRight_pressed() {
    	if (ui->frame_Secondary->isHidden()) 
            {
    		ui->frame_Secondary->setHidden(false);
    		ui->pb_ExtendRight->setText(QString("<"));
    		adjustSize();
    		resize(minimumWidth(), minimumHeight());
                    // this works fine
    	}
    	else
    	{
    		ui->frame_Secondary->setHidden(true);
    		ui->pb_ExtendRight->setText(QString(">"));
                    adjustSize();
    		// This doesn't work unless adjustSize()
                    // is called twice, or resize() twice
    	       resize(minimumWidth(), minimumHeight());
    	}
    }
    

Log in to reply