Important: Please read the Qt Code of Conduct -

Resizing main widget after removal the internal widget

  • I can’t figure out, why i can’t resize my main widget right after removing the internal widget? And how to make resizing work? Could please you tell me? I wrote an example:

    #include <QApplication>
    #include <QHBoxLayout>
    #include <QWidget>
    #include <QGroupBox>
    #include <QTimer>
    #include <QObject>
    int main(int argc, char *argv[]) {
    	QApplication a(argc, argv);
    	QWidget* main = new QWidget;
    	QGroupBox* widget1 = new QGroupBox("Widget 1");
    	QGroupBox* widget2 = new QGroupBox("Widget 2");
    	widget1->setFixedSize(400, 250);
    	widget2->setFixedSize(400, 250);
    	QHBoxLayout* grid_layout = new QHBoxLayout;
    	QTimer::singleShot(3000, [&](){
    		delete widget2;
    		main->resize(1, 1); // <-------------| Does not works
    	QTimer::singleShot(5000, [&](){
    		main->resize(1, 1); // <-------------| Works
    	return a.exec();

    Something like this freezes the program

    QTimer::singleShot(3000, [&](){
    		delete widget2;
    		while (main->size() != QSize(1, 1))
    			main->resize(QSize(1, 1));

  • Lifetime Qt Champion

    When you remove something from a layout a re-calculation event is put on the event
    queue. So its asynchronous as seen if you do

     QTimer::singleShot(3000, [&](){
            delete widget2;
            a.processEvents();  // allow it to process the events from "delete"
            main->resize(1, 1); // <-------------| Does not works

    then resize works. However, using processEvents() is not recommended but you can use a single shot for the
    resize and it will work too.

  • @mrjj , @nammidd
    Are you sure you can just delete a widget on a layout without telling the layout about it? I thought you need to go something like:



  • Lifetime Qt Champion

    Yes you are right that the correct way
    is to use removeWidget or takeAt.
    But apparently plain delete also seems to work, i would guess due to the internal
    qobject housekeeping notifications.
    But very good point!

  • @mrjj
    Looking at the code for QLayout::removeWidget(QWidget *widget) at it does:

    delete takeAt(i);

    so it invalidate()s the layout, which update()/redraws it, which might be good here to get main redrawn/resized correctly?

  • Lifetime Qt Champion

    Good digging :)
    Well i did try to call invalidate but in this case, the result was the same.

Log in to reply