How can i remove a widget from QSplitter?



  • I've tried the QSplitter::hide(),show(),update function, also delete QWidget.
    Nothing work.

    @void PlainView::addComponent(QWidget *widget)
    {
    qDebug() << _splitOne->widget(1);

    //delete current widget on index 1
    delete _splitOne->widget(1);
    //add new widget on index 1
    _splitOne->addWidget(widget);

    qDebug() << _splitOne->widget(1);
    }

    //output
    QObject(0x0)
    QTextEdit(0xa0f580@

    Thats okey, but i can't see the new widget. Some People told me, try this:
    @_splitOne->widget(1)->setParent(NULL)@ or @_splitOne->widget(1)->setParent(0);@ But my program throw an exception:
    @The program has unexpectedly finished.@

    How can i delete the widget / see the new widget ?


  • Moderators

    Hi, welcome to devnet.

    Have a look at these examples:
    @
    QString nextName() {
    static int index = 0;
    return QString("Label ") + QString::number(index++);
    }

    void MainWindow::on_pushButtonAddFirst_clicked() {
    ui->splitter->insertWidget(0, new QLabel(nextName(), this));
    }

    void MainWindow::on_pushButtonAddLast_clicked() {
    ui->splitter->addWidget(new QLabel(nextName(), this));
    }

    void MainWindow::on_pushButtonRemoveFirst_clicked() {
    if(ui->splitter->count() > 0)
    ui->splitter->widget(0)->deleteLater();
    }

    void MainWindow::on_pushButtonRemoveLast_clicked() {
    if(ui->splitter->count() > 0)
    ui->splitter->widget(ui->splitter->count() -1)->deleteLater();
    }
    @



  • here is my solution:

    @ if(splitter->widget(widgetPos)){
    // remove widget from splitter
    QWidget *oldWidget = splitter->widget(widgetPos);
    // remove parent of the widget from the gui
    oldWidget->setParent(0);
    // delete current widget on index 1
    oldWidget->deleteLater();
    }
    splitter->insertWidget(widgetPos, widget);@


Log in to reply
 

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