Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I get the items selected from a QTreeview?
Qt 6.12 LTS is out! See what's new in the release blog

How do I get the items selected from a QTreeview?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 5.9k 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.
  • RatzzR Offline
    RatzzR Offline
    Ratzz
    wrote on last edited by
    #1

    I am trying to drag an item from the treeview to listview.
    listview is promoted and the dropEvent is shown below.
    How can I get the dropped item from the row , col or mimedata ?

    void dropEvent(QDropEvent* event)
        {
    
            const QMimeData* mime = event->mimeData();
            QByteArray encoded = mime->data("application/x-qabstractitemmodeldatalist");
            QDataStream stream(&encoded, QIODevice::ReadOnly);
    
            while (!stream.atEnd()) {
                int row, col;
                QMap<int,  QVariant> roleDataMap;
                stream >> row >> col >> roleDataMap;
                event->setDropAction(Qt::MoveAction);
                event->accept();
    
            }
        }

    --Alles ist gut.

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

      Hi,

      You should take a look at the Using drag and drop with item views chapter of Qt's documentation.

      It explains what you need to handle that.

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

      RatzzR 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You should take a look at the Using drag and drop with item views chapter of Qt's documentation.

        It explains what you need to handle that.

        RatzzR Offline
        RatzzR Offline
        Ratzz
        wrote on last edited by Ratzz
        #3

        @SGaist
        Thanks for the reply. Qt's documentation was helpful.
        I was able to find the index of selected items in the dropEventusing

                QTreeView * view= dynamic_cast<QTreeView*>(event->source());
                QModelIndexList list= view->selectionModel()->selectedIndexes();
        

        Is there any wayto do it in mimeData?

        --Alles ist gut.

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

          That's really not the role of mime data. What do you want to do with that information ?

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

          RatzzR 1 Reply Last reply
          0
          • SGaistS SGaist

            That's really not the role of mime data. What do you want to do with that information ?

            RatzzR Offline
            RatzzR Offline
            Ratzz
            wrote on last edited by
            #5

            @SGaist
            I just wanted to get the index of selected items from the tree. Just taught it can be done using mimeData or not .

            --Alles ist gut.

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

              Again passing object through mime data is not the right thing to do. But without knowing what you want to do with the indexes, it's pretty difficult to find a suitable solution.

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

              RatzzR 1 Reply Last reply
              0
              • SGaistS SGaist

                Again passing object through mime data is not the right thing to do. But without knowing what you want to do with the indexes, it's pretty difficult to find a suitable solution.

                RatzzR Offline
                RatzzR Offline
                Ratzz
                wrote on last edited by
                #7

                @SGaist
                With index i want to restrict particular items to be dragged from treeview to listview

                --Alles ist gut.

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

                  What are the restriction rules ?

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

                  RatzzR 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    What are the restriction rules ?

                    RatzzR Offline
                    RatzzR Offline
                    Ratzz
                    wrote on last edited by
                    #9

                    @SGaist
                    As you see here only messages (Here message1)has to be dragged.

                    --Alles ist gut.

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

                      In that case, re-implement the flags method of your model. Then you can disable there the possibility to drag items that are not messages.

                      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
                      1

                      • Login

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