Connecting to slot by string name
-
Hello I have following code:
namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); signals: void testSignal(); public slots: void testSlot(); private: Ui::MainWindow *ui; }; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); bool result = false; result = connect(this, SIGNAL(testSignal()), this, SLOT("testSlot()")); qDebug() << result; } MainWindow::~MainWindow() { delete ui; } void MainWindow::testSlot() { }
but when I ran it I get:
QObject::connect: No such slot MainWindow::"testSlot()" in ../connections/MainWindow.cpp:14
QObject::connect: (sender name: 'MainWindow')
QObject::connect: (receiver name: 'MainWindow')
falseCan you tell me how to properly connect to the slot by its string name? I know hot to use signal and slots but I have no idea how to force it work with the slot name passed by name.
BR,
poorBob -
-
Hello @m-sue ,
which namespace? MainWindow? How to remove it then?
-
@poor_robert said in Connecting to slot by string name:
SLOT("testSlot()"));
Should be
SLOT(testSlot()));
methinks. -
-
@artwaw
of course you're right, but in this case I want to know how to connect with slot name in parenthesis so SLOT("testSlot()")); is exactly what I want to try.@m-sue :
I have simplified the code:class MainWindow : public QObject { Q_OBJECT public: explicit MainWindow(QObject *parent = 0); ~MainWindow(); signals: void testSignal(); public slots: void testSlot(); }; #include "MainWindow.h" #include <QDebug> MainWindow::MainWindow(QObject *parent) { bool result = false; result = connect(this, SIGNAL(testSignal()), this, SLOT("testSlot()")); qDebug() << result; } MainWindow::~MainWindow() { } void MainWindow::testSlot() { }
but I still receive:
QObject::connect: No such slot MainWindow::"testSlot()" in ../connections/MainWindow.cpp:8
falseDo you have any ideas?
-
Hi,
@poor_robert , as told by @artwaw change the connect statement , u cannot
provide the function name in quotes.Both signal and slot function signature has to match.
Thanks,
-
result = connect(this, SIGNAL(testSignal()), this, SLOT(testSlot()));
replace the above line and check.
Thanks,
-
@poor_robert said in Connecting to slot by string name:
of course you're right, but in this case I want to know how to connect with slot name in parenthesis so SLOT("testSlot()")); is exactly what I want to try.
Of course I may be wrong and I would welcome an opinion from someone more advanced in the subject but according to my knowledge this will not work at all.
-
@poor_robert said in Connecting to slot by string name:
I want to know how to connect with slot name in parenthesis so SLOT("testSlot()"));
Why? Why do you want to use "?
-
-
The code given above was just an example. The real reason was to use slot as callback. I have created following method and wanted to do following connection:
void MyClass::setCallback(QObject * obj, const char* slotName) { connect(m_member, SIGNAL(updated()), obj, SLOT(slotName)); }
in my implementation obj is class derived from the QObject. I have solved the problem with help of std::function used as callbacks.
Do you know how to solve this in Qt way? -
-
@poor_robert said in Connecting to slot by string name:
Just use the SLOT at the place where you call the function:
void MyClass::setCallback(QObject * obj, const char* slotName) { connect(m_member, SIGNAL(updated()), obj, slotName); } setCallback(myObj,SLOT(myFunction()));
-Michael.
-
@poor_robert Just pass SLOT(mySlot()) to setCallback :-)
void MyClass::setCallback(QObject * obj, const char* slotStr) { connect(m_member, SIGNAL(updated()), obj, slotStr); } setCallback(obj, SLOT(mySlot()));