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? -
Okey, I but why there is a opportunity, to call
connectas a reference of individual member of QPushButton class?@kocka said in QObject connection, why do I have to add sender twice?:
Okey, I but why there is a opportunity, to call
connectas 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 -
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
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.
-
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.
-
Okey, I but why there is a opportunity, to call
connectas a reference of individual member of QPushButton class?@kocka
because the connect method is public in the QObject class and QWidget inherits QObject -
Okey, I but why there is a opportunity, to call
connectas a reference of individual member of QPushButton class?@kocka said in QObject connection, why do I have to add sender twice?:
Okey, I but why there is a opportunity, to call
connectas 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