Objects and connect
-
In my code I try to receive the name of the control where the text input occour.
For this I use:
QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged, this, &MainWindow::onEnterCDTextText );and the function:
void MainWindow::onEnterCDTextText(QString strText) { qDebug() << sender()->objectName(); }But I always recive an empty qDebug string. My idea was to receive the name "cdTextArrangerEdit". Is there anything I missunderstood?
-
In my code I try to receive the name of the control where the text input occour.
For this I use:
QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged, this, &MainWindow::onEnterCDTextText );and the function:
void MainWindow::onEnterCDTextText(QString strText) { qDebug() << sender()->objectName(); }But I always recive an empty qDebug string. My idea was to receive the name "cdTextArrangerEdit". Is there anything I missunderstood?
-
So where do you set your object name to the one you expect?
-
Do you ever set the object's name? You won't get the name of an object that you used in the C++ source -- you only get a name explicitly set with setObjectName(). C++ doesn't allow that sort of introspection automatically.
-
In my code I try to receive the name of the control where the text input occour.
For this I use:
QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged, this, &MainWindow::onEnterCDTextText );and the function:
void MainWindow::onEnterCDTextText(QString strText) { qDebug() << sender()->objectName(); }But I always recive an empty qDebug string. My idea was to receive the name "cdTextArrangerEdit". Is there anything I missunderstood?
@pixbyte
By default there would be no object name set. To do it this way you would need to usesetObjectName()to have set the name previously.Consider instead of
sender()using a lambda connection, like:QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged, this, [cdTextArrangerEdit, this](const QString &strText) { onEnterCDTextText(cdTextArrangerEdit->objectName(), strText); } ); // or, you might want to pass the literal name here: QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged, this, [this](const QString &strText) { onEnterCDTextText("cdTextArrangerEdit", strText); } ); void MainWindow::onEnterCDTextText(const QString &objectName, const QString &strText) { qDebug() << objectName; }You can do more things with lambdas than via
sender(), which also has issues. -
In my code I try to receive the name of the control where the text input occour.
For this I use:
QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged, this, &MainWindow::onEnterCDTextText );and the function:
void MainWindow::onEnterCDTextText(QString strText) { qDebug() << sender()->objectName(); }But I always recive an empty qDebug string. My idea was to receive the name "cdTextArrangerEdit". Is there anything I missunderstood?
-
In my code I try to receive the name of the control where the text input occour.
For this I use:
QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged, this, &MainWindow::onEnterCDTextText );and the function:
void MainWindow::onEnterCDTextText(QString strText) { qDebug() << sender()->objectName(); }But I always recive an empty qDebug string. My idea was to receive the name "cdTextArrangerEdit". Is there anything I missunderstood?
@pixbyte said in Objects and connect:
My idea was to receive the name "cdTextArrangerEdit". Is there anything I missunderstood
Yes!
objectName != name of your variableThe objectName is a (not only) Qt specific thing. You can give every
QObjectits own unique name.
QObjects you create in QtDesigner have a default name, others don't. So you have to set one.
Having a
QLineEditcreated in Designer before,this
QLineEdit * lineEd = new QLineEdit(this); qDebug() << ui->lineEdit->objectName(); qDebug() << lineEd->objectName();prints:
"lineEdit" ""