1

I am trying to move with the mouse a push button on a QGraphicsView but is not working, some body can help me whit my issue?

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: Ignore my memory leaks here please.

1 Answers1

3

QGraphicsProxyWidget is just a wrapper for any normal QWidget, hence it has no own surface to click on. All the click events are consumed by the nested QPushButton.

In the example below I have added an extra parentWidget which has a larger area:

#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();
}
paceholder
  • 1,064
  • 1
  • 10
  • 21