Important: Please read the Qt Code of Conduct -

list of labels not returning values as expected

  • Hello!

    I'm having trouble extracting the data I'd like out of a list of QLabels.

    I defined my list in my header as such
    QList<QLabel*> myListOfLabels;and access it from two functions in my .cpp file.

    One file adds to the list and checks to see that it is indeed the text I wanted.

     qDebug() <<; //not necessarily at index 0, could be iterated over.

    and the other is supposed to print the text of the label. This should be the same as qDebug() <<; but it prints a null value. I can check in this function if there is anything in the list and it does returns the value I expect.

    How can I get the values to print to qDebug() in the second function?

  • Lifetime Qt Champion

    Your list is

    qDebug() <<;
    prints the address ?

    qDebug() <<>text();
    should print the text.

  • @mrjj
    Thank you for your response!

    So when I do qDebug() <<;, I get QObject(0x7fb3d55193c0), which is the address.
    I was expecting a QLabel returned but I got a QObject, which I understand since it is the top most parent of QLabel.
    So I tried qDebug() <<>text() and i get the null value.

    I tried qobject_cast<QLabel*>(>text(); and my app crashes.

  • Hi @mar0029
    This sample code may help you.
    Widget::Widget(QWidget *parent)
    : QWidget(parent)
    hlayout = new QHBoxLayout(this);
    for(int i=0;i<7;i++){
    QString str = QString::number(i);
    QLabel *lb = new QLabel(str);

    void Widget:: Print_labelName(){
    QLabel *lb_test;
    for(int i=0;i<myListofLabels.length();i++){
    lb_test =;
    qDebug()<<"label ["<<i<<"] ::"<<lb_test->text()<<endl;

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    Widget w;;

    return a.exec();


  • @yuvaram , thank you for the response!

    I tried something similar to your void Widget::Print_labelName() function but it still returns the null value.

    Question: I am using finding and adding the QLabel I want from a layout using the following:
    I wanted to make sure that the labels were of the correct type, even though I cast them as such, with qDebug() <<>metaObject()->className(); and it returns QLabel as expected.

    When the QDialog from which this function is called is closed, what happens to the list of labels where I added all of my labels? I declared the list in the header, so as to have access to it later, but I fear something else is happening when the QDialog goes away.

    Thank you for your time.

  • Lifetime Qt Champion

    @mar0029 said:


    If this find an object that is not a QLabel, it will insert NULL..
    and if u ever say NULL->text() u will crash.

  • @mrjj , thank you for your response!!

    That is good to know that if i call NULL->text() that my app will crash.

    By calling>metaObject()->className(); I know that there is an object there but when i call>text(); it returns a null value. Why wouldn't the value stay the same?

  • Lifetime Qt Champion

    Well its hard to say what is going on.
    They dont normally loose their text.

    If you do
    QLabel *;
    does sit change them?

    QList<QLabel*> myListOfLabels
    lives in dialog
    and you close/delete dialog. List is invalid.

  • @mrjj

    The QList was defined in the header as:

          QList<QLabel*> checkBoxLabels;

    When I do test->setText("hello"); , it does change the value for each value in the QList.

    I was reading about passing objects in and out of dialogs using signals but I'm not quite sure how that works.

  • Lifetime Qt Champion

    Hi if you can setText then list is ok ?
    So what part is not working?
    if myListOfLabels is a class member, then you can use from
    any function in that class.
    so in what function does it stop working?

    Very short with signals.
    you can define a signal

    void MyFatListReady( QStringList list);

    and you can then
    QStringList real_list;
    (build the list)
    emit MyFatListReady(real_list);

    Then from say mainwindow, you can connect this signal to a slot
    and in that slot use the list.

  • @mrjj
    Thank you for your response.
    What isn't working is this: the text that I set to the labels the first time I opened the dialog goes to NULL. Using myListOfLabels->count(); on the second time opening the dialog, provides the suspected number of items.

  • Lifetime Qt Champion

    Well, how do u set text?
    In dialog constructor?
    Why is second time different?
    When do u add to list?
    how do you close dialog ?

    U must do something I dont expect with the list. else it must just work.
    So please show most code else its impossible to guess.

  • @mrjj

    Ok. Here is some code. I tried to change as little as possible.

     void MainWindow::configFunction()
    configScreen = new QDialog;
    myHbox = new QHBoxLayout;
    tab0 = new QWidget;
    tab1 = new QWidget;
    tab2 = new QWidget;
    tab3 = new QWidget;
    sa0 = new QScrollArea;
    sa1 = new QScrollArea;
    sa2 = new QScrollArea;
    sa3 = new QScrollArea;
    vbox0 = new QVBoxLayout;
    vbox1 = new QVBoxLayout;
    vbox2 = new QVBoxLayout;
    vbox3 = new QVBoxLayout;
    configTabArea = new QTabWidget;
    configMainVbox = new QVBoxLayout;
     //Get locations of the files to pull variables from
    QFile file(setupLocation);;
    QTextStream in(&file);
    while(!in.atEnd()) //gets contents of file into queue
        inQueue.enqueue(in.readLine()); // this queue holds all of the file locations that i'll pull from later. 
    for(int i = 1; i < inQueue.size(); i++)//checks to see if there is anything after equals sign of file1_location =
        if( > 18) 
            qDebug() << "length is > 18";
            QChar mychar = Qt::Key_Slash;
            QString string =;
            int slashLocation, j;
            j = 0; slashLocation = 0;
            while((j = string.indexOf("/", j)) != -1)
                if( == mychar)
                    slashLocation = j;
            activeFile = (string.right(string.length() - slashLocation));
    //get file1 variables
    QFile file1(;
    QFile file2(;
    QFile file3(;
     //    QFile file4(; // place holder for future
    QQueue<QString> varNameQ;
    QQueue<QString> varQ;
    QQueue<QString> commentQ;
    QQueue<qint32> orderQ;
    QQueue<QString> boldString;
    qint8 zero = 0;   qint8 one = 1; qint8 three = 3; qint8 four = 4; //these will be the variables in the orderQ.
    //header #      //varName #    //varQ #       //comment #      //newLine #
    QString stringFromInput;     //this will hold an entire line from the "in" textStream.
    QString headerString;       // this is a special case in that there is only one of these.
    //QStringList myStringList;   // since Qt doesn't support the getline function that makes file parsing more intuitive in c++,
                                // it is necessary to break up the 'stringfromInput into chunks that make since
                    // UPDATE To myStringList:: I used QString::left(), mid(), right() to get substrings of the string. this
                    // let me keep the original. using these functions, logic, and math, i was able to simplify the process.
    QString varName; QString var; QString comment; /*qint32 newLine = 0;*/
    QString nullString = "";
    QTextStream in_0(&file1);  // this is similar to std::fstream, the ifstream portion. the ofstream portion comes in MainWindow::saveFunction
    QTextStream in_1(&file2);
    QTextStream in_2(&file3);
     //    QTextStream in_3(&file4); //place holder for future input stream for sacsap
     //START file1 STUFF
    if(file1.size() == 0)
        qDebug() << "The file is empty";
    else if(file1.size() != 0 &&
        qDebug() << "file1 Parse";
        bool firstCheck = true;
        while(!in_0.atEnd())   //while the file is available to be read AND it is not at the end yet
           //Add stuff to the queues that hold the variables, variable names, comments
        qDebug() << "size of checkedLabelstab0 is " << checkedLabelstab0.count();
     //checkedLabelstab0 should be EMPTY the first time that MainWindow::configFunction() is called
     //should only contain values after MainWindow::configApply is called. 
        for(int i = 0; i < varNameQ.size(); i++)
     //            qDebug() << "labeling " <<;
            QHBoxLayout *hboxtest = new QHBoxLayout;
            QCheckBox *checkBox = new QCheckBox;
            QLabel *label = new QLabel();
            qDebug() << "before isEmpty check";
                qDebug() << "before for loop";
                for(int i = 0; i < checkedLabelstab0.count(); i++)
                    qDebug() << "iteration " << i;
                    QLabel *testLabel = new QLabel();
                    testLabel =;
     //                    testLabel->setText("Label ["+QString::number(i)+"]");
                    qDebug() << "This is testLabel " << testLabel->text();
                    testLabel = NULL;
                    delete testLabel;
     //*************This is done 3 more times for each of the remaining files.
     vbox0->setSizeConstraint(QLayout::SetFixedSize); //important to keep scrolling active
    QPushButton *configApplyButton = new QPushButton("Apply");
    QPushButton *configCancelButton = new QPushButton("Cancel");
    configTabArea->addTab(sa0, tr("file1"));
    configTabArea->addTab(sa1, tr("file2"));
    configTabArea->addTab(sa2, tr("file3"));
    configTabArea->addTab(sa3, tr("file4"));
    connect(configApplyButton, SIGNAL(clicked(bool)), this, SLOT(configApply()));
    connect(configApplyButton, SIGNAL(clicked(bool)), configScreen, SLOT(accept()));
    connect(configCancelButton, SIGNAL(clicked(bool)), configScreen, SLOT(accept()));
    configScreen->setWindowTitle("Set your file's configurations");

    and this is the MainWindow::configApply(); slot that is called in the previous function

    void MainWindow::configApply()
    QList<QTabWidget*> AllTabWidgetList = configScreen->findChildren<QTabWidget*>();

    QScrollArea *sa_0 = new QScrollArea;
    QScrollArea *sa_1 = new QScrollArea;
    QScrollArea *sa_2 = new QScrollArea;
    QScrollArea *sa_3 = new QScrollArea;
    QWidget *file1Tab = new QWidget;
    QWidget * file2Tab = new QWidget;
    QWidget * file3Tab = new QWidget;
    QWidget * file4Tab = new QWidget;
    sa_0 = qobject_cast<QScrollArea*>(>widget(0));
    sa_1 = qobject_cast<QScrollArea*>(>widget(1));
    sa_2 = qobject_cast<QScrollArea*>(>widget(2));
    sa_3 = qobject_cast<QScrollArea*>(>widget(3));
    file1Tab = sa_0->widget();
    file2Tab = sa_1->widget();
    file3Tab = sa_2->widget();
    file4Tab = sa_3->widget();
    layoutsFromtab0 = sa_0->findChildren<QHBoxLayout*>();
    layoutsFromtab1 = sa_1->findChildren<QHBoxLayout*>();
    layoutsFromtab2 = sa_2->findChildren<QHBoxLayout*>();
    layoutsFromtab3 = sa_3->findChildren<QHBoxLayout*>();
    qDebug() <<"count of hboxlayouts in tab0 is " << layoutsFromtab0.count();
    qDebug() <<"count of hboxlayouts in tab1 is " << layoutsFromtab1.count();
    qDebug() <<"count of hboxlayouts in tab2 is " << layoutsFromtab2.count();
    qDebug() <<"count of hboxlayouts in tab3 is " << layoutsFromtab3.count();
        int size = layoutsFromtab0.size();
        checkedLabelstab0.clear(); // makes sure that checked labels are checked for checking each run through.
        for(int i = 0; i < size; i ++)
            for(int j = 0; j <>count(); ++j)
                    if(std::strncmp(>itemAt(j)->widget()->metaObject()->className(), "QCheckBox", 9) == 0)
                        QWidget *w = new QWidget();
                        w =>itemAt(j)->widget();
                    else if (std::strncmp(>itemAt(j)->widget()->metaObject()->className(), "QLabel", 6) == 0)

    and just for clarification's sake, here is a photo of the configScreen dialog that I'm dealing with.

  • Lifetime Qt Champion

    It does seem fine.
    im confused why you do
    if(std::strncmp(>itemAt(j)->widget()->metaObject()->className(), "QCheckBox", 9) == 0)
    and not
    tab0->findChildren<QCheckBox*>(); U can ask to find only direct children.

    But we need to have small talk about qobject_cast
    think of it as asking "is this Object of this type." ?
    If yes, it returns NOT NULL; else NULL;

    so when you do
    if(qobject_cast<QCheckBox*>(w)->isChecked()) {
    checkedLabelstab0.append(qobject_cast<QLabel*>(>itemAt(j + 1)->widget()));

    it really makes no sense. as if its Not a QCheckBox*, then you will
    get NULL and say NULL->isChecked()) and crash.

    To make sense it should be
    QCheckBox* cb=qobject_cast<QCheckBox*>(w);
    if( cb) { if (cb->isChecked()) ...

    is it in configApply, that list goes empty ?

  • @mrjj

    Thank you for telling me about qobject_cast. I thought I was casting some object to another type of object.

    and oh my goodness you've done it again...
    You've led me in the right direction and I can now see the text of the labels. instead of passing the entire list of labels around, i just created a QStringList and put the label->text() in there.


  • Lifetime Qt Champion


    • . I thought I was casting some object to another type of object.
      You are. That is correct. But if you cast something that not possible
      you get NULL.
      So using it directly is slightly risky :)
      In case it fails.

    Well if u only need the Text from the checked, then a QStringList seems fine :)

Log in to reply