QObject connection, why do I have to add sender twice?
-
This function find QPushButton by their name.
QPushButton *MainWindow::findByObjName(QWidget *widget, QString objName) { return widget->findChild<QPushButton *>(objName); }
findByObjName(widget,"pushButton_xx")->connect(findByObjName(widget,"pushButton_xx"), &QPushButton::clicked, this, [=]{MainWindow::function(widget);});
Why do I have to add
findByObjName(widget,"pushButton_xx")
twice?
Is there a case where I need to use this method? -
@kocka said in QObject connection, why do I have to add sender twice?:
Okey, I but why there is a opportunity, to call
connect
as a reference of individual member of QPushButton class?C++ allows accessing a static class member via the scope operator, eg Class::member, or via an object that is an instance of the class, such as objectPtr->member or objectRef.member.
https://en.cppreference.com/w/cpp/language/static -
@kocka
you dont have to.You can remove it when you are inside a QObject, or add
QObject::
namespace to it -
The connect just needs to be run on QObject. I believe connect is static.
QObject::connect(findByObjName(widget,"pushButton_xx"), &QPushButton::clicked, this, [=]{MainWindow::function(widget);}); // Also, you can store your object for reuse: auto foundObject = findByObjName(widget,"pushButton_xx"); QObject::connect(foundObject, &QPushButton::clicked, this, [=]{MainWindow::function(widget);});
This works because QPushButton base class is QObject.
-
@kocka
because the connect method is public in the QObject class and QWidget inherits QObject -
@kocka said in QObject connection, why do I have to add sender twice?:
Okey, I but why there is a opportunity, to call
connect
as a reference of individual member of QPushButton class?C++ allows accessing a static class member via the scope operator, eg Class::member, or via an object that is an instance of the class, such as objectPtr->member or objectRef.member.
https://en.cppreference.com/w/cpp/language/static