QT开发中文网

将QPushButton加入QGraphicsProxyWidget控件中

要将QPushButton添加到QGraphicsProxyWidget控件中,您可以执行以下步骤:

  1. 创建一个QGraphicsScene对象作为场景,并将其设置为视图的场景。
QGraphicsScene scene;
view.setScene(&scene);
  1. 创建一个QGraphicsProxyWidget对象,并将其用于包装QPushButton小部件。
QPushButton* button = new QPushButton("Button");
QGraphicsProxyWidget* proxyWidget = scene.addWidget(button);
  1. 设置按钮在场景中的位置和大小。
proxyWidget->setPos(x, y); // 设置按钮在场景中的位置
proxyWidget->resize(width, height); // 设置按钮的大小

完整的示例代码如下所示:

#include <QApplication>
#include <QGraphicsView>
#include <QPushButton>
#include <QGraphicsProxyWidget>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QGraphicsView view;
    view.setWindowTitle("QPushButton in QGraphicsProxyWidget");

    QGraphicsScene scene;
    view.setScene(&scene);

    QPushButton* button = new QPushButton("Button");
    QGraphicsProxyWidget* proxyWidget = scene.addWidget(button);

    proxyWidget->setPos(100, 100);
    proxyWidget->resize(200, 50);

    view.show();

    return a.exec();
}

在上述示例中,我们创建了一个带有场景和视图的应用程序窗口。然后,我们创建了一个QPushButton对象,并使用addWidget()方法将其封装在一个QGraphicsProxyWidget中。最后,我们设置了该代理小部件的位置和大小,并显示视图。

运行此代码,您将看到按钮被添加到了QGraphicsView中,并且可以在场景中自由移动和调整大小。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=10372

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?