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. [solved]Drag and drop between two QGraphicViews in single widget
Qt 6.11 is out! See what's new in the release blog

[solved]Drag and drop between two QGraphicViews in single widget

Scheduled Pinned Locked Moved General and Desktop
21 Posts 2 Posters 9.7k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #10

    Did you add the QGraphicsPixmapItem to the scene ?
    Did you move it to a visible position ?

    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
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #11

      Hi,
      Yeah i have actually promoted the class to the QGraphicsView created using QTCreator.

      @

      #include "gview.h"
      #include<QGraphicsPixmapItem>
      #include <QtGui>

      gview::gview(QWidget *parent)
      : QGraphicsView(parent)
      {
      setMinimumSize(200, 200);
      setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
      setAlignment(Qt::AlignCenter);
      setAcceptDrops(true);
      setAutoFillBackground(true);
      clear();

      }

      void gview::dragEnterEvent(QDragEnterEvent *event)
      {
      //s setText(tr("<drop content>"));
      setBackgroundRole(QPalette::Highlight);
      //const QMimeData *mimeData = event->mimeData();
      //if (mimeData->hasImage()) {
      // event->acceptProposedAction();
      // update();
      // } else {
      event->setAccepted(true);
      }

      //}

      void gview::dragMoveEvent(QDragMoveEvent *event)
      {
      event->acceptProposedAction();
      }

      void gview::dropEvent(QDropEvent *event)
      {
      const QMimeData *mimeData=event->mimeData();
      if(mimeData->hasImage()){
      QGraphicsView *v = new QGraphicsView();
      QGraphicsScene *scn = new QGraphicsScene(v);
      // QPixmap pixmap= QPixmap::fromImage(qvariant_cast<QImage>(mimeData->imageData()));
      // item->setPixmap(QPixmap::fromImage(qvariant_cast<QImage>(mimeData->imageData())));
      item->setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
      scn->addItem(item);
      v->setScene(scn);
      v->show();
      // }
      //else if (mimeData->hasHtml()) {
      //s setText(mimeData->html());
      //s setTextFormat(Qt::RichText);
      // else if (mimeData->hasText()) {
      //s setText(mimeData->text());
      //s setTextFormat(Qt::PlainText);
      // else if (mimeData->hasUrls()) {
      // QList<QUrl> urlList = mimeData->urls();
      //QString text;
      //for (int i = 0; i < urlList.size() && i < 32; ++i) {
      // QString url = urlList.at(i).path();
      //text += url + QString("\n");
      //return(text);
      }
      //} else {
      //label->setText(tr("Cannot display data"));
      //event->setAccepted(false);
      //}
      //event->acceptProposedAction();

      //}
      void gview::dragLeaveEvent(QDragLeaveEvent *event)
      {
      // clear();
      event->accept();
      }

      void gview::clear()
      {
      // setText(tr("<drop content>"));
      // setBackgroundRole(QPalette::Dark);

      emit changed();
      

      }

      @

      Please do guide me whats going wrong here.

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

        You should not create a new scene and a new view each time you drop.
        Just set a scene on your view after you created it and add your new item to that scene.

        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
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #13

          Hi again,

          But how to access the graphicsView which is the object of the @Widget::ui->graphicsView@ This cannot be accessed directly as its from a non- static member. It would be great if you could provide me the code how to use it in another class gview, for setting the scene in this view @ graphicsView and graphicsView2 @ Both are QGraphicView created using QTDesigner

          Thanks and regards
          Venkatesh Padmanabhan

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

            Create the scenes in Widget's constructor and set them your graphics views using setScene and then access them in your drop event using the scene() function

            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
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #15

              Hi,
              Thanks for your valuable response. I have a problem, how to access the scenes declared in the constructor of WIDGET using scene() in drop event. I couldn't find a method to access those @ scn1 and scn @
              declared in Widget using @ scene() @

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

                You have to call it on the views you are implementing

                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
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #17

                  I have created an object of class Widget in class gview to call upon @graphicsView1
                  graphicsView2
                  @ and created new scenes to accommodate it in gview, but it gets terminated when application starts to run.

                  As you guided, how can I call upon the class using any other method?

                  I have tried @

                  Widget *wid = new Widget();
                  wid->ui->scn1->additem(item);
                  wid->ui->scn2->additem(item);
                  update();

                  @
                  The item is the qgraphicspixmapitem, declared in gview as a pointer. I am not sure whether declaration is correct too

                  @ QGraphicsPixmapItem *item; @

                  And used it for the variant_cast. I am not sure what's the mistake I am doing, as the application terminates as soon as I drop the image into the view.

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

                    Hi SGaint,
                    Atlast i was able to drag and drop the images into the qgraphicsview.

                    I used
                    @ scene()->setPixmap(pixmap); @

                    Thank you so much for your wonderful guidance.

                    Regards,
                    Venkatesh Padmanabhan.

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

                      You're welcome

                      Don't forget to update the thread's title prepending solved so other forum users may know that a solution has been found :)

                      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
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #20

                        Hi,
                        I would like to ask one more query related to the original post. I am able to drag and drop images into the view now. But it doesn't work with Url's from local file, if i want to drag an image from the desktop into QGraphicsView it doesn't work as i think it is passed as Url not as an image i hope. It would be great help if you could suggest how to cast an Url into QPixmap... Awaiting your response.

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

                          I'd rather answer on your other "post":http://qt-project.org/forums/viewthread/31764/

                          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

                          • Login

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