【问题标题】:Adding QGraphicsView and QGraphicsScene to a label in Qt将 QGraphicsView 和 QGraphicsScene 添加到 Qt 中的标签
【发布时间】:2013-10-18 19:01:37
【问题描述】:

我有一个堆叠的小部件,上面有一个 QLabel。我在此标签上显示图像。我想缩放和平移这个图像,我正在尝试使用 QGraphicsView 技术。但是会打开一个新窗口。 这就是我正在做的。

 scene = new QGraphicsScene(this);
 view = new QGraphicsView(this);
 QPixmap pix("/root/Image);
 label->setPixmap(pix);
 scene->addWidget(label);
 view->setScene(scene);
 view->setDragMode(QGraphicsView::scrollHandDrag);
 view->show();

有人可以建议我应该做什么。我希望标签的行为类似于 QGraphicsView。

谢谢你:)

【问题讨论】:

    标签: qt qgraphicsview


    【解决方案1】:

    您创建一个场景和视图并将标签添加到场景中,然后告诉视图显示自己:-

    view->show()
    

    如您所说,如果您希望 QGraphicsView 在标签上,请不要将标签添加到场景中,而是将 QGraphicsView 添加到标签中:-

    QLabel* pLabel = new QLabel(mainWindow); // setting mainWindow as the parent
    QGraphicsView* pView = new QGraphicsView(pLabel) // set the label as the parent of the view.
    

    您不需要调用 show,因为标签会为您处理,假设标签位于已显示的 Widget 上。

    现在,不要在标签上设置像素图,而是在场景中创建像素图:-

    pScene->addPixmap(pixmap);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      • 2020-07-01
      • 1970-01-01
      相关资源
      最近更新 更多