using sender() in a namespace
-
I have created a namespace, there is no class in there, there are some function in that namespace. I would like to use the following code:
QPushButton* button = (QPushButton*)sender(); QString buttonStr = button->text();
I am getting this error:
error: use of undeclared identifier 'sender'
If I using the code in the
mainwindow.cpp
file, there is no problem in there. -
Hi
You cant use sender() from a free function.
Has to be part of QObject.
https://doc.qt.io/qt-5/qobject.html#senderCan you show more of what you are trying as maybe you can use a c++ lambda?
-
There are couple of QPushButton and I get the PushButton text and add that text to QLineEdit.
void getInput(QLineEdit* lineEdit) { QString textBoxStr =lineEdit->text(); QPushButton* button = (QPushButton*)sender(); QString buttonStr = button->text(); textBoxStr += buttonStr; lineEdit->setText(textBoxStr); }
In the mainwindow constructor I create and connect the push buttons with
getInput
function using the following code:QList<QPushButton*> listFrameGetInput = frameGetInput->findChildren<QPushButton*>(); foreach(QPushButton* b, listFrameGetInput ) connect(b, &QPushButton::clicked, this, [=]{names::getInput(lineEdit);});
When I create the push buttons I add them to a QFrame.
I just want to get the button text in the
getInput
function