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. Drag & Drop inside of a ListView
Forum Updated to NodeBB v4.3 + New Features

Drag & Drop inside of a ListView

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 1.3k Views 4 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
    #4

    Do you mean you are using setItemWidget ?

    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
    3
    • WeWon44W Offline
      WeWon44W Offline
      WeWon44
      wrote on last edited by WeWon44
      #5
      void MainView::on_add1_clicked()
      {
            int row =model1->rowCount();
            QStandardItem* newItem = new QStandardItem();
            Activity* act = new Activity(nullptr, this,1, newItem);
            act->resize(ui->listV1->size().width()-2, 42);
            model1->appendRow(newItem);
            model1->item(row)->setSizeHint(act->size());
            ui->listV1->setIndexWidget(model1->item(row)->index(), act);
            
      }
      

      Yes, this is the code for adding a new Activity, the steps are:

      1. get current row
        1.5) see edit
      2. create a new Activity which has null parent, a pointer to MainView (to access the models that are stored as PRIVATE attributes inside of MainView), the last argument tells us which of the 3 lists the activity belongs to
      3. resize the activity to fit the list
      4. append an empty item/row onto the model
      5. resize the newly appended item
      6. set the newly created Activity as the widget for the newly created item

      At least this was my thought process while writing this code

      EDIT: I slightly changed the code, now an Activity has even an "item" pointer attribute, this is very important to perform "setCurrentIndex" on the item linked to the Activity when the "remove" button is pressed, the logic remains the same.

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

        Why exactly do you need to use a widget for your "Activity" ?

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

          An Activity needs to be a label (for example “buy milk”), a checkbox (to toggle the “completed” attribute), an “Edit” button and a “Delete” button.
          Both buttons works perfectly letting me edit the text and remove the Activity from both the listview and the model. Here’s the code for the remove button if it can help

          void MainView::deleteAct(Activity* act)
          {
              int id=act->getListId(); //to know where the Activity has to be removed
              switch(id){
          
                  case(1): { 
                  ui->listV1->setCurrentIndex(act->getItem()->index()); 
                  //selects the item so the model can access it trough "currentIndex" 
                  model1->removeRows(ui->listV1->currentIndex().row(),1);
                  //the row gets removed from the model
                            }
                  break;
          
                 //cases for the other 2 lists
               }
                  }
          

          This gets invoked by the widget by

          void Activity::on_removeButton_clicked()
          {
          
              this->point->deleteAct(this);
          
          }
          

          where "point" is a pointer to the MainView

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

            Hi
            Since Widgets cannot be copied, it's not really possible for Qt to drag-drop any attached widget. (setIndexWidget)

            You might have to handle the drag and drop yourself and recreate the Activity widget for the
            newly dropped item.

            1 Reply Last reply
            2
            • WeWon44W Offline
              WeWon44W Offline
              WeWon44
              wrote on last edited by
              #9

              Thanks for answering, unfortunately I posted here because I don’t know how to do that, could you give me some clues?

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

                If it's a move, get the widget from the original cell, take it and put it back in your target.

                If it's a copy, them create a dump/restore pair of methods that allows you to copy the content of your widget one by one in your "copy".

                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
                • WeWon44W Offline
                  WeWon44W Offline
                  WeWon44
                  wrote on last edited by
                  #11

                  I already thought about the implementation but how do I override the drop method?
                  My issue is similar to this one

                  jsulmJ 1 Reply Last reply
                  0
                  • WeWon44W WeWon44

                    I already thought about the implementation but how do I override the drop method?
                    My issue is similar to this one

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @WeWon44 said in Drag & Drop inside of a ListView:

                    how do I override the drop method?

                    See https://doc.qt.io/qt-5/dnd.html

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • WeWon44W Offline
                      WeWon44W Offline
                      WeWon44
                      wrote on last edited by
                      #13

                      Thanks, that was very useful, I still have doubts on how to implement this correctly (mainly how to get the correct position) since I never manipulated events but I’ll give this a try and share the results in a few hours

                      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