Search code examples
c++qtqtablewidgetqstringqtablewidgetitem

cannot update string in QTableWidget


I want to change for example 2.16 into 2,16 in a table. This is my code

{
    .......
    connect(ui.tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(change_string(QTableWidgetItem*)));
}

void MyClass::change_string(QTableWidgetItem* input_item)
{
    if (input_item->text() != "") {
        if (input_item->text().contains(".", Qt::CaseSensitive)) {
            input_item->text().replace(".", ",", Qt::CaseSensitive);
        }
    }
}

My code run normally, I have debugged, the line input_item->text().replace(".", ",", Qt::CaseSensitive); is implemented. But after that, the table still shows 2.16, not 2,16. I don't know why? Do I need to refresh table or something like that after replacing the string?


Solution

  • You don't setText in your code. Try this:

    QString text = input_item->text().replace(".", ",", Qt::CaseSensitive);
    input_item->setText(text);