PDA

View Full Version : Date disappears in tableview



rdjenner
18th July 2011, 22:56
I have a item delegate to format my date in tableview. Here is this code

class ticketDateFormatDelegate : public QStyledItemDelegate
{
public:
ticketDateFormatDelegate (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);
// return value.toDate().toString("MM/dd/yyyy");
}
private:
QString m_dateFormat;
};

Here is the code that uses this

ui->tableView->setItemDelegateForColumn(2,new ticketDateFormatDelegate("MM/dd/yy", this));

Problem is when I tab off of the date, or I add a new line and default the date, it disappears from the view. The date is there because when I save the line the record is updated with whatever date is entered, just disappears in data entry. Any help?

srazi
18th July 2011, 23:20
From Qt documentation (QVariant::toDate () const):

If the type() is String, an invalid date will be returned if the string cannot be parsed as a Qt::ISODate format date.
This means the 'value' must be in form Qt::ISODate!
Do you check that 'value.toDate().isNull()' returns FALSE?
For your remark: you should insert your code into code tag (button with sharp symbol)

rdjenner
18th July 2011, 23:29
I'm not sure I understand what you are saying. I default the value to a valid date. All I want to accomplish is that the date is formatted the way the customer wants to see it. Can you be a bit more specific.

srazi
18th July 2011, 23:55
The Qt::ISODate format is YYYY-MM-DD for dates.
If you default the value to a date like above then the code that you used:


return value.toDate().toString(m_dateFormat);

is correct, but if you default it in another form you should use a code like this one:


//myDateFormat is of type 'Qt::DateFormat' and is same as
//the date format that you use for filling date cells.
return QDate::fromString ( value.toString(), myDateFormat);