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:
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.
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:
The problem, I can't move the camera widget.
What could be the error?
Thank you very very much!
-
@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.
-
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
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/30Yes, 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.