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. Why i can't replace QTableWidgetItem with another?
Forum Updated to NodeBB v4.3 + New Features

Why i can't replace QTableWidgetItem with another?

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 4.9k 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #2

    Can you show us the code you use to insert?

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    EngelardE 1 Reply Last reply
    1
    • VRoninV VRonin

      Can you show us the code you use to insert?

      EngelardE Offline
      EngelardE Offline
      Engelard
      wrote on last edited by Engelard
      #3

      @VRonin said in Why i can't replace QTableWidgetItem with another?:

      Can you show us the code you use to insert?

      ui->tableWidMemory->setItem(0, 1, valOrder.at(i));
      
      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #4

        How do you add items to valOrder?

        What we need to answer is the complete lifetime of the QTableWidgetItem before it ends up in ui->tableWidMemory->setItem

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        EngelardE 1 Reply Last reply
        4
        • EngelardE Engelard

          I have TableWidget, in runtime it fills up with Items using setItem function. But then, when i needed, i try to replace some items with another and it gives warnings like that:

          QTableWidget: cannot insert an item that is already owned by another QTableWidget

          This is the declaration of NEW widgetItems with which i want to replace old one:

          QVector<QTableWidgetItem*> valOrder;
          

          I tried so many ways like remove all rows from a TableWidget, then fill it with this new items, but same warning and empty cells. Or some other functions, but nothing seems to work well.

          How can i simply replace one item in cell with another?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #5

          @Engelard
          From the error message:

          QTableWidget: cannot insert an item that is already owned by another QTableWidget

          A QTableWidget "knows" what QTableWidgetItems have been inserted into it, and each QTableWidgetItem "knows" which (one) QTableWidget it has been inserted into. A particular QTableWidgetItem instance can only be stored in one QTableWidget, you cannot re-insert it into another.

          In some shape or form, your QVector<QTableWidgetItem*> items must have previously been inserted into another QTableWidget, so you cannot also insert them into this QTableWidget.

          1 Reply Last reply
          1
          • VRoninV VRonin

            How do you add items to valOrder?

            What we need to answer is the complete lifetime of the QTableWidgetItem before it ends up in ui->tableWidMemory->setItem

            EngelardE Offline
            EngelardE Offline
            Engelard
            wrote on last edited by Engelard
            #6

            @VRonin said in Why i can't replace QTableWidgetItem with another?:

            How do you add items to valOrder?

            What we need to answer is the complete lifetime of the QTableWidgetItem before it ends up in ui->tableWidMemory->setItem

            Well, add it like that:

            valOrder.append(ui->tableWidMemory->item(i, 0));
            

            valOrder declaration(which i posted above) placed in .h file, so i suppose it's lifetime = lifetime of whole program.

            So as you might gues, i'm adding that items FROM tabWidget to that vector, and then from it after some time fill tabWidget with old items which i just take recently from it.

            JonBJ 1 Reply Last reply
            0
            • EngelardE Engelard

              @VRonin said in Why i can't replace QTableWidgetItem with another?:

              How do you add items to valOrder?

              What we need to answer is the complete lifetime of the QTableWidgetItem before it ends up in ui->tableWidMemory->setItem

              Well, add it like that:

              valOrder.append(ui->tableWidMemory->item(i, 0));
              

              valOrder declaration(which i posted above) placed in .h file, so i suppose it's lifetime = lifetime of whole program.

              So as you might gues, i'm adding that items FROM tabWidget to that vector, and then from it after some time fill tabWidget with old items which i just take recently from it.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #7

              @Engelard

              fill tabWidget with old items which i just take recently from it

              So for a start did you remove the item before you tried to re-add it? From what you have shown of populating your valOrder they are existing/live items in the table widget. You cannot add the same item twice....

              EngelardE 1 Reply Last reply
              0
              • JonBJ JonB

                @Engelard

                fill tabWidget with old items which i just take recently from it

                So for a start did you remove the item before you tried to re-add it? From what you have shown of populating your valOrder they are existing/live items in the table widget. You cannot add the same item twice....

                EngelardE Offline
                EngelardE Offline
                Engelard
                wrote on last edited by Engelard
                #8

                @JonB no, i dont remove existing in tabWid first. I tried, but found only removeRow function, so using it in for loop i'm removing all rows from tabWid so it empty then, but for some reason i can't fill tabWid with my valOrder after such actions. Here whole code section:

                for(int i=valOrder.size(); i>0; i--)
                {
                    ui->tableWidMemory->removeRow(i-1);
                }
                ui->tableWidMemory->setRowCount(valOrder.size());
                for(int i=0; i<sizeof(valOrder); i++)
                {
                    ui->tableWidMemory->setItem(i, 1, valOrder.at(i));
                }
                

                Would be nice if i could use some function like "replace" or "removeItemAtCell" or something like that...

                JonBJ 1 Reply Last reply
                0
                • EngelardE Engelard

                  @JonB no, i dont remove existing in tabWid first. I tried, but found only removeRow function, so using it in for loop i'm removing all rows from tabWid so it empty then, but for some reason i can't fill tabWid with my valOrder after such actions. Here whole code section:

                  for(int i=valOrder.size(); i>0; i--)
                  {
                      ui->tableWidMemory->removeRow(i-1);
                  }
                  ui->tableWidMemory->setRowCount(valOrder.size());
                  for(int i=0; i<sizeof(valOrder); i++)
                  {
                      ui->tableWidMemory->setItem(i, 1, valOrder.at(i));
                  }
                  

                  Would be nice if i could use some function like "replace" or "removeItemAtCell" or something like that...

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #9

                  @Engelard
                  I just know you won't be able to re-add the same item twice. Hence the error message (in this case, I guess the "already owned by another QTableWidget" ends up meaning "[nor] already owned by the same QTableWidget"). I don't know how you do it in your situation, but whatever (unless you can delete/remove the existing one from the table) you need to create a fresh, new QTableWidgetItem, which does not already belong to the table, and then copy in your data or whatever from the one it's a duplicate of. Or retain the QTableWidgetItem but change what's in it. I'm not really sure whether you're trying to add a new, duplicate one or alter an existing one.

                  1 Reply Last reply
                  1
                  • EngelardE Offline
                    EngelardE Offline
                    Engelard
                    wrote on last edited by Engelard
                    #10

                    Ok, the other question then. How can i replace existed item in tableWdiget to some other cell? Because that's the point of all my actions.

                    Or even replace whole row. Like i want replace rowAt(5) to 2.

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • EngelardE Engelard

                      Ok, the other question then. How can i replace existed item in tableWdiget to some other cell? Because that's the point of all my actions.

                      Or even replace whole row. Like i want replace rowAt(5) to 2.

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @Engelard said in Why i can't replace QTableWidgetItem with another?:

                      How can i replace existed item in tableWdiget to some other cell?

                      By taking a look in the documentation: http://doc.qt.io/qt-5/qtablewidget.html#takeItem

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      EngelardE 1 Reply Last reply
                      3
                      • Christian EhrlicherC Christian Ehrlicher

                        @Engelard said in Why i can't replace QTableWidgetItem with another?:

                        How can i replace existed item in tableWdiget to some other cell?

                        By taking a look in the documentation: http://doc.qt.io/qt-5/qtablewidget.html#takeItem

                        EngelardE Offline
                        EngelardE Offline
                        Engelard
                        wrote on last edited by
                        #12

                        @Christian-Ehrlicher said in Why i can't replace QTableWidgetItem with another?:

                        @Engelard said in Why i can't replace QTableWidgetItem with another?:

                        How can i replace existed item in tableWdiget to some other cell?

                        By taking a look in the documentation: http://doc.qt.io/qt-5/qtablewidget.html#takeItem

                        But which item exactly it will take? There must be at least 3 parameters for 1.QTableWidgetItem, 2.Row to which i want it remove, 3. and column.

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          I don't understand your problem - takeItem has two parameters - row and column of the item to take and returns this item. This is exactly what you want. After this you can set the item to the new row/column with setItem().

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          EngelardE 1 Reply Last reply
                          3
                          • Christian EhrlicherC Christian Ehrlicher

                            I don't understand your problem - takeItem has two parameters - row and column of the item to take and returns this item. This is exactly what you want. After this you can set the item to the new row/column with setItem().

                            EngelardE Offline
                            EngelardE Offline
                            Engelard
                            wrote on last edited by
                            #14

                            @Christian-Ehrlicher Now i get it. Why they did'nt call it like "remove"... I thought that function shout "take" item from one cell and place it to the another....

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              @Engelard said in Why i can't replace QTableWidgetItem with another?:

                              I thought that function shout "take" item from one cell and place it to the another.

                              Imo the documentation is clear enough: Removes the item at row and column from the table without deleting it.
                              Calling sth like remove() I would expect that the item gets deleted since the QTreeWidget has the ownership.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              2

                              • Login

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