Qt 当列表小部件项被单击两次时崩溃

Qt Crash when listWidget item is clicked twice

本文关键字:两次 崩溃 单击 列表 小部 Qt      更新时间:2023-10-16

在我的项目中,我有一个listWidget。当用户单击列表中的项目时,它将加载以下内容:

void BlockSelect::on_blockList_clicked(const QModelIndex &index)
{
    QString blockListName;
    QString temp_hex;
    QString temp_hex2;
    int temp_int;
    QListWidgetItem *newitem = ui->blockList->currentItem();
    blockListName = newitem->text();
    temp_hex = blockListName.mid(0, blockListName.indexOf(" "));
    if(temp_hex.indexOf(":") == -1)
    {
        temp_int = temp_hex.toInt();
        ui->blockIdIn->setValue(temp_int);
        ui->damageIdIn = 0;
    }
    else
    {
        temp_hex2 = temp_hex.mid(temp_hex.indexOf(":")+1, temp_hex.length()-(temp_hex.indexOf(":")+1));
        temp_hex = temp_hex.mid(0, temp_hex.indexOf(":"));
        temp_int = temp_hex.toInt();
        ui->blockIdIn->setValue(temp_int);
        temp_int = temp_hex2.toInt();
        ui->damageIdIn->setValue(temp_int);
    }
}

其中大部分只是字符串操作。(你不需要研究这个语法或任何东西)

我的问题是,当用户快速单击另一个列表项(在当前过程完成之前)时,程序崩溃。有没有办法允许快速点击(一次多个进程)或替代解决方案?

感谢您抽出宝贵时间:)

我希望

您在 GUI 线程中执行所有这些代码。如果是这样,那么就不会有问题 - 如果你的代码是正确的(它不是)。没有您在问题中提到的"过程"之类的东西。单击由槽处理,并从列表中的事件处理程序调用它们。这不应该崩溃,点击将以序列化的方式处理 - 一个接一个。

这是错误:为什么要将分配的 UI 指针元素的值重置为零?

ui->damageIdIn = 0;

这是无稽之谈。也许你的意思是ui->damageIdIn->setValue(0)ui->damageIdIn->hide().然后,您继续在

ui->damageIdIn->setValue(temp_int);

它崩溃了。

代码中的其他地方也可能有 bug。