6

How do I draw interactive widgets such as QButtons and Line Edits over a QGraphicsView? For ex, I have selected a region over an image in an image editing app which displays an image with QGraphicsView, and I want to annotate this region with a name.

So I want to have a Line edit and two buttons (Cross and Tick) below this rectangular selection. How do I draw these?

Sample code would be cool!

Aditya Bhatt
  • 63
  • 1
  • 1
  • 3

2 Answers2

6

QGraphicsScene has a function addWidget() where you can add a widget to a scene. If you don't want to go through the scene addWidget function you can create a QGraphicsProxyWidget use setWidget() and add the proxy widget to your scene.

Harald Scheirich
  • 9,676
  • 29
  • 53
4

You can just add these as you would do with any other control. I used Qt's Designer to generate 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));
        }
};
Gianni
  • 4,300
  • 18
  • 24
  • @Phlip There is no layout, only geometry. http://stackoverflow.com/a/2296040/1090455 – K3---rnc Feb 08 '16 at 15:43
  • 1
    that sez "add the progress bar as child of your QWidget without adding it in the layout." Tx - if this means a object->addChild(x) or similar is available. I have almost no Qt projects on any desktops right now; exigencies of the econ boom. – Phlip Feb 12 '16 at 16:29