QListWidget - Add Items by For Loop Skipping Some Items?

113 Views Asked by At

I am experiencing an odd issue with a QListWidget seemingly adding items on every other iteration of a loop.

To be more detailed, I read a file in, which parses the contents line by line, and the information from those items populate variables for instances of a class I've named Test, which get appended to a vector in a loop. Once this initial loop completes, another loop executes which populates a QListWidget with items, which are named after the Test instances.

When I used qDebug() to output the results of adding Test instances to my vector, the output was as expected, for example:

Test1
Test2
Test3
Test4
Test5
Test6
...
testN

But when I added the loop to append items to the QListWidget, the for loop appears to be skipping every odd-numbered iteration. This is what makes it onto the QListWidget:

Test1
Test3
Test5
Test7
...
TestN

I added a qDebug() to print each iteration to see if the loop is skipping, and it's printing 0, 1, 2, 3, 4, ..., N as expected, yet the even-numbered items are not being added to the list.

The code I am using is below. I know it may seem odd to be using a string list to grab the contents from an .ini file, but I have reasons for doing so.

Here in the second loop I changed the code from printing each test name as "test1, test2,..." etc. in an attempt to troubleshoot, as I wanted to see just how many items were being skipped from being added to the list, and it was easier to tell by using digits rather than text.

for (int i = 0; i < iniContentsStrList.count(); i++)
{
    // If the test procedure hasn't been found yet...
    // And the current line contains the identifying 'test procedure' string...
    if (!testProcedureReached && (iniContentsStrList[i].contains(TEST_PROCEDURE_STR) || iniContentsStrList[i].contains("TEST_PROCEDURE")))
    {
        // Mark the test procedure as reached
        testProcedureReached = true;
        
        // Skip ahead to the next line/iteration so we start reading the actual tests
        i++;
    }
    
    // Only execute this section if the test procedure has been reached and we are in it
    if (testProcedureReached)
    {
        QStringList data = iniContentsStrList[i].split(" ");
        // Break out of the loop if any of the ini data that was read in is a different section
        if (iniContentsStrList[i].contains("]"))
        {
            break;
        }
        if (data[0].contains(";") || data[0].compare("\n") == 0)
        {
            continue;
        }
        bool testIsOn = true;
        // Since the lines that were read in will contain 'test_name', '=', and 'ON/OFF'...
        // ...skip check for the actual 'ON/OFF' text before determining whether the test will be on or off
        for (int j = 0; j < data.count(); j++)
        {
            if (data[j].compare("ON") != 0 && data[j].compare("OFF") != 0)
            {
                continue;
            }
            testIsOn = ((data[j].compare("ON") == 0) ? true : false);
        }
        // Add the new test to the vector, using the test name that was read for the test instance's name
        Test* test = new Test();
        test->on = testIsOn;
        test->testName = data[0].trimmed();
        testList.append(test);
    }
}

// Now add the Tests to the QListWidget
for (int j = 0; j < testList.count(); j++)
{
   qDebug() << j; // Outputs 0, 1, 2, 3, 4, ..., N
   ui->listWidget_testProcedure->addItem(QString::number(j)); // Outputs Test1, Test 3, Test 5, ... Test N into the list
}
0

There are 0 best solutions below