Question

In my project, I have a listWidget. When the user clicks an item in the list, it loads this:

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);
    }
}

Most of this is just string manipulation. (You don't have need to study this syntax or anything)

My problem is, when the user clicks on another list item quickly (Before this current process is finished) the program crashes. Is there any way to allow fast clicks (multiple processes at once) or maybe an alternative solution?

Thanks for your time :)

Was it helpful?

Solution

I hope that you execute all this code in the GUI thread. If so, then there will be no problem - if your code were correct (it isn't). There is no such thing as the "process" that you mention in your question. The clicks are handled by a slot, and they are invoked from an event handler within the list. This is not supposed to crash, and the clicks will be handled in a serialized fashion - one after another.

Here's the bug: Why do you reset the value of an allocated UI pointer element to zero?

ui->damageIdIn = 0;

This is nonsense. Maybe you mean to ui->damageIdIn->setValue(0) or ui->damageIdIn->hide(). You then proceed to use this zero value in

ui->damageIdIn->setValue(temp_int);

and it crashes.

You may also have bugs in other places in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top