Passing slot as parameter in function to connect to a signal
-
Hi,
I'm trying to connect a signal to a slot that I have received as const char* in my function but I'm doing something wrong.
This is the function that create a QPushButton and connect the signal click to my slot.void addPushButtonInTableCell(U2bYTES row, U2bYTES column, U2bYTES numberToPushButton, QTableWidget * pTableWidget , QWidget* parent, QString ruta, const char* slot) { QWidget *pWidget = new QPushButton(QString::number(numberToPushButton),parent); ... QObject::connect(pWidget, SIGNAL(clicked()),parent, SLOT(slot)); }
them I invoke the function.
addPushButtonInTableCell(0,15,pQ->u.cue.n, tableWidget , pMW->options, ":/ui/recursos/ExtrasInsert.png","on_pushButton_newJump_clicked()");
It compile and give to me next warning message
warning: unused parameter ‘slot’ [-Wunused-parameter] void addPushButtonInTableCell(U2bYTES row, U2bYTES column, U2bYTES numberToPushButton, QTableWidget * pTableWidget , QWidget* parent, QString ruta, const char* slot) ^
when I execute it i received this message on qdebug
QObject::connect: Parentheses expected, slot optionstouch::slot in ../motor/lib.cpp:26206 QObject::connect: (receiver name: 'optionstouch')
-
Hi,
I'm trying to connect a signal to a slot that I have received as const char* in my function but I'm doing something wrong.
This is the function that create a QPushButton and connect the signal click to my slot.void addPushButtonInTableCell(U2bYTES row, U2bYTES column, U2bYTES numberToPushButton, QTableWidget * pTableWidget , QWidget* parent, QString ruta, const char* slot) { QWidget *pWidget = new QPushButton(QString::number(numberToPushButton),parent); ... QObject::connect(pWidget, SIGNAL(clicked()),parent, SLOT(slot)); }
them I invoke the function.
addPushButtonInTableCell(0,15,pQ->u.cue.n, tableWidget , pMW->options, ":/ui/recursos/ExtrasInsert.png","on_pushButton_newJump_clicked()");
It compile and give to me next warning message
warning: unused parameter ‘slot’ [-Wunused-parameter] void addPushButtonInTableCell(U2bYTES row, U2bYTES column, U2bYTES numberToPushButton, QTableWidget * pTableWidget , QWidget* parent, QString ruta, const char* slot) ^
when I execute it i received this message on qdebug
QObject::connect: Parentheses expected, slot optionstouch::slot in ../motor/lib.cpp:26206 QObject::connect: (receiver name: 'optionstouch')
@JonexElectronic I'm hesitant to help, because your approach smells like a fundamental problem in the setup of what you want to do.
anyway, your
slot
function argument is already aconst char*
, so no need to make it one, with theSLOT
macro.-> QObject::connect(pWidget, SIGNAL(clicked()),parent,slot);
-
@JonexElectronic I'm hesitant to help, because your approach smells like a fundamental problem in the setup of what you want to do.
anyway, your
slot
function argument is already aconst char*
, so no need to make it one, with theSLOT
macro.-> QObject::connect(pWidget, SIGNAL(clicked()),parent,slot);
@J-Hilk said in Passing slot as parameter in function to connect to a signal:
so no need to make it one, with the SLOT macro
the SLOT macro does a little bit more than just casting it to a const char*
but i agree, connecting a slot on a signal invocation doesnt seem right
-
@J-Hilk said in Passing slot as parameter in function to connect to a signal:
so no need to make it one, with the SLOT macro
the SLOT macro does a little bit more than just casting it to a const char*
but i agree, connecting a slot on a signal invocation doesnt seem right
@J-Hilk @raven-worx If I do not use SLOT I received the next qDebug message.
QObject::connect: Use the SLOT or SIGNAL macro to connect optionstouch::on_pushButton_newJump_clicked()
I do not understand what do you mean with "because your approach smells like a fundamental problem in the setup of what you want to do."
I just create a QPushButton and insert it in QTableWidget and I want to execute the funtion that I passed in my slot when the button is clicked.
how should I do that? Is there a better way to do it? -
@J-Hilk said in Passing slot as parameter in function to connect to a signal:
so no need to make it one, with the SLOT macro
the SLOT macro does a little bit more than just casting it to a const char*
but i agree, connecting a slot on a signal invocation doesnt seem right
@raven-worx said in Passing slot as parameter in function to connect to a signal:
the SLOT macro does a little bit more than just casting it to a const char*
really? 🤔 Probably moc, when its running, right ?
I'm based mine purely on this:
Is there a better way to do it?
probably, but to be honest, I'm unsure, what exactly you want to do:
I want to execute the function that I passed in my slot when the button is clicked
doesn't make much sense to me -
@raven-worx said in Passing slot as parameter in function to connect to a signal:
the SLOT macro does a little bit more than just casting it to a const char*
really? 🤔 Probably moc, when its running, right ?
I'm based mine purely on this:
Is there a better way to do it?
probably, but to be honest, I'm unsure, what exactly you want to do:
I want to execute the function that I passed in my slot when the button is clicked
doesn't make much sense to me@J-Hilk said in Passing slot as parameter in function to connect to a signal:
I'm based mine purely on this:
exactly, and it adds "1" as a prefix. which is then used by the meta-object instance for lookup of the method and check if its really a slot.
-
I have decided to change my funtion.
QWidget* addPushButtonInTableCell(U2bYTES row, U2bYTES column, U2bYTES numberToPushButton, QTableWidget * pTableWidget , QWidget* parent, QString ruta, const char* slot) { QWidget *pWidget = new QPushButton(QString::number(numberToPushButton),parent); ... return pWidget; }
Invoke
QWidget* pWidget = addPushButtonInTableCell(0,15,pQ->u.cue.n, tableWidget , pMW->options, ":/ui/recursos/ExtrasInsert.png"); QObject::connect(pWidget,SIGNAL(clicked()), pMW->options, SLOT(on_pushButton_newJump_clicked()));
It works fine.