0

I have a QPushButton with related icon, and I add it to a scene:

QPushButton *button = new QPushButton;
button->setIcon(QIcon(myIcon));
buttonWidget = detail->scene()->addWidget(button);
// buttonWidget is declared globally and is a QGraphicsProxyWidget*

Later on, in a different function, the buttonWidget is still accessible. How can I retrieve the QPushButton object from the buttonWidget? I would like to change its icon.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89

2 Answers2

2

Perhaps you could use QGraphicsProxyWidget::widget to get the underlying QWidget* and then use dynamic_cast to cast to QPushButton*:

QPushbutton *otherButton = dynamic_cast<QPushButton*>(buttonWidget->widget());
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
2

You can retrieve the object as a QWidget using QGraphicsProxyWidget::widget and then casting to a QPushButton.

But I'd recommand to make the QPushButton be an attribute of your class (always better that casting). Then you access it later whenevr you want.

button = new QPushButton; // declare button as an attribute of your class in the header file
button->setIcon(QIcon(myIcon));
buttonWidget=detail->scene()->addWidget(button);
jpo38
  • 20,821
  • 10
  • 70
  • 151