QGroupBox moving widgets up and down



  • I am doing a lot of work with lists of objects and have a QGroupBox that is filled with custom widgets. I needed to be able to move them up and down in the list so I used a QGridLayout in the QGroupBox. I use the following code to do the move up. Since I am real new to Qt, I was wondering if there is a better way to do it.

    @
    void ChannelGroup::MoveToneUp(int pos){
    //setTitle("Channel*: " + QString::number(pos));
    if (pos > 0){
    QWidget prev = theLayout->itemAtPosition(pos -1, 0)->widget();
    QWidget curr = theLayout->itemAtPosition(pos, 0)->widget();
    theLayout->removeWidget(prev);
    theLayout->removeWidget(curr);
    theLayout->addWidget(curr, pos -1, 0);
    ((ToneWidget
    )curr)->setGridPos(pos -1);
    theLayout->addWidget(prev, pos, 0);
    ((ToneWidget
    )prev)->setGridPos(pos);
    }
    }
    @



  • If there is only a single column of widgets then you could use a QVBoxLayout. You should only need to use takeAt() followed by insertWidget() once to move a widget up or down.

    You could also consider using a QListWidget


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.