The arguments for QTableView::setItemDelegateForColumn() are a column number and pointer to an instance of a QItemDelegate. The view later calls the functions in the delegate with the appropriate arguments at the appropriate time.
class DateDelegate: public QStyledItemDelegate {
Q_OBJECT
public:
DateDelegate
(QObject * parent
= 0): QStyledItemDelegate(parent)
{ }
// ^^^ I have given the delegate class a constructor to ensure object ownership is captured.
return value.toDate().toString("...");
}
return QStyledItemDelegate::displayText(value, locale);
}
};
...
DateDelegate *delegate = new DateDelegate(this);
ui->myTable->setItemDelegateForColumn(0, delegate);
class DateDelegate: public QStyledItemDelegate {
Q_OBJECT
public:
DateDelegate(QObject * parent = 0):
QStyledItemDelegate(parent)
{ }
// ^^^ I have given the delegate class a constructor to ensure object ownership is captured.
QString displayText(const QVariant &value, const QLocale &locale) const {
if (value.type() == QVariant::Date) {
return value.toDate().toString("...");
}
return QStyledItemDelegate::displayText(value, locale);
}
};
...
DateDelegate *delegate = new DateDelegate(this);
ui->myTable->setItemDelegateForColumn(0, delegate);
To copy to clipboard, switch view to plain text mode
Bookmarks