How to Get QLabel Text in Qt

In Qt, to retrieve the text content of a label, you can use the text() function. Here is an example code:

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QLabel label("Hello, Qt!");
    
    // 读取label的文本内容
    QString labelText = label.text();
    qDebug() << "Label text: " << labelText;

    return app.exec();
}

In the code above, a QLabel object called label is first created, then the text() function is used to retrieve the text content of label and store it in a variable labelText. Finally, the qDebug() function is used to print out the text content of label.

bannerAds