簡體   English   中英

在QGraphicsView / QGraphicsScene中的QGraphicsProxyWidget中移動嵌入式窗口小部件

[英]Move an embedded widget in QGraphicsProxyWidget in a QGraphicsView/QGraphicsScene

我試圖用鼠標在QGraphicsView上移動按鈕,但無法正常工作,某些機構可以幫助我解決我的問題嗎?

int main(int argc, char *argv[]) {
  QApplication app(argc, argv);
  MainWindow windows;
  QGraphicsScene scene(&windows);
  QGraphicsView view(&scene, &windows);
  QGraphicsProxyWidget *proxy = scene.addWidget(new QPushButton("MOVE IT"));
  proxy->setFlags(QGraphicsItem::ItemIsMovable |
                  QGraphicsItem::ItemIsSelectable |
                  QGraphicsItem::ItemSendsGeometryChanges |
                  QGraphicsItem::ItemSendsScenePositionChanges);
  windows.setCentralWidget(&view);
  windows.show();
  return app.exec();
}

PD:請忽略我的內存泄漏。

QGraphicsProxyWidget只是任何普通QWidget的包裝,因此沒有可單擊的表面。 所有的單擊事件都由嵌套的QPushButton

在下面的示例中,我添加了一個額外的parentWidget ,它具有較大的面積:

#include <QtCore>
#include <QtGui>
#include <QtWidgets>

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

  QGraphicsScene scene;
  QGraphicsView  view(&scene);

  QGraphicsWidget* parentWidget = new QGraphicsWidget();

  // make parent widget larger that button
  parentWidget->setMinimumSize(QSizeF(100, 30));
  parentWidget->setFlags(QGraphicsItem::ItemIsMovable);
  parentWidget->setAutoFillBackground(true);
  scene.addItem(parentWidget);

  QGraphicsProxyWidget *proxy =
    scene.addWidget(new QPushButton("MOVE IT"));
  // put your wrapped button onto the parent graphics widget
  proxy->setParentItem(parentWidget);

  view.setFixedSize(QSize(600, 400));
  view.show();
  return app.exec();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM