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. QListView & item movement
Forum Updated to NodeBB v4.3 + New Features

QListView & item movement

Scheduled Pinned Locked Moved Unsolved General and Desktop
29 Posts 4 Posters 12.1k Views 2 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 have a QListView and it's a listview for showing the queue in my music player project. I have two problems:

    first: i want the user to be able to move the items up and down by draging and droping them. I set the dragDropMode to internalMove and the dafaultDropAction to targetMoveAcion but when I run the app and move the items they go into each other.
    what can I do about that? (QListWidget doesn't have this problem)

    second: I need to know which Item is moved and where is it moved to so I can be able to update the QMediaPlayer->playList(). how can I do that?

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

      Hi,

      What do you mean by "they go into each other" ?

      You can use the indexesMoved method to get the information and rebuild the playlist from there.

      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
      • S Offline
        S Offline
        shahriar25
        wrote on last edited by shahriar25
        #3

        @SGaist I mean when you move the Item in QListView you can drop the item between two items or on another item. when you drop it on another item the other item disappears and the moved item takes its place. I think it replaces the item's data

        and also I need to know where the index is moved to. is that possible?

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

          Are you using the QListView in Icon mode or List mode ?

          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
          • S Offline
            S Offline
            shahriar25
            wrote on last edited by
            #5

            @SGaist I'm using it in list mode

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

              Can you share how you setup your QListView ?

              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
              • S Offline
                S Offline
                shahriar25
                wrote on last edited by
                #7

                @SGaist
                I create a queueModel and set the listView's model to queue model. the Items in queueModel have a few userRole data. this is the slot that connets to doubleClicked signal on a track that I have in my trackModel:

                for (int i=0; i<trackModel->rowCount(); ++i)
                {
                queueModel->setItem(i, trackModel->item(i)->clone());

                    const QPixmap *artWork = new QPixmap;
                    artWork = ui->trackArtLabel->pixmap();
                    queueModel->item(i)->setIcon(QIcon(*artWork));
                
                    QString itemTitle=QString("%1\n%2\n%3")
                            .arg(queueModel->item(i)->data(Qt::UserRole+3).toString())
                            .arg(queueModel->item(i)->data(Qt::UserRole+4).toString())
                            .arg(queueModel->item(i)->data(Qt::UserRole+6).toString());
                
                    queueModel->item(i)->setText(itemTitle);
                }
                
                for (int i=0; i<queueModel->rowCount(); ++i)
                {
                    mediaPlayer.playlist()->addMedia(QMediaContent(QUrl::fromLocalFile(queueModel->item(i)->data(Qt::UserRole+1).toString())));
                }
                
                playQueue(item.data(Qt::UserRole+7).toInt());
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You have a memory leak here. You allocate artWork on the heap and then directly replace its value by the one from trackArtLabel's pixmap. There's no need for that allocation.

                  Is your queueModel a custom model ?

                  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
                  • S Offline
                    S Offline
                    shahriar25
                    wrote on last edited by
                    #9

                    @SGaist
                    yes you are right. I'm using QStandardItemModel

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

                      Any help would be appreciated.

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

                        Again, can you show how you initialize your QListView and your model ?

                        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
                        • S Offline
                          S Offline
                          shahriar25
                          wrote on last edited by
                          #12

                          Hi, @SGaist I'm sorry I didn't get the question in the first place
                          the QListview is initialized from the ui with these

                          queueView->setObjectName(QStringLiteral("queueView"));
                          queueView->setEnabled(true);
                          queueView->setFrameShape(QFrame::NoFrame);
                          queueView->setFrameShadow(QFrame::Plain);
                          queueView->setSizeAdjustPolicy(QAbstractScrollArea::AdjustIgnored);
                          queueView->setEditTriggers(QAbstractItemView::NoEditTriggers);
                          queueView->setDragEnabled(false);
                          queueView->setDragDropMode(QAbstractItemView::InternalMove);
                          queueView->setDefaultDropAction(Qt::TargetMoveAction);
                          queueView->setAlternatingRowColors(false);
                          queueView->setSelectionBehavior(QAbstractItemView::SelectItems);
                          queueView->setIconSize(QSize(150, 150));
                          queueView->setMovement(QListView::Snap);
                          queueView->setResizeMode(QListView::Adjust);
                          queueView->setSpacing(0);
                          queueView->setViewMode(QListView::ListMode);
                          queueView->setModelColumn(0);

                          and the model:

                          QStandardItemModel *queueModel;
                          

                          queueModel = new QStandardItemModel(this);

                          ui->queueView->setModel(queueModel);

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            asanka424
                            wrote on last edited by
                            #13

                            Ideally you want the dragged item to be placed between existing items, right? You can change standard item flags to disable drops. (ItemIsDropEnabled).

                            And for getting new index perhaps you can use itemChanged signal?

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

                              Hi @asanka424
                              I tried the Qt::itemIsDropEnabled but the Items in the model get disabled. what should I do?

                              A 1 Reply Last reply
                              0
                              • S shahriar25

                                Hi @asanka424
                                I tried the Qt::itemIsDropEnabled but the Items in the model get disabled. what should I do?

                                A Offline
                                A Offline
                                asanka424
                                wrote on last edited by
                                #15

                                @shahriar25
                                Hi,
                                Did you disable drops in QStandardItem or ListView? You have to disable drops in QStandardItem not in the view
                                setDropEnabled(false)

                                BTW if you want to implement more custom behaviours I encourage to use QAbstractItemModel where you have more control over these operations.

                                Thanks

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

                                  @asanka424
                                  Hi,
                                  I did this in a "for" for every Item:

                                  queueModel->item(i)->setFlags(Qt::ItemIsDropEnabled);

                                  And also I used QStandardModel in my app a lot but if this thing that I want can't be done in QStandardModel I will have to change the model. And also note that QListWidget doesn't have this problem

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    asanka424
                                    wrote on last edited by
                                    #17

                                    this will enable drops in each item in your model. what you should do is disabling it like this

                                    item->setFlags(item->flags() ^ Qt::ItemIsDropEnabled)

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

                                      Hi @asanka424 @SGaist
                                      I was able to set the item flags correctly but now I'm having problem with item changed signal.
                                      How do I know where was the Item (it's row) before it was moved? (I can set the item's data to hold the current row but isn't there a better way?)

                                      And also I was searching the QStandardItemModel's signals to find something useful and I found QStandardItemModel::rowsMoved(...) and QStandardItemModel::rowsAboutToBeMoved(...) and I tried them but they don't ge triggred when an item is moved. why is that?

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        asanka424
                                        wrote on last edited by
                                        #19

                                        You can try dataChanged signal but it won't identify moves explicitly.

                                        BTW does drag and drop work as you expected now?

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

                                          Hi @asanka424
                                          Yes it does. if it won't work then I will have to set the user data every time an item moves. but I think there has to be another way.

                                          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