【问题标题】:Drawing widgets (such as buttons) over QGraphicsView在 QGraphicsView 上绘制小部件(例如按钮)
【发布时间】:2011-02-28 10:43:35
【问题描述】:

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

所以我想在这个矩形选区下方有一个 Line 编辑和两个按钮(Cross 和 Tick)。 我怎么画这些?

示例代码会很酷!

【问题讨论】:

    标签: qt qgraphicsview qgraphicsitem


    【解决方案1】:

    您可以像添加任何其他控件一样添加这些。我使用 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));
            }
    };
    

    【讨论】:

    • @Phlip 没有布局,只有几何。 stackoverflow.com/a/2296040/1090455
    • that sez“将进度条添加为 QWidget 的子项,而不将其添加到布局中。” Tx - 如果这意味着 object->addChild(x) 或类似的可用。我现在几乎没有任何桌面上的 Qt 项目;经济繁荣的紧急情况。
    【解决方案2】:

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

    【讨论】:

    • 谢谢,这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多