在QGraphicsView上绘制小部件(如按钮)

时间:2010-06-06 18:32:58

标签: qt qgraphicsview qgraphicsitem

如何在QGraphicsView上绘制QButtons和Line Edits等交互式小部件? 例如,我在图像编辑应用程序中选择了一个区域,该应用程序使用QGraphicsView显示图像,我想用名称注释该区域。

所以我希望在这个矩形选区下面有一个Line编辑和两个按钮(Cross和Tick)。 我该如何绘制这些?

示例代码很酷!

2 个答案:

答案 0 :(得分:2)

您可以像对待任何其他控件一样添加这些。我使用Qt的Designer生成以下内容:

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));
        }
};

答案 1 :(得分:2)

QGraphicsScene有一个函数addWidget(),您可以在其中添加窗口小部件。如果您不想浏览场景addWidget函数,可以创建QGraphicsProxyWidget使用setWidget()并将代理窗口小部件添加到场景中。

相关问题