-1

在我的程序中,我有一系列选项卡,每个选项卡都有一个组合框和QListWidget. 我正在尝试QListWidget通过 type 的指针读取项目的状态QListWidgetItem。程序在代码的这一点崩溃。我确定程序在这里崩溃,因为我用断点仔细检查了它。

这是我的代码;

void MainWindow::on_applyButton_clicked()
{
//Reset list
MainWindow::revenueList.clear();
QStringList itemList;
itemList <<"Revenue growth" << "Cost of revenue growth" << "Operating income growth"
        << "Net income growth" << "Total operating expense growth" << "Gross profit"
        << "Operating profit" << "Net profit";

//Processing income statement
//Loop through all itemsin ComboBox
int items = ui->inc_st_comb->count();

for(int currentItem = 0; currentItem < items; currentItem++)
{
    //Set to current index
    ui->inc_st_comb->setCurrentText(itemList.at(currentItem));

    //Point to QListWidget Item and read checkbox
    QListWidgetItem *listItem = ui->inc_st_list->item(currentItem);

    if(listItem->checkState() == Qt::Checked)
    {
        MainWindow::revenueList.append(true);
    }
    else if (listItem->checkState() == Qt::Unchecked)
    {
        MainWindow::revenueList.append(false);
    }
}

    qDebug() << "U: " << MainWindow::revenueList;
}

程序在此块崩溃;

if(listItem->checkState() == Qt::Checked)
 {
      MainWindow::revenueList.append(true);
 }
 else if (listItem->checkState() == Qt::Unchecked)
 {
      MainWindow::revenueList.append(false);
 }

这可能是因为指针listItem指向无效位置或NULL. 我该如何解决这个问题?我编码错了吗?

4

1 回答 1

0

所以我修正了我的错误;我做错的部分是我试图QListWidget使用函数返回的值访问项目QComboBox::count()。组合框内的项目数为 8;但是QListWidget对于给定的QComboBox选择,这个上的项目数是 3。我通过添加另一个 for 循环来QListWidget通过限制循环计数来循环遍历 上的项目QListWidget::count()

这是我的工作代码;

void MainWindow::on_applyButton_clicked()
{
//Reset list
MainWindow::revenueList.clear();
QStringList itemList;
itemList <<"Revenue growth" << "Cost of revenue growth" << "Operating income growth"
        << "Net income growth" << "Total operating expense growth" << "Gross profit"
        << "Operating profit" << "Net profit";

//Processing income statement
//Loop through all itemsin ComboBox
int items = ui->inc_st_comb->count();

for(int currentItem = 0; currentItem < items; currentItem++)
{
    //Set to current index
    ui->inc_st_comb->setCurrentText(itemList.at(currentItem));

    for(int index = 0; index < ui->inc_st_list->count(); index++)
    {
         //Point to QListWidget Item and read checkbox
         QListWidgetItem *listItem = ui->inc_st_list->item(index);


         if(listItem->checkState() == Qt::Checked)
         {
             MainWindow::revenueList.append(true);
         }
         else if (listItem->checkState() == Qt::Unchecked)
         {
             MainWindow::revenueList.append(false);
         }
    }
}

   qDebug() << "U: " << MainWindow::revenueList;
} 
于 2017-02-23T17:59:40.687 回答