Results 1 to 18 of 18

Thread: QDateEdit and date returned

  1. #1
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QDateEdit and date returned

    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.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    try to set display format using setDisplayFormat.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    The method setDisplayFormat doesn't work, QDateEdit returns the same formatted value "dd-MM-yyyy"!!!

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    QDateEdit returns QDate, if you need to convert date to string then use QDate::toString with format==dd/MM/yyyy.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    I'm trying to set the format value returned because QDateEdit widget is mapped to a QSqlTableModel!!!

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    I don't understand what do you want.
    can you attach compilable example?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    The following code show you how I'm using the QDateEdit widget:

    Qt Code:
    1. frmEntUsc_AME::frmEntUsc_AME(QSqlRelationalTableModel *model,
    2. const int &isItNew, QWidget *parent) : QDialog(parent)
    3. {
    4. ui.setupUi(this);
    5. this->setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    6. this->setAttribute(Qt::WA_DeleteOnClose);
    7.  
    8. modelAME = new QSqlTableModel(this);
    9. modelAME = model;
    10.  
    11. modelAME->database().transaction();
    12.  
    13. if (isItNew < 0)
    14. {
    15. id = modelAME->rowCount();
    16. modelAME->insertRow(id);
    17. ui.dedData->setDate(QDate::currentDate());
    18. }
    19. else
    20. {
    21. id = isItNew;
    22. }
    23.  
    24. ui.dedData->setDisplayFormat("dd/MM/yyyy"); //QDateEdit widget
    25.  
    26. mapper = new QDataWidgetMapper(this);
    27. mapper->setModel(modelAME);
    28. mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    29. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    30. //...
    31. //...
    32. mapper->addMapping(ui.dedData, 9); // the QDateEdit widget is mapped to a date field
    33. mapper->setCurrentIndex(id);
    34.  
    35. connect(ui.btnAnnulla, SIGNAL(clicked()), this, SLOT(revert()));
    36. connect(ui.btnOk, SIGNAL(clicked()), this, SLOT(submit()));
    37.  
    38. qDebug() << "frmEntUsc_AME Loaded!";
    39. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    which date format do you use in a database?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    I'm using a SQLITE database, the field is declared as TEXT!

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    why? why is not date?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #11
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    As you know, SQLITE allow only few datatype :
    • NULL
    • INTEGER
    • REAL
    • TEXT
    • BLOB

  12. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    yes, forgot about that.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  13. #13
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    So, I'm right to claim that there isn't a way to set up the return value for a QDateEdit widget!

  14. #14
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateEdit and date returned

    I made a test and data in QDateEdit sets normal, but mask in sql-table changed to another.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  15. #15
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDateEdit and date returned

    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:
    Qt Code:
    1. QVariant MyTableModel::data ( const QModelIndex & item, int role ) const
    2. {
    3. // Default return value
    4. QVariant ret = QSqlTableModel::data(item, role);
    5.  
    6. // Convert date column into real date type
    7. if (role == Qt::EditRole || role == Qt::DisplayRole)
    8. if (item.column() == dateIssued)
    9. if (ret.canConvert(QVariant::Date))
    10. ret.convert(QVariant::Date);
    11.  
    12. return ret;
    13. }
    To copy to clipboard, switch view to plain text mode 
    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.

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDateEdit and date returned

    The proper way of handling such situations is to provide a custom delegate for the widget mapper with setModelData() and setEditorData() reimplemented.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #17
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Question Re: QDateEdit and date returned

    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?

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDateEdit and date returned

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to put empty value in QDateEdit?
    By sawerset in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2019, 21:06
  2. QDateTimeEdit (QTimeEdit, QDateEdit) invalid input
    By Kumosan in forum Qt Programming
    Replies: 10
    Last Post: 15th June 2012, 12:21
  3. QDateEdit and empty date
    By vieraci in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2007, 16:19
  4. Replies: 0
    Last Post: 2nd May 2007, 10:53
  5. Problem with receiving events from QDateEdit
    By gunhelstr in forum Qt Programming
    Replies: 4
    Last Post: 20th April 2006, 11:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.