Laurent Crivello
Laurent Crivello

Reputation: 3931

Retrieving QPushButton from QWidget

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.

Upvotes: 0

Views: 1586

Answers (2)

jpo38
jpo38

Reputation: 21514

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);

Upvotes: 2

LogicStuff
LogicStuff

Reputation: 19607

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());

Upvotes: 2

Related Questions