How can someone reference a QObject using a QString? I have tried to accomplish this several different ways based off Qt documentation and similar answers on this site to no avail.
QString string = "mylabel_"
for(int i = 0; i < my.count(); i++)
{
QString thisString = string + QString::number(i);
QString labelText = ui->thisString->text(); //'ui' has no members named 'thisString'
functions::saveLine(&nameFile, labelText);
}
An objectName is just a QString, but using it this way does not work. I have also tried using findChildren, but I might not be understanding the documentation on this function completely.
QList<QWidget *> labels = dialog.findChildren<QWidget *>("mylabel_");
for(int i = 0; i < labels.count(); i++)
{
QString labelText = ui->labels[i]->text(); //'dialog' has no member named 'labels'
functions::saveLine(&nameFile, labelText);
}
The end result is, I have a list of QLabels in my dialog that I want to save the text for. I've used the saveLine function elsewhere successfully, but get the above errors trying to reference the object. Any recommendations would be very helpful.
UPDATE: I was able to fix it so that it does not return any errors, but it still does not return any results, so I guess the question is more targeted now at the findChildren method and how the QString translates into an objectName. Here is the updated code, but it still does not return any results.
QList<QLabel *> labels = this->findChildren<QLabel *>("mylabel_");
for(int i = 0; i < labels.count(); i++)
{
QString labelText = labels.at(i)->text();
functions::saveLine(&nameFile, labelText);
}
So if I was able to find all of the labels that start with objectName = "mylabel_", and put them into a QList, this would be able to pull the text from them and pass it to the saveLine function I've implemented. However, I don't know why it is not finding the labels based off the QString I put in as the argument for findChildren.