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.
  • 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
                                • A asanka424

                                  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 Offline
                                  kshegunovK Offline
                                  kshegunov
                                  Moderators
                                  wrote on last edited by kshegunov
                                  #21

                                  @asanka424

                                  what you should do is disabling it like this

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

                                  Beware of such handling of flags!

                                  It's a very wrong way to do it. If the flag is not set you'll actually enable it, additionally it doesn't work with compound flags! Suppose you have (in binary) a = 010 and b = 110, then a ^ b == 100 which is very different from the expected 000.

                                  The proper way to remove a set of bits is to AND the inversion: a & ~b == 000
                                  /and this can be rigorously proven to be different from a ^ b if you expand the xor in the other basic operations a ^ b = (a & ~b) | (~a & b)/

                                  Read and abide by the Qt Code of Conduct

                                  A 1 Reply Last reply
                                  1
                                  • S Offline
                                    S Offline
                                    shahriar25
                                    wrote on last edited by
                                    #22

                                    Hi @kshegunov . Thank you for correcting that I cahnged my code.
                                    I tried the item changed flag and the data of item but it didn't work. I really don't know what to do

                                    kshegunovK 1 Reply Last reply
                                    0
                                    • S shahriar25

                                      Hi @kshegunov . Thank you for correcting that I cahnged my code.
                                      I tried the item changed flag and the data of item but it didn't work. I really don't know what to do

                                      kshegunovK Offline
                                      kshegunovK Offline
                                      kshegunov
                                      Moderators
                                      wrote on last edited by
                                      #23

                                      @shahriar25
                                      Well I don't know, I can't spot anything plainly wrong, although there isn't much code to begin with. However, this line:

                                      queueView->setDragEnabled(false);
                                      

                                      does look suspicious, have you removed it as it had been suggested, because it's not clear from your answer to @asanka424's question.

                                      Read and abide by the Qt Code of Conduct

                                      1 Reply Last reply
                                      0
                                      • kshegunovK kshegunov

                                        @asanka424

                                        what you should do is disabling it like this

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

                                        Beware of such handling of flags!

                                        It's a very wrong way to do it. If the flag is not set you'll actually enable it, additionally it doesn't work with compound flags! Suppose you have (in binary) a = 010 and b = 110, then a ^ b == 100 which is very different from the expected 000.

                                        The proper way to remove a set of bits is to AND the inversion: a & ~b == 000
                                        /and this can be rigorously proven to be different from a ^ b if you expand the xor in the other basic operations a ^ b = (a & ~b) | (~a & b)/

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

                                        @kshegunov
                                        Yes agree with you. I just replied for the context where I knew the flag is set.

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

                                          Hi @asanka424 @kshegunov
                                          Sorry for replying late.
                                          an interesting thing happened:
                                          when I set the flags of the items to
                                          queueModel->item(i)->setFlags(queueModel->item(i)->flags() & !Qt::ItemIsDropEnabled);
                                          the items get disabled!
                                          bot when I do:
                                          queueModel->item(i)->setFlags(queueModel->item(i)->flags() ^ Qt::ItemIsDropEnabled);
                                          the is no problem. I think I'm doing something wrong.

                                          ANd I will try:
                                          queueView->setDragEnabled(false);
                                          and get back with a result. thank all of you for answering again. I love this from and the people who answer the questions

                                          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