How to modify QDateTime format in QTableView of QFileSystemModel ?



  • QTableView of QFileSystemModel:
    How to change QDateTime format to YYYY/MM/dd HH:mm:ss.
    How to change QTableView header ?
    https://github.com/sonichy/HTYFileManager

    alt text



  • @sonichy said in How to modify QDateTime format in QTableView of QFileSystemModel ?:

    How to change QDateTime format to YYYY/MM/dd HH:mm:ss.

    Have you tried
    QDateTime::fromString
    QDateTime::toString

    as shown here ?

    Header can be changed through setHorizontalHeaderLabels .


  • Lifetime Qt Champion

    Hi,

    A QStyledItemDelegate comes to mind for that or a QIdentityProxyModel.



  • Hi,

    U can u yyyy/MM/dd HH:mm:ss
    year in small letters,
    toString() method is present for changing as desired.

    Thanks,



  • I'm with Samuel here. Subclass QStyledItemDelegate.

    class DateFormatDelegate : public QStyledItemDelegate{
    Q_OBJECT
    Q_PROPERTY(QString dateFormat READ dateFormat WRITE setDateFormat NOTIFY dateFormatChanged)
    Q_DISABLE_COPY(DateFormatDelegate)
    public:
    explicit DateFormatDelegate(QObject *parent = Q_NULLPTR)
    :QStyledItemDelegate(parent),m_dateFormat("yyyy-MM-dd"){}
    DateFormatDelegate(const QString dtFormat,QObject *parent = Q_NULLPTR)
    :QStyledItemDelegate(parent),m_dateFormat(dtFormat){}
    QString displayText(const QVariant &value, const QLocale &locale) const Q_DECL_OVERRIDE{
    switch(value.type()){
    case QMetaType::QDate:
    return locale.toString(value.toDate(),m_dateFormat);
    case QMetaType::QDateTime:
    return locale.toString(value.toDateTime(),m_dateFormat);
    case QMetaType::QTime:
    return locale.toString(value.toTime(),m_dateFormat);
    default:
    return QStyledItemDelegate::displayText(value,locale);
    }
    }
    const QString& dateFormat() const {return m_dateFormat;}
    void setDateFormat(const QString& dtFormat){
    if(m_dateFormat==dtFormat) return;
    m_dateFormat=dtFormat;
    dateFormatChanged(m_dateFormat);
    }
    Q_SIGNAL void dateFormatChanged(const QString& dtFormat);
    private:
    QString m_dateFormat;
    };

Log in to reply
 

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