QListWidget-添加项目以跳过一些项目?
我遇到一个奇怪的问题,Qlistwidget似乎在循环的其他所有迭代中都添加了项目。
要更详细地,我读了一个文件,该文件逐行解析了目录,这些项目的信息填充了我命名为test test
的类实例的变量循环中的向量。此初始循环完成后,另一个循环执行,该循环用项目命名的项目填充了QListWidget。
当我使用qdebug()
将测试实例添加到我的向量的结果时,输出如预期的:
Test1
Test2
Test3
Test4
Test5
Test6
...
testN
但是,当我添加循环以将项目附加到qlistwidget中时似乎正在跳过所有奇数迭代的迭代。这就是使它在Qlistwidget上的原因:
Test1
Test3
Test5
Test7
...
TestN
我添加了一个QDebug()以打印每次迭代以查看循环是否跳过,并且它是打印0、1、2、3、4,...,...,n 正如预期的那样,但是偶数的项目未添加到列表中。
我使用的代码在下面。我知道使用字符串列表从.ini文件中获取内容似乎很奇怪,但是我有这样做的理由。
在第二个循环中,我更改了代码,从将每个测试名称打印为“ Test1,test2,...”等。 ,并且使用数字而不是文本更容易分辨。
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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论