Drawing widgets (e.g. buttons) over a QGraphicsView

How to draw interactive widgets like QButtons and Line Edits over QGraphicsView? For example, I selected the area above the image in the image editing application that displays the image with QGraphicsView, and I want to annotate this area with the name.

So, I want to have line editing and two buttons (Cross and Tick) under this rectangular selection. How to do it?

Sample code will be cool!

+1
source share
2 answers

QGraphicsScene has an addWidget() function where you can add a widget to the scene. If you do not want to go through the addWidget function of the scene, you can create a QGraphicsProxyWidget using setWidget() and add a proxy widget to your scene.

+2
source

You can simply add them as you would with any other control. I used Qt Designer to create the following:

 class MyForm: public QMainWindow { private: QGraphicsView *graphicsView; QLineEdit *lineEdit; QPushButton *pushButton; QPushButton *pushButton_2; public: MyForm() { graphicsView = new QGraphicsView(this); graphicsView->setObjectName(QString::fromUtf8("graphicsView")); graphicsView->setGeometry(QRect(130, 90, 441, 191)); lineEdit = new QLineEdit(graphicsView); lineEdit->setObjectName(QString::fromUtf8("lineEdit")); lineEdit->setGeometry(QRect(160, 150, 113, 22)); pushButton = new QPushButton(graphicsView); pushButton->setObjectName(QString::fromUtf8("pushButton")); pushButton->setGeometry(QRect(280, 140, 115, 32)); pushButton_2 = new QPushButton(graphicsView); pushButton_2->setObjectName(QString::fromUtf8("pushButton_2")); pushButton_2->setGeometry(QRect(400, 140, 115, 32)); } }; 
+2
source

All Articles