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. Can not move widgets in QGraphicsView

Can not move widgets in QGraphicsView

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 2.9k 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.
  • I Offline
    I Offline
    ivanicy
    wrote on 20 Mar 2018, 16:30 last edited by ivanicy
    #1

    Hello!!

    I am trying to move some widgets in a reimplemented QGraphicsView class.

    So, first, I have tried it in a new project with this code:

    #include "mainwindow.h"
    #include <QApplication>
    #include <QtCore>
    #include <QtGui>
    #include <QtWidgets>
    #include <wcameramap.h>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QGraphicsScene scene;
        QGraphicsView  view(&scene);
    
        QGraphicsPixmapItem *item = new QGraphicsPixmapItem();
        item->setFlag(QGraphicsItem::ItemIsMovable, false);
        item->setPixmap(QPixmap(":/image/Resources/plano01.PNG").scaled(580,380));
        scene.addItem(item);
    
        WCameraMap *pCamera = new WCameraMap();
        QGraphicsWidget *parentWidget = new QGraphicsWidget();
        // make parent widget larger that button
        parentWidget->setMinimumSize(QSizeF(40,60));
        parentWidget->setFlags(QGraphicsItem::ItemIsMovable);
        parentWidget->setAutoFillBackground(false);
        scene.addItem(parentWidget);
        QGraphicsProxyWidget *proxy =
                scene.addWidget(pCamera);
        // put your wrapped button onto the parent graphics widget
        proxy->setParentItem(parentWidget);
    
        view.setFixedSize(QSize(600, 400));
        view.show();
        return app.exec();
    }
    

    And this is the result:

    0_1521562304881_be9c4d69-7904-4b24-b17e-dc56fd99168d-image.png

    But, when I try to export this to my project, the widget appears in the top-left corner and I can't move it. In this case, the code is a little more complex:

    In this case, I have an application that opens a window, and this window has a reimplemented QGraphicView class.

    0_1521562890361_c923b52d-503d-4f10-8fdb-bfc1a3788425-image.png

    The reimplemented QGraphicsView class is called mapView and this is its constructor:

    ...
    scene = new QGraphicsScene(this);
    this->setScene(scene);
    item = new QGraphicsPixmapItem();
    scene->addItem(item);
    this->setMouseTracking(true);
    item->setFlag(QGraphicsPixmapItem::ItemIsMovable, false);
    ...
    

    Finally, in the QDialog class, I try to put the widget in its constructor:

    ...
    this->setWindowTitle("Settings");
    this->setFixedSize(1050, 650);
    
    viewerMap = new mapView(this);
    viewerMap->item->setPixmap(QPixmap(":/maps/resources/planos/plano01.PNG"));
    QShowEvent *event = new QShowEvent();
    viewerMap->showEvent(event);
    ui->layoutMap->addWidget(viewerMap);
    
    WCameraMap *pCamera = new WCameraMap();
    QGraphicsWidget *parentWidget = new QGraphicsWidget();
    // make parent widget larger that button
    parentWidget->setMinimumSize(QSizeF(40,60));
    parentWidget->setFlags(QGraphicsItem::ItemIsMovable);
    parentWidget->setAutoFillBackground(false);
    viewerMap->scene->addItem(parentWidget);
    QGraphicsProxyWidget *proxy =
            viewerMap->scene->addWidget(pCamera);
    // put your wrapped button onto the parent graphics widget
    proxy->setParentItem(parentWidget);
    ...
    

    And this is the result:

    0_1521563405641_fc73cd87-4f08-4f02-8384-a4e8e240b0a8-image.png

    The problem, I can't move the camera widget.

    What could be the error?

    Thank you very very much!

    K 1 Reply Last reply 21 Mar 2018, 03:12
    0
    • I ivanicy
      20 Mar 2018, 16:30

      Hello!!

      I am trying to move some widgets in a reimplemented QGraphicsView class.

      So, first, I have tried it in a new project with this code:

      #include "mainwindow.h"
      #include <QApplication>
      #include <QtCore>
      #include <QtGui>
      #include <QtWidgets>
      #include <wcameramap.h>
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
      
          QGraphicsScene scene;
          QGraphicsView  view(&scene);
      
          QGraphicsPixmapItem *item = new QGraphicsPixmapItem();
          item->setFlag(QGraphicsItem::ItemIsMovable, false);
          item->setPixmap(QPixmap(":/image/Resources/plano01.PNG").scaled(580,380));
          scene.addItem(item);
      
          WCameraMap *pCamera = new WCameraMap();
          QGraphicsWidget *parentWidget = new QGraphicsWidget();
          // make parent widget larger that button
          parentWidget->setMinimumSize(QSizeF(40,60));
          parentWidget->setFlags(QGraphicsItem::ItemIsMovable);
          parentWidget->setAutoFillBackground(false);
          scene.addItem(parentWidget);
          QGraphicsProxyWidget *proxy =
                  scene.addWidget(pCamera);
          // put your wrapped button onto the parent graphics widget
          proxy->setParentItem(parentWidget);
      
          view.setFixedSize(QSize(600, 400));
          view.show();
          return app.exec();
      }
      

      And this is the result:

      0_1521562304881_be9c4d69-7904-4b24-b17e-dc56fd99168d-image.png

      But, when I try to export this to my project, the widget appears in the top-left corner and I can't move it. In this case, the code is a little more complex:

      In this case, I have an application that opens a window, and this window has a reimplemented QGraphicView class.

      0_1521562890361_c923b52d-503d-4f10-8fdb-bfc1a3788425-image.png

      The reimplemented QGraphicsView class is called mapView and this is its constructor:

      ...
      scene = new QGraphicsScene(this);
      this->setScene(scene);
      item = new QGraphicsPixmapItem();
      scene->addItem(item);
      this->setMouseTracking(true);
      item->setFlag(QGraphicsPixmapItem::ItemIsMovable, false);
      ...
      

      Finally, in the QDialog class, I try to put the widget in its constructor:

      ...
      this->setWindowTitle("Settings");
      this->setFixedSize(1050, 650);
      
      viewerMap = new mapView(this);
      viewerMap->item->setPixmap(QPixmap(":/maps/resources/planos/plano01.PNG"));
      QShowEvent *event = new QShowEvent();
      viewerMap->showEvent(event);
      ui->layoutMap->addWidget(viewerMap);
      
      WCameraMap *pCamera = new WCameraMap();
      QGraphicsWidget *parentWidget = new QGraphicsWidget();
      // make parent widget larger that button
      parentWidget->setMinimumSize(QSizeF(40,60));
      parentWidget->setFlags(QGraphicsItem::ItemIsMovable);
      parentWidget->setAutoFillBackground(false);
      viewerMap->scene->addItem(parentWidget);
      QGraphicsProxyWidget *proxy =
              viewerMap->scene->addWidget(pCamera);
      // put your wrapped button onto the parent graphics widget
      proxy->setParentItem(parentWidget);
      ...
      

      And this is the result:

      0_1521563405641_fc73cd87-4f08-4f02-8384-a4e8e240b0a8-image.png

      The problem, I can't move the camera widget.

      What could be the error?

      Thank you very very much!

      K Offline
      K Offline
      kenchan
      wrote on 21 Mar 2018, 03:12 last edited by kenchan
      #2

      @ivanicy
      Why do you need to put the camera into a parent widget and the parent widget into a proxy widget, will it not function as required on its own?

      I 1 Reply Last reply 21 Mar 2018, 08:34
      0
      • K kenchan
        21 Mar 2018, 03:12

        @ivanicy
        Why do you need to put the camera into a parent widget and the parent widget into a proxy widget, will it not function as required on its own?

        I Offline
        I Offline
        ivanicy
        wrote on 21 Mar 2018, 08:34 last edited by
        #3

        @kenchan I think I have to do it for adding a custom widget (my camera) to the qgraphicsscene. Using only the proxy widget, it doesn't work. But I do not know exactly, because it's an example I found on the web.

        K 1 Reply Last reply 21 Mar 2018, 13:40
        0
        • I ivanicy
          21 Mar 2018, 08:34

          @kenchan I think I have to do it for adding a custom widget (my camera) to the qgraphicsscene. Using only the proxy widget, it doesn't work. But I do not know exactly, because it's an example I found on the web.

          K Offline
          K Offline
          kenchan
          wrote on 21 Mar 2018, 13:40 last edited by
          #4

          @ivanicy
          If you just want to draw the camera, drag it around etc. you can do all that if it is a custom QGraphicsItem. I am just not sure why you need to wrap inside two other widgets. If you need the functionality of the widget then, I guess so but, it is not clear to me what you need which a simple QGraphicsItem can provide.
          I am of course assuming the camera is a QGraphicsItem.

          I 1 Reply Last reply 21 Mar 2018, 16:08
          0
          • K kenchan
            21 Mar 2018, 13:40

            @ivanicy
            If you just want to draw the camera, drag it around etc. you can do all that if it is a custom QGraphicsItem. I am just not sure why you need to wrap inside two other widgets. If you need the functionality of the widget then, I guess so but, it is not clear to me what you need which a simple QGraphicsItem can provide.
            I am of course assuming the camera is a QGraphicsItem.

            I Offline
            I Offline
            ivanicy
            wrote on 21 Mar 2018, 16:08 last edited by
            #5

            @kenchan I need a custom widget because it is formed by three QLabels, the first one is the camera image, and the others are the camera name and the camera status respectively. So I think that this is imposible to do with a simple QGraphicsItem. I need to move this custom widget around the scene and get its coordinates for saving its position.

            K 1 Reply Last reply 21 Mar 2018, 22:34
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 21 Mar 2018, 21:37 last edited by
              #6

              Hi,

              Why ? You can create a QImage on which you start by painting the camera image, then the camera status and finally the name all using QPainter.

              You can trigger an update to the item when one of the properties is changed and then refresh the images.

              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
              • I ivanicy
                21 Mar 2018, 16:08

                @kenchan I need a custom widget because it is formed by three QLabels, the first one is the camera image, and the others are the camera name and the camera status respectively. So I think that this is imposible to do with a simple QGraphicsItem. I need to move this custom widget around the scene and get its coordinates for saving its position.

                K Offline
                K Offline
                kenchan
                wrote on 21 Mar 2018, 22:34 last edited by
                #7

                @ivanicy
                As @SGaist say you can do all of that inside the paint function of a custom QGraphicsItem using a QPainter.
                You don't need to complicate things using different widgets which are a bit overkill for this.

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  ivanicy
                  wrote on 22 Mar 2018, 07:37 last edited by ivanicy
                  #8

                  Does anybody have an example of QPainter drawing? I don't know if I could draw this with QPainter... With QPainter could I use a QPixmap with the image?

                  K 1 Reply Last reply 22 Mar 2018, 07:59
                  0
                  • I ivanicy
                    22 Mar 2018, 07:37

                    Does anybody have an example of QPainter drawing? I don't know if I could draw this with QPainter... With QPainter could I use a QPixmap with the image?

                    K Offline
                    K Offline
                    kenchan
                    wrote on 22 Mar 2018, 07:59 last edited by
                    #9

                    @ivanicy
                    Here is a simple example of a custom QGraphicsItem using QPainter and with some interaction with the cursor. It might help to start you off.
                    https://forum.qt.io/topic/87873/highlight-qgraphicsitem/30

                    Yes, you can use images. If you need to you can do image composition with the QPainter. Its up to you what you draw, just make sure you update the bounding box and the shape correctly and things will work fine.
                    If you search the forum for stuff about QGraphicsItems you should find many references to give you hints.

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      ivanicy
                      wrote on 22 Mar 2018, 08:24 last edited by
                      #10

                      Thank you very much both of you @kenchan @SGaist. Let's try it!

                      1 Reply Last reply
                      0

                      5/10

                      21 Mar 2018, 16:08

                      • Login

                      • Login or register to search.
                      5 out of 10
                      • First post
                        5/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved