get the return value of the emitted signal
-
MyLineEdit class
virtual void keyPressEvent(QKeyEvent* event) override { if(event->key() == Qt::Key_A || event->key() == Qt::Key_2) { QString keyName = QKeySequence(event->key()).toString(); emit oneDigit() = keyName; } else { event->ignore(); } }
signals: QString oneDigit();
MainWindow constructor connection
connect(line, &MyLineEdit::oneDigit, this, [=]{MainWindow::updateLineEdit(line,***)})
void MainWindow::updateLineEdit(MyLineEdit* line, QString str) { QString txt = line->text(); line->setText(txt + str); }
I would like to give the
oneDigit
signal return value to theupdateLineEdit
function. -
I just want to get the pressed key as a string and add it to the lineedit. Very important is to allow specific key.
I know about lambdas but how to pass the string?
connect(line, &MyLineEdit::KeyOneDigit, this, [=]{MainWindow::updateLineEdit(line, ***);});
@ekato993 said in get the return value of the emitted signal:
I know about lambdas but how to pass the string?
connect(line, &MyLineEdit::KeyOneDigit, this, [=](Qstring){MainWindow::updateLineEdit(line, ***);}); ... emit KeyOneDigit(key);
-
You could do it like that
// In MyLineEdit signals: void oneDigit(QString str); // In MainWindow public slots: void updateLineEdit(QString str) { QString text = line->text(); line->setText(text + str); }
then connect it this way
// MainWindow constructor connect(line, &MyLineEdit::oneDigit, this, &MainWindow::updateLineEdit);
In keyPressEvent send it this way:
emit oneDigit(keyName);
-
MyLineEdit class
virtual void keyPressEvent(QKeyEvent* event) override { if(event->key() == Qt::Key_A || event->key() == Qt::Key_2) { QString keyName = QKeySequence(event->key()).toString(); emit oneDigit() = keyName; } else { event->ignore(); } }
signals: QString oneDigit();
MainWindow constructor connection
connect(line, &MyLineEdit::oneDigit, this, [=]{MainWindow::updateLineEdit(line,***)})
void MainWindow::updateLineEdit(MyLineEdit* line, QString str) { QString txt = line->text(); line->setText(txt + str); }
I would like to give the
oneDigit
signal return value to theupdateLineEdit
function.@ekato993 Return values in signals do not make sense. If you want to pass something from signal to slot then simply use normal parameters:
signals: void oneDigit(QString);
Also what do you want to achieve with this line:
emit oneDigit() = keyName;
?!
You are assigning keyName to a temporary string which has absolutely no effect... -
MyLineEdit class
virtual void keyPressEvent(QKeyEvent* event) override { if(event->key() == Qt::Key_A || event->key() == Qt::Key_2) { QString keyName = QKeySequence(event->key()).toString(); emit oneDigit() = keyName; } else { event->ignore(); } }
signals: QString oneDigit();
MainWindow constructor connection
connect(line, &MyLineEdit::oneDigit, this, [=]{MainWindow::updateLineEdit(line,***)})
void MainWindow::updateLineEdit(MyLineEdit* line, QString str) { QString txt = line->text(); line->setText(txt + str); }
I would like to give the
oneDigit
signal return value to theupdateLineEdit
function.@ekato993 I was under the impression, that it was not possible period.
but I found this stack overflow thread:
https://stackoverflow.com/questions/5842124/can-qt-signals-return-a-valueSeems like you can get the return value of the slot to the emitter. With a view caveats. Like DirectConnections only and only the last slot connected.
But, when I understand your question correctly, thats not even your goal right?
-
MyLineEdit class
virtual void keyPressEvent(QKeyEvent* event) override { if(event->key() == Qt::Key_A || event->key() == Qt::Key_2) { QString keyName = QKeySequence(event->key()).toString(); emit oneDigit() = keyName; } else { event->ignore(); } }
signals: QString oneDigit();
MainWindow constructor connection
connect(line, &MyLineEdit::oneDigit, this, [=]{MainWindow::updateLineEdit(line,***)})
void MainWindow::updateLineEdit(MyLineEdit* line, QString str) { QString txt = line->text(); line->setText(txt + str); }
I would like to give the
oneDigit
signal return value to theupdateLineEdit
function.@ekato993 said in get the return value of the emitted signal:
signals:
QString oneDigit();Please take time to read documentation (cf. https://doc.qt.io/qt-5/signalsandslots.html).
You will find there:Signals are automatically generated by the moc and must not be implemented in the .cpp file. They can never have return types (i.e. use void).
So you want to do something which is not allowed.
Please tell us why you think you need to have a return value for a signal.
I think you have missunderstood how signals/slots mechanism works. -
You could do it like that
// In MyLineEdit signals: void oneDigit(QString str); // In MainWindow public slots: void updateLineEdit(QString str) { QString text = line->text(); line->setText(text + str); }
then connect it this way
// MainWindow constructor connect(line, &MyLineEdit::oneDigit, this, &MainWindow::updateLineEdit);
In keyPressEvent send it this way:
emit oneDigit(keyName);
That is look like what I want.
Missing function argument, so it should look liket this:
updateLineEdit(MyLineEdit *line, QString str)
But I am getting the following error,
C:\Qt\5.12.11\mingw73_64\include\QtCore\qglobal.h:121: error: static assertion failed: The slot requires more arguments than the signal provides.
-
That is look like what I want.
Missing function argument, so it should look liket this:
updateLineEdit(MyLineEdit *line, QString str)
But I am getting the following error,
C:\Qt\5.12.11\mingw73_64\include\QtCore\qglobal.h:121: error: static assertion failed: The slot requires more arguments than the signal provides.
@ekato993 said in get the return value of the emitted signal:
That is look like what I want.
Missing function argument, so it should look liket this: updateLineEdit(MyLineEdit *line, QString str)
But I am getting the following error,
C:\Qt\5.12.11\mingw73_64\include\QtCore\qglobal.h:121: error: static assertion failed: The slot requires more arguments than the signal provides.Why do you don't take time to read documentation to understand signals/slots mechanism?
- You can NOT connect a signal to a slot which have less arguments
- signal could have more arguments types must match with slots arguments types.
If you need to add extra parameters for a slot, you can use lambda for this (cf. https://doc.qt.io/qt-5/signalsandslots.html#advanced-signals-and-slots-usage).
But first you should know and explain what you want to do, so we could give you hints how to achieve it.
-
@ekato993 said in get the return value of the emitted signal:
That is look like what I want.
Missing function argument, so it should look liket this: updateLineEdit(MyLineEdit *line, QString str)
But I am getting the following error,
C:\Qt\5.12.11\mingw73_64\include\QtCore\qglobal.h:121: error: static assertion failed: The slot requires more arguments than the signal provides.Why do you don't take time to read documentation to understand signals/slots mechanism?
- You can NOT connect a signal to a slot which have less arguments
- signal could have more arguments types must match with slots arguments types.
If you need to add extra parameters for a slot, you can use lambda for this (cf. https://doc.qt.io/qt-5/signalsandslots.html#advanced-signals-and-slots-usage).
But first you should know and explain what you want to do, so we could give you hints how to achieve it.
I just want to get the pressed key as a string and add it to the lineedit. Very important is to allow specific key.
I know about lambdas but how to pass the string?
connect(line, &MyLineEdit::KeyOneDigit, this, [=]{MainWindow::updateLineEdit(line, ***);});
-
I just want to get the pressed key as a string and add it to the lineedit. Very important is to allow specific key.
I know about lambdas but how to pass the string?
connect(line, &MyLineEdit::KeyOneDigit, this, [=]{MainWindow::updateLineEdit(line, ***);});
@ekato993 said in get the return value of the emitted signal:
I know about lambdas but how to pass the string?
connect(line, &MyLineEdit::KeyOneDigit, this, [=](Qstring){MainWindow::updateLineEdit(line, ***);}); ... emit KeyOneDigit(key);
-
I just want to get the pressed key as a string and add it to the lineedit. Very important is to allow specific key.
I know about lambdas but how to pass the string?
connect(line, &MyLineEdit::KeyOneDigit, this, [=]{MainWindow::updateLineEdit(line, ***);});
@ekato993 said in get the return value of the emitted signal:
I just want to get the pressed key as a string and add it to the lineedit. Very important is to allow specific key.
alright,
make the following changes:
signals: void oneDigit(QString digit); // your signal now as an argument
emit oneDigit(keyName); // your emit the signal with the argument
connect(line, &MyLineEdit::oneDigit, [line](QString key)->void{line->setText(line->text() + key);});
No guarantee the lambda/connect is free of typos :D
-
I just want to get the pressed key as a string and add it to the lineedit. Very important is to allow specific key.
I know about lambdas but how to pass the string?
connect(line, &MyLineEdit::KeyOneDigit, this, [=]{MainWindow::updateLineEdit(line, ***);});
@ekato993 said in get the return value of the emitted signal:
I know about lambdas but how to pass the string?
Perhaps you are aware about lambdas but that is not the question here.
You have to learn signals/slots usage, because it is a very important concept in Qt world.
So if you want to build applications with Qt, take time to learn:- what
QObject
are => https://doc.qt.io/qt-5/object.html - what
QObject
parent <=> child relation chip => https://doc.qt.io/qt-5/objecttrees.html - how signals/slots works => https://doc.qt.io/qt-5/signalsandslots.html
Those are the minimum to be comfortable with Qt development.
This will save you many hours of frustration and headaches!It is up to you to learn, the documentation is freely available.
- what