Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Removing and adding cells in a QGridLayout
QtWS25 Last Chance

Removing and adding cells in a QGridLayout

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 722 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nefarious
    wrote on last edited by
    #1

    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 = mMicrophones.at(index +i);
                qDebug() << "Remove: " << mCtrl->getMicrophoneIndex();
                mGridLayout->removeWidget(mCtrl);
            }
            mCurrentTab = tab;
            index = mCurrentTab * mTabSize;
            for (int i = 0; i < mTabSize; i++)
            {
                QMicrophoneCtrl* mCtrl = mMicrophones.at(index + 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.

    jsulmJ 1 Reply Last reply
    0
    • N nefarious

      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 = mMicrophones.at(index +i);
                  qDebug() << "Remove: " << mCtrl->getMicrophoneIndex();
                  mGridLayout->removeWidget(mCtrl);
              }
              mCurrentTab = tab;
              index = mCurrentTab * mTabSize;
              for (int i = 0; i < mTabSize; i++)
              {
                  QMicrophoneCtrl* mCtrl = mMicrophones.at(index + 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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved