QList<QTableView*> list=findChildren <QTableView*>();
#include <QStyledItemDelegate>class MyDelegate : public QStyledItemDelegate{ Q_OBJECTpublic: explicit MyDelegate(QObject *parent = 0); virtual QString displayText(const QVariant &value, const QLocale &locale) const;};
QString MyDelegate::displayText(const QVariant &value, const QLocale &locale) const{ switch (value.type()) { case QVariant::Date: return locale.toString(value.toDate(), "dd.MM.yyyy"); break; case QVariant::Time: return locale.toString(value.toTime(), "hh:mm"); break; case QVariant::DateTime: return locale.toString(value.toDateTime(), "dd.MM.yyyy hh:mm"); break; default: return QStyledItemDelegate::displayText(value, locale); break; }}
tableView->setItemDelegateForColumn(1, new MyDelegate(tableView));
ui->tableView->setItemDelegateForColumn(1, MyDelegate(ui->tableView));
ui->tableView->setItemDelegateForColumn(1, new MyDelegate(ui->tableView));
#ifndef MYDELEGATE_H#define MYDELEGATE_H#include <QStyledItemDelegate>#include <QObject>class MyDelegate : public QStyledItemDelegate{ Q_OBJECTpublic: explicit MyDelegate(QObject *parent = 0); virtual QString displayText(const QVariant &value, const QLocale &locale) const;};#endif // MYDELEGATE_H
#include <QDateTime>#include "mydelegate.h"QString MyDelegate::displayText(const QVariant &value, const QLocale &locale) const{ return locale.toString(value.toTime(), "hh:mm");}
#include <QtSql>#include <QMessageBox>#include "mainwindow.h"#include "ui_mainwindow.h"#include "mydelegate.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); md=new QSqlTableModel(this); md->setTable("test"); md->setEditStrategy(QSqlTableModel::OnManualSubmit); md->select(); md->setHeaderData(0,Qt::Horizontal, trUtf8("kod")); md->setHeaderData(1,Qt::Horizontal, trUtf8("name")); md->setHeaderData(2,Qt::Horizontal, trUtf8("time")); ui->tableView->setModel(md); ui->tableView->setItemDelegateForColumn(1, new MyDelegate(ui->tableView));}MainWindow::~MainWindow(){ delete ui;}