format date/time value shown by a QTableView

人走茶凉 提交于 2021-02-08 11:13:02

问题


I´m using a QTableView to show a database table via model. One of the table columns have a timestamp, acctually a QDateTime was stored there before.

Is there some way to format the timestamp value at presentation time? I was thinking in something like the .toString of a QDateTime("yyyy-MM-dd hh:mm:ss.zzz").


回答1:


It is possible to return date formatted as you wish in this virtual method of QAbstractItemModel:

QVariant QAbstractItemModel::data(const QModelIndex &item, int role = Qt::DisplayRole) const;

You'll have to subclass your own model from QSqlTableModel and override this method. Code shold be something as follows:

QVariant MySubclassedMode::data(const QModelIndex& item, int role = Qt::DisplayRole) const{

    if(role == Qt::DisplayRole && itemBelongsTodateTimeColumn(item)){

        QDateTime* dateTime = retrieveDateTimeObjectForModelIndex(item);
        return QVariant(dateTime.toString("d MMM YYYY, h:mm"));
    }

    return QSqlTableModel::data(item, role)
}

This approach will allow you to easily change how the object will be displayed in table view.

QDateTime formatting details are here




回答2:


There is function, which returns you the date format you want.

You can use "Qt::DateFormat" different flags from below link as input to "tostring", based on your required format.

http://doc.qt.io/qt-5/qt.html#DateFormat-enum

The function is :

QString QDateTime::toString(Qt::DateFormat format = Qt::TextDate)

http://doc.qt.io/qt-5/qdatetime.html#toString-1

Append the the QString to table cell.

If you want only time:

You have "time()" function available in "QDateTime"

QTime time() const"

There is "tostring" for "QTime" too and you can customize the time you want

QString QTime::toString(const QString &format) const

http://doc.qt.io/qt-5/qtime.html#toString

Updating Datetime in model before adding to tableview: This is my wild guess (code is pseudo. And not tested and not working)

You can handle this situation with signal "datachanged"

http://doc.qt.io/qt-5/qabstractitemmodel.html#dataChanged

First connect the signal and define a slot to handle it in your class.

To connect the datachanged signal below is prototype:

connect(ui->tableView->yourMODEL(),SIGNAL(dataChanged(QModelIndex,QModelIndex)),SLOT(UpdateData(QModelIndex,QModelIndex)));

And define "UpdateData()" in your window.

void YourWIndow::UpdateData(const QModelIndex & indexA, const QModelIndex & indexB)
{
    int columnValue = indexA.column();
    int rowValue = indexA.row();

    if("your Column value is QDATETIME column")
    {
        QSqlRecord record = ui->tableView->model()->record(rowValue);
        QVariant var = record (datefieldcolumn);
        QDateTime dTime = var.toDateTime();


        //Format it in your way.

        //Update your QSqlRecord.
        record.setValue(datefieldcolumn,QVariant(formatedDateTime));

        ui->tableView->model()->setRecord(rowValue,record);
    }
}


来源:https://stackoverflow.com/questions/40494687/format-date-time-value-shown-by-a-qtableview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!