Can not move widgets in QGraphicsView



  • 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!



  • @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?



  • @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.



  • @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.



  • @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.


  • Lifetime Qt Champion

    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.



  • @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.



  • 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?



  • @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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.