1

I'm trying to use QSqlTableModel with a QTableView, but i want to hide the 2 columns updated_date and created_date, its easy, but i want that the user can edit a data in the TableView, and when finish (or when submit that data) automatically the updated_date updates to the current date time. I need the program does it, not from the database engine. What steps should i follow to do this? I've thinking in implement the setData method, but i don't know how. Thank you very much for your time and Help.

1 Answers1

1

You can reimplement setData() function of your QSqlTableModel subclass and make like this:

virtual bool YourModel::setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)
{
    if (index.column() != updated_date) {
        QSqlTableModel::setData(index(index.row(), updated_date), QDate::currentDate(), role);      
    }

    return QSqlTableModel::setData(index, value, role);
}

But I'd rather suggest you to use the database triggers for this task.

VVV
  • 499
  • 4
  • 12
  • Thanks for your answer, but i'm trying it, and i don't know what's the best way, How do i know, in the submit method, which row i should modified? the user modify a cell, how do i know the index or row of that cell to update the date column? – karenrojaz27 Oct 27 '13 at 17:43
  • My bad, I've read in your question that you want to update that columns on submit and didn't think that you need to update a particular row, not the whole table column. Updated the answer. – VVV Oct 27 '13 at 18:39