Question

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.

Was it helpful?

Solution

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.

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