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. How to make copy of QTableWidgetItem?
Qt 6.11 is out! See what's new in the release blog

How to make copy of QTableWidgetItem?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 3.4k Views 1 Watching
  • 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.
  • EngelardE Offline
    EngelardE Offline
    Engelard
    wrote on last edited by
    #1

    It appears to be a real problem, i can't make a copy of item from QTableWidget1 and place it in QTableWidget2.
    Program runs, but in output messages:

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

    Tried to make separated, new instance of item and assign it with first, but not working...

    QTableWidgetItem *temp = ui->tableWid->item(0, 0);
    
    JonBJ raven-worxR 2 Replies Last reply
    0
    • EngelardE Engelard

      It appears to be a real problem, i can't make a copy of item from QTableWidget1 and place it in QTableWidget2.
      Program runs, but in output messages:

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

      Tried to make separated, new instance of item and assign it with first, but not working...

      QTableWidgetItem *temp = ui->tableWid->item(0, 0);
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @Engelard
      A QTableWidgetItem has a "pointer" in it so that it knows which QTableWidget it belongs to. A given item can only belong to one table/parent.

      So you cannot just "copy" a QTableWidgetItem and stuff it into another table. You must create a new, blank one, set fields one-by-one from the one you want to copy from, and then add it to the new owner.

      EngelardE 1 Reply Last reply
      2
      • JonBJ JonB

        @Engelard
        A QTableWidgetItem has a "pointer" in it so that it knows which QTableWidget it belongs to. A given item can only belong to one table/parent.

        So you cannot just "copy" a QTableWidgetItem and stuff it into another table. You must create a new, blank one, set fields one-by-one from the one you want to copy from, and then add it to the new owner.

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

        @JonB that what i was afraid of... manually reimplement all stuff and parameters to new Item..

        There is really no way to make it faster and simpler?

        Edit:

        Or maybe some function like inheritAllStuffFromOtherItem?

        1 Reply Last reply
        0
        • EngelardE Engelard

          It appears to be a real problem, i can't make a copy of item from QTableWidget1 and place it in QTableWidget2.
          Program runs, but in output messages:

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

          Tried to make separated, new instance of item and assign it with first, but not working...

          QTableWidgetItem *temp = ui->tableWid->item(0, 0);
          
          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Engelard
          something like QTableWidgetItem::clone()? :)
          Which is literally new QTableWidgetItem( *ui->tableWid->item(0,0) )

          and also doesn't assign a QTableWidget (view):

          QTableWidgetItem::QTableWidgetItem(const QTableWidgetItem &other)
              : rtti(Type), values(other.values), view(0),
                d(new QTableWidgetItemPrivate(this)),
                itemFlags(other.itemFlags)
          {
          }
          

          --- 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

          EngelardE JonBJ 2 Replies Last reply
          4
          • raven-worxR raven-worx

            @Engelard
            something like QTableWidgetItem::clone()? :)
            Which is literally new QTableWidgetItem( *ui->tableWid->item(0,0) )

            and also doesn't assign a QTableWidget (view):

            QTableWidgetItem::QTableWidgetItem(const QTableWidgetItem &other)
                : rtti(Type), values(other.values), view(0),
                  d(new QTableWidgetItemPrivate(this)),
                  itemFlags(other.itemFlags)
            {
            }
            
            EngelardE Offline
            EngelardE Offline
            Engelard
            wrote on last edited by
            #5

            @raven-worx oh, i forgot to mention that i'm must using QVector of QTabWidgetItems, so how should i treat that? Next code not working:

            copyItem.append(new QTableWidgetItem(ui->tableWid->item(0, 1));
            
            raven-worxR 1 Reply Last reply
            0
            • EngelardE Engelard

              @raven-worx oh, i forgot to mention that i'm must using QVector of QTabWidgetItems, so how should i treat that? Next code not working:

              copyItem.append(new QTableWidgetItem(ui->tableWid->item(0, 1));
              
              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #6

              @Engelard said in How to make copy of QTableWidgetItem?:

              copyItem.append(new QTableWidgetItem(ui->tableWid->item(0, 1));

              this is not the code i've posted. Note the *
              And as i said, for the sake of simplicity just call clone()

              copyItem.append( ui->tableWid->item(0,1)->clone() );
              

              --- 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
              5
              • raven-worxR raven-worx

                @Engelard
                something like QTableWidgetItem::clone()? :)
                Which is literally new QTableWidgetItem( *ui->tableWid->item(0,0) )

                and also doesn't assign a QTableWidget (view):

                QTableWidgetItem::QTableWidgetItem(const QTableWidgetItem &other)
                    : rtti(Type), values(other.values), view(0),
                      d(new QTableWidgetItemPrivate(this)),
                      itemFlags(other.itemFlags)
                {
                }
                
                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #7

                @raven-worx said in How to make copy of QTableWidgetItem?:

                something like QTableWidgetItem::clone()? :)
                Which is literally new QTableWidget( *ui->tableWid->item(0,0) )

                To avoid confusion would you care to correct your second line in your earlier post above from QTableWidget to QTableWidgetItem?

                raven-worxR 1 Reply Last reply
                0
                • JonBJ JonB

                  @raven-worx said in How to make copy of QTableWidgetItem?:

                  something like QTableWidgetItem::clone()? :)
                  Which is literally new QTableWidget( *ui->tableWid->item(0,0) )

                  To avoid confusion would you care to correct your second line in your earlier post above from QTableWidget to QTableWidgetItem?

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  @JonB
                  done

                  --- 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
                  1

                  • Login

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