Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    How can i remove a widget from QSplitter?

    General and Desktop
    2
    3
    4656
    Loading More Posts
    • 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.
    • K
      kiesxx last edited by

      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 ?

      1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        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();
        }
        @

        1 Reply Last reply Reply Quote 0
        • K
          kiesxx last edited by

          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);@

          1 Reply Last reply Reply Quote 0
          • First post
            Last post