Show other data in QTableView with QItemDelegate
Asked Answered
V

2

6

I have a QTableView connected with an QSqlTableModel.
In the first column, there are only dates at this format: 2010-01-02
I want this column to show the date at this format (but without changing the real data): 02.01.2010
I know that I have to create an QItemDelegate for this column, but I don't know how I can read the existing data and overwrite it with something different. You have any idea how to manage that?

Vasya answered 6/1, 2010 at 13:6 Comment(0)
O
4

An item delegate doesn't necessarily change the data, it just renders the data. Also, if you're using Qt 4.4 or newer, look at QStyledItemDelegate instead--it's theme-aware and will look nicer.

There's an example of item delegates in this article (which seems to be a mirror of official documentation that is now down or gone).

Since all you really want to do is customize the text, have you considered using a proxy model instead and just returning your custom QString for the date column's DisplayRole?

Obey answered 6/1, 2010 at 13:30 Comment(5)
Either the delegate or the proxy model would work pretty well in this situation. The delegate is probably closer to what what intended, however.Pulverable
my problem is, that i don't get how to make it with an QItemDelegate. the paint()-method will not work for meVasya
What, specifically, is the problem with paint()?Obey
i know how to get the data ( qVariantValue<QString>(index.data()) ), but i don't know how to overwrite it with "foo" for exampleVasya
This is where using a proxy model will come in handy, as it's easier to support row/column/type-specific hacks there, but since you apparently really want a delegate... Create a QStyledItemDelegate subclass which reimplements paint() and sizeHint(). These implementations should actually check whether index.data() returns a QDate in the variant; if not, they should just call the base class's implementation. If so, do your own string conversion and either measure it (for sizeHint()) or draw it (for paint()).Obey
B
15

The simplest solution is to create a QStyledItemDelegate subclass and reimplement displayText(...) ie

class DateFormatDelegate : public QStyledItemDelegate
{
public:
 DateFormatDelegate (QString dateFormat, QObject *parent = 0) : 
  QStyledItemDelegate(parent),
  m_dateFormat(dateFormat)
 {
 }

 virtual QString displayText(const QVariant & value, const QLocale & locale ) const
 {
  Q_UNUSED(locale);
  return value.toDate().toString(m_dateFormat);
 }

private:
 QString m_dateFormat;
};

Then in your view -

setItemDelegateForColumn(/*date column*/, new DateFormatDelegate("MM.dd.yyyy", this));
Biolysis answered 16/2, 2010 at 22:26 Comment(0)
O
4

An item delegate doesn't necessarily change the data, it just renders the data. Also, if you're using Qt 4.4 or newer, look at QStyledItemDelegate instead--it's theme-aware and will look nicer.

There's an example of item delegates in this article (which seems to be a mirror of official documentation that is now down or gone).

Since all you really want to do is customize the text, have you considered using a proxy model instead and just returning your custom QString for the date column's DisplayRole?

Obey answered 6/1, 2010 at 13:30 Comment(5)
Either the delegate or the proxy model would work pretty well in this situation. The delegate is probably closer to what what intended, however.Pulverable
my problem is, that i don't get how to make it with an QItemDelegate. the paint()-method will not work for meVasya
What, specifically, is the problem with paint()?Obey
i know how to get the data ( qVariantValue<QString>(index.data()) ), but i don't know how to overwrite it with "foo" for exampleVasya
This is where using a proxy model will come in handy, as it's easier to support row/column/type-specific hacks there, but since you apparently really want a delegate... Create a QStyledItemDelegate subclass which reimplements paint() and sizeHint(). These implementations should actually check whether index.data() returns a QDate in the variant; if not, they should just call the base class's implementation. If so, do your own string conversion and either measure it (for sizeHint()) or draw it (for paint()).Obey

© 2022 - 2024 — McMap. All rights reserved.