How to call a QProperty binding from a subclass?
-
How i could call the
test
binding in this case without calling
qobject_cast
to transform the QWidget back to PushButton?qDebug() << listWidget[0]->property("test"); // return invalid qDebug() < listWidget[0]->test; // error as test is not a property of a QWIDGET qDebug() << qobject_cast<PushButton*>(listWidget[0])->test; // works
class PushButton : public QPushButton { Q_OBJECT public: QProperty<QString> test; PushButton(QWidget* parent = nullptr) : QPushButton(parent) { test.setBinding([&]() { QString string = "OK"; return string; }); } } PushButton* pushButton = new PushButton; QList<QWidget*> listWidget { pushButton };
-
@Kattia said in How to call a QProperty binding from a subclass?:
How i could call the test binding in this case without calling
qobject_cast to transform the QWidget back to PushButton?If your list constains only PushButton objects, you can try:
QList<PushButton*> listWidget { pushButton };
Otherwise a cast is needed.
-
I'm using setProperty to call functions in subclassed classes to avoid casting the widget/including its classes in other files.
Example
class PushButton : public QPushButton { Q_OBJECT Q_PROPERTY(bool etc MEMBER etc WRITE callMyFunction); public: bool etc; // unused void callMyFunction(bool) { myFunction(); } }
Where
w
is aQWidget*
but from typePushButton*
w->setProperty("etc", true); // The etc variable and its value is ignored, i just want it to call a specific function
The other way would be including
"pushbutton.h"
casting theQWidget
toPushButton
and
pushButton->myFunction();
There's a better way to do this?
-
@Kattia said in How to call a QProperty binding from a subclass?:
There's a better way to do this?
No, because there is no virtual function named myFunction anywhere in the class hierarchy. It is defined in PushButton, so you have to cast.