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. QTableWidget + setCellWidget(myWidget) how remove from Cell without delete myWidget ?
Forum Update on Monday, May 27th 2025

QTableWidget + setCellWidget(myWidget) how remove from Cell without delete myWidget ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 6.5k 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.
  • P Offline
    P Offline
    pdn_mail
    wrote on 23 May 2016, 10:39 last edited by
    #1

    Hi!
    I insert single myWidget to QTableWidget by setCellWidget().
    When I using removeCellWidget() function myWidget is removed for ever, but it still will be necessary to me repeatedly.
    How correctly remove from Cell myWidget without delete myWidget?

    R R 2 Replies Last reply 23 May 2016, 10:46
    0
    • P pdn_mail
      23 May 2016, 10:39

      Hi!
      I insert single myWidget to QTableWidget by setCellWidget().
      When I using removeCellWidget() function myWidget is removed for ever, but it still will be necessary to me repeatedly.
      How correctly remove from Cell myWidget without delete myWidget?

      R Offline
      R Offline
      Ratzz
      wrote on 23 May 2016, 10:46 last edited by
      #2

      @pdn_mail
      Hi,
      You may try http://doc.qt.io/qt-4.8/qtablewidget.html#takeItem

      --Alles ist gut.

      1 Reply Last reply
      1
      • P Offline
        P Offline
        pdn_mail
        wrote on 23 May 2016, 10:54 last edited by
        #3

        it hardly, takeItem works with QTableWidgetItem, but not with QWidget. Different mechanisms.

        1 Reply Last reply
        0
        • P pdn_mail
          23 May 2016, 10:39

          Hi!
          I insert single myWidget to QTableWidget by setCellWidget().
          When I using removeCellWidget() function myWidget is removed for ever, but it still will be necessary to me repeatedly.
          How correctly remove from Cell myWidget without delete myWidget?

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 23 May 2016, 11:58 last edited by
          #4

          @pdn_mail
          There is no way - currently.
          The index widget will be deleted whenever a new index widget is set.
          removeCellWidget() simply sets a NULL-widget. So you have implicitly the same problem.

          What exactly is your usecase?

          An alternative would be to add a container widget. for example: QStackedWidget
          And add the widgets there, so Qt doesn't delete them.

          if( QStackedWidget* w = qobject_cast<QStackedWidget*>( tableWidget->cellWidget(r,c) )
          {
               w->addWidget( ... )  // set widget
               w->removeWidget( ... );  //remove widget
               w->setCurrentIndex( ... ) / w->setCurrentWidget( ... );  //switch widgets
          }
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          2
          • P Offline
            P Offline
            pdn_mail
            wrote on 24 May 2016, 03:44 last edited by pdn_mail
            #5

            Thanks. But unfortunately I have other usecase, I shouldn't replace widgets in a cell, I should return to a cell initial properties which were to setCellWidget.

            void MainWindow::on_tableWidget_cellEntered(int row, int column)
            {
            QStackedWidget *sw=new QStackedWidget;
            int r=cellwidgetcoord.row; 
            int c=cellwidgetcoord.column;
            cellwidgetcoord.row=row; 
            cellwidgetcoord.column=column;
            ui->tableWidget->removeCellWidget(r,c);
            sw->addWidget(&comboBox_5);
            ui->tableWidget->setCellWidget(row, column, sw);
            }
            
            R 1 Reply Last reply 24 May 2016, 05:19
            0
            • P pdn_mail
              24 May 2016, 03:44

              Thanks. But unfortunately I have other usecase, I shouldn't replace widgets in a cell, I should return to a cell initial properties which were to setCellWidget.

              void MainWindow::on_tableWidget_cellEntered(int row, int column)
              {
              QStackedWidget *sw=new QStackedWidget;
              int r=cellwidgetcoord.row; 
              int c=cellwidgetcoord.column;
              cellwidgetcoord.row=row; 
              cellwidgetcoord.column=column;
              ui->tableWidget->removeCellWidget(r,c);
              sw->addWidget(&comboBox_5);
              ui->tableWidget->setCellWidget(row, column, sw);
              }
              
              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 24 May 2016, 05:19 last edited by
              #6

              @pdn_mail
              maybe it's enough to simply hide it

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0

              2/6

              23 May 2016, 10:46

              topic:navigator.unread, 4
              • Login

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