Search code examples
qtexecuteqstringqlabel

Want to execute a QString statement, which has the last part as integer variable


The statement is:

int nmbr;
QString strngs;

for( nmbr = 62; nmbr <65; nmbr++)
{
   strngs=(QString)"qDebug()<<(QString)ui->label_"+QString::number(nmbr)+"->text();";
}

Actually I want to access QLabels, a lot of them and extract their text for using at some other place.

Thanks!


Solution

  • Are you trying to get the text from a lot of QLabels of a current widget?

    Then you'd better do something like that:

    for( int nmbr = 62; nmbr <65; nmbr++)
    {
        QString labelname = QString("label_%1") .arg( nmbr );
        QLabel * label = findChild<QLabel*>( labelname );
    
        if ( label )
            qDebug() << label->text();
    }
    

    Is this what you're looking for?