PDA

View Full Version : How to dispaly date in format dd/mm/yyyy using QSqlTableModel



bala
16th November 2007, 07:03
Hi,
How to dispaly date in format dd/mm/yyyy using QSqlTableModel
and through QtableView.
By default it displays date in yyyy-mm-dd format.

jpn
16th November 2007, 08:13
QWidget has a property called locale (http://doc.trolltech.com/4.3/qwidget.html#locale-prop) since Qt 4.3. This is used to localize dates and times in items views. In earlier versions, you can do it the way our wiki describes: Localizing dates and times in item views (http://wiki.qtcentre.org/index.php?title=Localizing_dates_and_times_in_item _views).

bala
16th November 2007, 10:26
Hi,
thanks for your help.
Please tell me how to use
"Qt::EditRole" with the model.
I have stored the date in mysql database.
And while displaying it in the view it
need to be in rhe format dd/mm/yyyy.
please help me.

jpn
16th November 2007, 23:44
Which version of Qt are you using?

bala
17th November 2007, 06:06
Hi,
I am using Qt 4.2.3.

jpn
17th November 2007, 07:25
I am using Qt 4.2.3.
Alright, so you might want to try the approach shown in our wiki. Start with subclassing QSqlTableModel and reimplementing data(). First, you'll get the actual value from base class. Then, you do some adjustments when appropriate, when certain conditions are met:


QVariant MySqlTableModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QModelIndex();

// get the actual value from base class
QVariant value = QSqlTableModel::data(index, role);

// do adjustments if necessary
if (role == Qt::DisplayRole && index.column() == theColumnWhichContainsDates)
{
QDate date = value.toDate();
value = date.toString("dd/MM/yyyy");
}

return value;
}

bala
17th November 2007, 12:27
Hi,
I have implemented the code.
It was compiling fine but while
running it shows segmentation fault.

jpn
17th November 2007, 12:33
Compile the app in debug mode ("qmake -config debug"), run it via debugger ("gdb ./app"), make it crash, and paste backtrace ("bt") here.