gpt4 book ai didi

linux - QTableWidget 在删除多行时崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:38 26 4
gpt4 key购买 nike

我想从 QTableWidget 中删除多行(三列),这是我的代码:

void on_del_clicked()
{
QList<QTableWidgetItem *> items = ui->tableWidget->selectedItems();
for(int i = 0;i<items.length(); i=i+3)
{
int row = items[i]->row();
if(row>=0)
{
ui->tableWidget->removeRow(row);
ui->tableWidget->setCurrentIndex(ui->tableWidget->model()->index(row,0));
}
}
}

如果我使用 Ctrl 选择多行,它工作正常,但如果我通过拖动鼠标选择多个条目,它会崩溃。它仅在所选项目是 3 的倍数并且通过拖动鼠标选择行时崩溃。我使用 Qt 4.8.4,我的操作系统是 Ubuntu 12.10 64 位

最佳答案

血是对的:

Mayby it's because you choose multiple items in the same row and then when you delete row and try to access next element in that row.

所以改用这段代码:

QList<QTableWidgetItem*> selected_rows = ui->tableWidget->selectedItems();
while( !selected_rows.isEmpty() )
{
QTableWidgetItem *itm = selected_rows.at(0);
ui->tableWidget->removeRow(itm->row());
selected_rows = ui->tableWidget->selectedItems();
}

关于linux - QTableWidget 在删除多行时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15178730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com