PDA

View Full Version : QDateEdit and date returned



cydside
15th April 2009, 10:10
Hi to All,
I'm using a QDateEdit widget to edit date values from a database, so I display date values as "dd/MM/yyyy" (settting displayFormat). When I finish to update the record the QDateEdit widget returns "yyyy-MM-dd" formtat. Is there a way to change the return value?
Thanks.

spirit
15th April 2009, 10:17
try to set display format using setDisplayFormat.

cydside
15th April 2009, 10:31
The method setDisplayFormat doesn't work, QDateEdit returns the same formatted value "dd-MM-yyyy"!!!

spirit
15th April 2009, 10:33
QDateEdit returns QDate, if you need to convert date to string then use QDate::toString with format==dd/MM/yyyy.

cydside
15th April 2009, 10:39
I'm trying to set the format value returned because QDateEdit widget is mapped to a QSqlTableModel!!!

spirit
15th April 2009, 10:42
I don't understand what do you want.
can you attach compilable example?

cydside
15th April 2009, 10:50
The following code show you how I'm using the QDateEdit widget:



frmEntUsc_AME::frmEntUsc_AME(QSqlRelationalTableMo del *model,
const int &isItNew, QWidget *parent) : QDialog(parent)
{
ui.setupUi(this);
this->setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
this->setAttribute(Qt::WA_DeleteOnClose);

modelAME = new QSqlTableModel(this);
modelAME = model;

modelAME->database().transaction();

if (isItNew < 0)
{
id = modelAME->rowCount();
modelAME->insertRow(id);
ui.dedData->setDate(QDate::currentDate());
}
else
{
id = isItNew;
}

ui.dedData->setDisplayFormat("dd/MM/yyyy"); //QDateEdit widget

mapper = new QDataWidgetMapper(this);
mapper->setModel(modelAME);
mapper->setItemDelegate(new QSqlRelationalDelegate(this));
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
//...
//...
mapper->addMapping(ui.dedData, 9); // the QDateEdit widget is mapped to a date field
mapper->setCurrentIndex(id);

connect(ui.btnAnnulla, SIGNAL(clicked()), this, SLOT(revert()));
connect(ui.btnOk, SIGNAL(clicked()), this, SLOT(submit()));

qDebug() << "frmEntUsc_AME Loaded!";
}

spirit
15th April 2009, 10:55
which date format do you use in a database?

cydside
15th April 2009, 10:58
I'm using a SQLITE database, the field is declared as TEXT!

spirit
15th April 2009, 11:02
why? why is not date? :confused:

cydside
15th April 2009, 11:06
As you know, SQLITE allow only few datatype (http://www.sqlite.org/datatype3.html) :

NULL
INTEGER
REAL
TEXT
BLOB

spirit
15th April 2009, 11:13
yes, forgot about that.

cydside
15th April 2009, 11:40
So, I'm right to claim that there isn't a way to set up the return value for a QDateEdit widget!

spirit
15th April 2009, 11:48
I made a test and data in QDateEdit sets normal, but mask in sql-table changed to another.

ChrisW67
24th May 2009, 06:03
I realise I'm a bit late into this thread but I found it looking for solutions to similar issues with Sqlite "dates". Maybe this post will be useful to others.

Sqlite will treat text in an ISO date format as a date for the purposes of its date functions. However, when it reports the data in the "date" column to Qt you end up with a QVariant::String. Some Qt components recognise the ISO date string and treat it as a date, some don't. I'd like everything to have the best chance of just doing the Right Thing. How I am tackling this is to create a model sub-class that overrides data() to convert the QVariant::String to QVariant::Date on relevant columns:

QVariant MyTableModel::data ( const QModelIndex & item, int role ) const
{
// Default return value
QVariant ret = QSqlTableModel::data(item, role);

// Convert date column into real date type
if (role == Qt::EditRole || role == Qt::DisplayRole)
if (item.column() == dateIssued)
if (ret.canConvert(QVariant::Date))
ret.convert(QVariant::Date);

return ret;
}
I'm still pondering whether I need to do something with setData() for these columns, but at the moment it doesn't seem to be needed. I end up with an ISO date in the column if I pass setData() a QVariant::Date.

wysota
24th May 2009, 10:14
The proper way of handling such situations is to provide a custom delegate for the widget mapper with setModelData() and setEditorData() reimplemented.

ChrisW67
26th May 2009, 01:16
I'm not sure why you would opt to change every view on the data in the model in order to apply a delegate rather than change the model in one place and allow Qt's default date handling to kick in. Is this just a preference thing, or is there some deep-seated principle that I am missing?

wysota
26th May 2009, 22:08
The functionality of the model is correct. We have a requirement on the display of the data thus the straight way to fulfilling it is to correct the view related to it. Of course you can also do it by subclassing the model or by applying a model proxy, it's all up to you and depends on the effect you want to obtain.