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. duplicate QStandardItem
Qt 6.11 is out! See what's new in the release blog

duplicate QStandardItem

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 4.5k Views 3 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.
  • S Offline
    S Offline
    shahriar25
    wrote on last edited by
    #1

    Hi
    I am working on a project and I have two QStandardModel(trackModel & queueModel) and I'm trying to copy items from one model to another:

    QList<QStandardItem*> tempList= new QList<QStandardItem>;
    for (int i=0; i<trackModel->rowCount(); ++i)
    {
    tempList->append(trackModel->item(i));
    }
    for (int i=0; i<trackModel->rowCount(); ++i)
    {
    queueModel->setItem(i, tempList->at(i));
    }

    But in queueModel I get empty items and this warnings in qdebugger:

    QStandardItem::setChild: Ignoring duplicate insertion of item 0xec83e0
    QStandardItem::setChild: Ignoring duplicate insertion of item 0x1947740
    QStandardItem::setChild: Ignoring duplicate insertion of item 0xec85a0
    QStandardItem::setChild: Ignoring duplicate insertion of item 0x19484d0
    QStandardItem::setChild: Ignoring duplicate insertion of item 0x1947f50
    QStandardItem::setChild: Ignoring duplicate insertion of item 0x19471d0
    QStandardItem::setChild: Ignoring duplicate insertion of item 0x1948030
    QStandardItem::setChild: Ignoring duplicate insertion of item 0x1948550
    QStandardItem::setChild: Ignoring duplicate insertion of item 0x1948340
    QStandardItem::setChild: Ignoring duplicate insertion of item 0x1948d00
    QStandardItem::setChild: Ignoring duplicate insertion of item 0x1947e50

    It ignores them all and I'm guessing because they already exist in ram.
    I want the Items to remain in queueModel whent I call:
    trackModel->clear();

    How should I do this?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      hi
      you must use TakeItem
      http://doc.qt.io/qt-5.5/qstandarditemmodel.html#takeItem
      else the model still owns it.

      Also this line confuses me;
      QList<QStandardItem*> tempList= new QList<QStandardItem>;
      why not just
      QList<QStandardItem*> tempList;
      ?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shahriar25
        wrote on last edited by
        #3

        I can,t use takeItem() because I want the items to be on both Models.
        And also i think if you use:

        QWidgetItem item;
        model.setItem(i, item)

        the item gets deleted when the function ends and the the item is not in the model anymore. I tried it a few time and the model doesn't show the item. am I wrong?

        mrjjM 1 Reply Last reply
        0
        • S shahriar25

          I can,t use takeItem() because I want the items to be on both Models.
          And also i think if you use:

          QWidgetItem item;
          model.setItem(i, item)

          the item gets deleted when the function ends and the the item is not in the model anymore. I tried it a few time and the model doesn't show the item. am I wrong?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi

          Im dont think the items can be in both models as each model thinks it owns it.
          Cant you just make a function to fill the model and call it for each ?

          I assume you mean
          QStandardItem item;
          model.setItem(i, item)

          The model takes ownership of the item.
          You should new it.
          QStandardItem *item=new QStandardItem ();
          Not sure it makes copy if u use non pointer.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            What does each model represent ? Depending on that, you could also use proxy models to show the information you want.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • Chris KawaC Online
              Chris KawaC Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by Chris Kawa
              #6

              After na item is inserted into a model it is owned by it, so it needs to be created on the heap (via new) and can't be shared across models.

              You can create a duplicate of an item with clone(). This new item has no model assigned and can be used to insert it into the other model.

              QList<QStandardItem*> tempList= new QList<QStandardItem>;
              

              This makes no sense. On the left side is a list of pointers. On the right side you create a pointer to a list.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                shahriar25
                wrote on last edited by
                #7

                Hi

                @SGaist : trackModel is to show the tracks in an album and the queueModel is to put all of the tracks in the
                QmediaPlayer.playList()
                And show them in queueModel in case the user wants to edit the queue

                And yes I made a mistake in QList. You are right chris

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  shahriar25
                  wrote on last edited by
                  #8

                  Hi,
                  thank you chris clone() worked.
                  I have two other questions. You can drag the tems in a QListView anywhere in the listView. How can I disable that?

                  And also I want the QListView to resize the items when I press ctrl + "+" or ctrl + "-" or ctrl + mousewheel. How should I do that?

                  1 Reply Last reply
                  0
                  • Chris KawaC Online
                    Chris KawaC Online
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    You can use setMovement to block item moves.

                    As for resize - what do you mean exactly? Change font size or the item rectangle size or what?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      shahriar25
                      wrote on last edited by
                      #10

                      I mean changing the grid and the icon size. In general bigger Items (albums)

                      1 Reply Last reply
                      0

                      • Login

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