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. Dragging QListWidgetItem into QGraphicScene and create QGraphicsItem

Dragging QListWidgetItem into QGraphicScene and create QGraphicsItem

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.8k Views
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hey.

    I do have an object in my software which I have declared a QMetaType so I can store it in a QVariant. Now I have a QListWidget that shows all instances of this object with its value stored inside the individual QListWidgetItems as UserType.

    Module module("Module A");
    QListWidget* list = new QListWidget();
    [...]
    
    QListWidgetItem* item = new QListWidgetItem(module.name());
    item->setData(Qt::UserRole, QVariant::fromValue(module));
    list->addItem(item);
    

    Now I want to drag this item onto a QGraphicsScene and extract the QVariant contained in the QListWidgetItem inside the QGraphicsScene::dropEvent(...) and create a new custom QGraphicsItem that is a graphical representation of the Module there, as well.

    I was reading in the Drag & Drop Framework documentation as well as the documentation for QGraphicsView but since the QListWidgetItem supports drag&drop by default, I was wondering if there isn't an easy way to get this done.

    Any help is really appreciated!

    raven-worxR 1 Reply Last reply
    0
    • ? A Former User

      Hey.

      I do have an object in my software which I have declared a QMetaType so I can store it in a QVariant. Now I have a QListWidget that shows all instances of this object with its value stored inside the individual QListWidgetItems as UserType.

      Module module("Module A");
      QListWidget* list = new QListWidget();
      [...]
      
      QListWidgetItem* item = new QListWidgetItem(module.name());
      item->setData(Qt::UserRole, QVariant::fromValue(module));
      list->addItem(item);
      

      Now I want to drag this item onto a QGraphicsScene and extract the QVariant contained in the QListWidgetItem inside the QGraphicsScene::dropEvent(...) and create a new custom QGraphicsItem that is a graphical representation of the Module there, as well.

      I was reading in the Drag & Drop Framework documentation as well as the documentation for QGraphicsView but since the QListWidgetItem supports drag&drop by default, I was wondering if there isn't an easy way to get this done.

      Any help is really appreciated!

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @tobiSF
      IIRC thats not cleanly possible with QListWidget.

      the model needs to provide the drop-data and a mime-type you can listen to in the QGraphicsScene (see this). QListWidget / QStandardItemModel lacks such a possibility.
      In the QGraphicsScene dropEvent handler you then can listen for your custom mime-type and read the drop-data (item-type, content,...) from the model.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @raven-worx, I was afraid this was the answer. Which is too bad, when seemingly everything you'd need is in place on one side of the drag&drop operation and you would only need to access that information on the receiving end.

        raven-worxR 1 Reply Last reply
        0
        • ? A Former User

          @raven-worx, I was afraid this was the answer. Which is too bad, when seemingly everything you'd need is in place on one side of the drag&drop operation and you would only need to access that information on the receiving end.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @tobiSF
          it's not that bad. All you have to do is just implement a new model class.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          ? 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @tobiSF
            it's not that bad. All you have to do is just implement a new model class.

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @raven-worx not bad in a hard/difficult way. More disappointing that there's no straightforward way. Thank you, anyway!

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              I did not test it but it looks like everything is already in Qt, just not documented.

              void dropEvent(QGraphicsSceneDragDropEvent *event){
              	const QByteArray mimeData = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
              	QDataStream mimeReader(mimeData);
              	int row,col;
              	QMap<int,QVariant> modelData;
              	for(;;){
              		mimeReader.startTransaction();
              		mimeReader >> row >> col >> modelData;
              		if(!mimeReader.commitTransaction())
              			break;
              		const auto userRoleIter = modelData.constFind(Qt::UserRole);
              		if(userRoleIter!=modelData.cend())
              			doSomethingWithModule(userRoleIter->value<Module>());
              	}
              }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                @VRonin said in Dragging QListWidgetItem into QGraphicScene and create QGraphicsItem:

                const QByteArray mimeData = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
                QDataStream mimeReader(mimeData);
                int row,col;
                QMap<int,QVariant> modelData;
                for(;;){
                mimeReader.startTransaction();
                mimeReader >> row >> col >> modelData;
                if(!mimeReader.commitTransaction())
                break;
                const auto userRoleIter = modelData.constFind(Qt::UserRole);
                if(userRoleIter!=modelData.cend())
                doSomethingWithModule(userRoleIter->value<Module>());
                }

                @VRonin, thank you very much. This works like a charm and does exactly what I was looking for.

                raven-worxR 1 Reply Last reply
                0
                • ? A Former User

                  @VRonin said in Dragging QListWidgetItem into QGraphicScene and create QGraphicsItem:

                  const QByteArray mimeData = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
                  QDataStream mimeReader(mimeData);
                  int row,col;
                  QMap<int,QVariant> modelData;
                  for(;;){
                  mimeReader.startTransaction();
                  mimeReader >> row >> col >> modelData;
                  if(!mimeReader.commitTransaction())
                  break;
                  const auto userRoleIter = modelData.constFind(Qt::UserRole);
                  if(userRoleIter!=modelData.cend())
                  doSomethingWithModule(userRoleIter->value<Module>());
                  }

                  @VRonin, thank you very much. This works like a charm and does exactly what I was looking for.

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #8

                  @tobiSF
                  @VRonin
                  that's what i meant it's not "cleanly possible". I knew that some internal data was in the mime-data, but i believed that you need a reference to the model afterall, thus "not cleanly". But thats not the case.

                  Sorry for not being more specific on this.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  ? 1 Reply Last reply
                  2
                  • raven-worxR raven-worx

                    @tobiSF
                    @VRonin
                    that's what i meant it's not "cleanly possible". I knew that some internal data was in the mime-data, but i believed that you need a reference to the model afterall, thus "not cleanly". But thats not the case.

                    Sorry for not being more specific on this.

                    ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    @raven-worx no worries, your help is appreciated and I think I understood you correctly.

                    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