duplicate QStandardItem



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

    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.


  • Lifetime Qt Champion

    Hi,

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


  • Moderators

    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.



  • 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



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


  • Moderators

    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?



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.