c++qtkeyrelease

Not able to set the geometry in QT


I am using setGeometry() to set the coordinate of widget. But when i am deleting some widget and remove it from main layout then it is not able to set the Geometry properly. I am using KeyReleaseEvent() ie, when i am pressing Key D then certain widget should be removed and it should set the Geometry to particular position. When i am pressing it for the first time, the effect is not seen but the widgets are removed and when i am pressing it for the second time it is set properly.

void mywindow::popUpWindow()
{
    if(stack1->currentIndex()==0){
        stack3->hide();
        mainLayout->addWidget(stack2);
        stack2->show();
        stack2->setFixedSize(400,200);
        this->setGeometry(100,400,900,200);
    }

    if(stack1->currentIndex()==1){
        stack2->hide();
        mainLayout->addWidget(stack3);
        stack3->show();
        stack3->setFixedSize(400,200);
        this->setGeometry(100,400,900,200);
    }
}

void mywindow::deleteWindow()
{
    mainLayout->removeWidget(stack2);
    mainLayout->removeWidget(stack3);
    stack2->hide();
    stack3->hide();
    this->setGeometry(100,400,500,200);

}

popUpWindow() is linked with one KeyReleasedEvent() and deleteWindow() is linked with other Key.

Original WIndow

After pressing I Key and calling popWindow()

After pressing D key and calling deleteWindow()After pressing 2nd time D Key and calling deleteWindow for the 2nd time

Fig 1: Original WIndow
Fig 2: After calling popUpWindow() by pressing key I
Fig 3: After calling deleteWindow() by pressing key D for the first time
Fug 4: After calling again deleteWindow() for 2nd time

Solution

  • After removing widgets, if you want everything to resize nicely, you should call adjustSize().

    Also widget sizes across a stacked widget can affect the end width and height that your window ends up being.

    Hope that helps.