unable to update QTableView cell text



  • I am trying to edit text in QTableView cell (update text base on another table) but I am unable to do any changes. Below is my code but it does not work.

    QTime time = ui->tv_timeSlot->model()->data(ui->tv_timeSlot->model()->index(index.row(),index.column())).toTime();
    QString timeStr = time.toString("hh:mm:ss");
    ui->tv_timeCard->model()->setData(ui->tv_timeCard->model()->index(1,1),timeStr,Qt::EditRole);
    

    I did find out that there are some people override the flags function with

    Qt::ItemFlags MyTableModel::flags (const QModelIndex &index) const
    {
        return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    }
    

    I would prefer not to override the function because I had no basic on this. But if that is the only way, please guide me. Thanks in advance



    • ui->tv_timeCard->model()->index(1,1) does your model have at least 2 rows and 2 columns?
    • ui->tv_timeCard->model()->setData returns a boolean, you should check what value it is
    • Can you try using QStandardItemModel instead of MyTableModel?


  • @VRonin Thanks for the suggestion.

    • I have 37 columns and 20+ rows
    • It return false. I am wondering why it unable to update and return false.
    • How can i know if i am using QStandardItemModel or MyTableModel? Or is there any way to set it?


  • @iradarann said in unable to update QTableView cell text:

    It return false. I am wondering why it unable to update and return false.

    You can step into it with the debugger to find out

    How can i know if i am using QStandardItemModel or MyTableModel? Or is there any way to set it?

    This is set when you call QTableView::setModel


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.