QListWidgetItem指针导致程序崩溃
QListWidgetItem pointer causes program to crash
在我的程序中,我有一系列选项卡,并赢得了每个选项卡,有一个组合框和QListWidget
。我正在尝试通过QListWidgetItem
类型的指针读取QListWidget
上项目的状态。该程序在代码的这一点上崩溃。我确定该程序在这里崩溃,因为我用断点对其进行了仔细检查。
这是我的代码;
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
。我该如何解决这个问题?我编码错误吗?
,所以我修复了错误;我做错了的一部分是,我试图使用QComboBox::count()
函数返回的值访问QListWidget
上的项目。组合盒中的物品数量为8;但是,对于给定的QComboBox
选择,此QListWidget
上的数字项为3。我通过使用QListWidget::count()
限制循环计数来添加另一个循环以通过QListWidget
上的项目循环解决。
这是我的工作代码;
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;
}
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- 试图创建流或fopen时程序崩溃
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- DLL Made with CMake 使程序崩溃
- 程序崩溃使用boost::asio
- 调用 free() 有时会导致程序崩溃
- 邮件加密程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- For 循环在尝试读取数组 c++ 时程序崩溃
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- Windows桌面程序保存您的计算机会话 - 基于程序崩溃时的恢复会话
- 无法访问的代码如何导致我的程序崩溃?
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- QCompleter set模型使应用程序崩溃
- 使用唯一指针调用函数会使我的程序崩溃
- 删除SDL_PollEvent时程序崩溃