Important: Please read the Qt Code of Conduct -

Removing and adding cells in a QGridLayout

  • I am trying to build a panel that uses a grid layout. To test the idea I build a QWidget and a QGridLayout for the QWidget. After I add the 20 elements, I want to remove these elements and replace them with 20 others. The code I use to replace them is:

            int index = mCurrentTab * mTabSize;
            //remove(mGridLayout, -1, 0, false);
            //mGridLayout->setColumnMinimumWidth(0, 0);
            //mGridLayout->setColumnStretch(0, 0);
            for (int i = 0; i < mTabSize; i++)
                QMicrophoneCtrl* mCtrl = +i);
                qDebug() << "Remove: " << mCtrl->getMicrophoneIndex();
            mCurrentTab = tab;
            index = mCurrentTab * mTabSize;
            for (int i = 0; i < mTabSize; i++)
                QMicrophoneCtrl* mCtrl = + i);
                qDebug() << "Add: " << mCtrl->getMicrophoneIndex();
                mGridLayout->addWidget(mCtrl, i, 0);

    The net effect is that the new cells are written over the old cells and depending on the size of the new cell I can see both the old cell and the new cell. Is there a reason this won't work.

  • Lifetime Qt Champion

    @nefarious You only remove old widgets from layout, you do not delete them. Maybe that is the reason: the old widgets are still shown at the same location they were before?

Log in to reply