Results 1 to 7 of 7

Thread: How to format QDate in QTableWidgetWiev

  1. #1
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question How to format QDate in QTableWidgetWiev

    Hi;

    I have QTableWidgetView that displays data from database. I have enabled sorting the data by columns (clicking on the header row). I have solved the problem of integers and date data with QTableWidgetItem::setData(Qt::DisplayRole, int) and QTableWidgetItem::setData(Qt::DisplayRole, QDate). That works perfectly but date is displayed in format "d MMM yyyy" while I need it in "dd'.'MM'.'yyyy" or similar format. Is it possible and how?

    Regards; Luka
    Last edited by omci; 12th April 2012 at 17:33. Reason: reformatted to look better

  2. #2
    Join Date
    Apr 2012
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to format QDate in QTableWidgetWiev

    try to use toString("dd.MM.'yyyy") method from QDate that returns QString

  3. #3
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to format QDate in QTableWidgetWiev

    That sets data type for that coloumn as QString instead of QDate. I need to have data type of QDate to enable sorting by date. Example:
    1. 01.01.2012
    2. 03.05.2011
    3. 04.03.2012

    should be sorted as:
    1. 03.05.2011
    2. 01.01.2012
    3. 04.03.2012

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

    Default Re: How to format QDate in QTableWidgetWiev

    Use a custom item delegate applied to the date column of the view. Something like:
    Qt Code:
    1. class DateDelegate: public QStyledItemDelegate {
    2. Q_OBJECT
    3. public:
    4. QString displayText(const QVariant &value, const QLocale &locale) const {
    5. if (value.type() == QVariant::Date) {
    6. return value.toDate().toString("...");
    7. }
    8. return QStyledItemDelegate::displayText(value, locale);
    9. }
    10. };
    To copy to clipboard, switch view to plain text mode 
    and QTableView::setItemDelegateForColumn()

  5. #5
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to format QDate in QTableWidgetWiev

    Thank for the tip. However I can not make it compile with error: expected primary-expression before ‘.’ token:

    ui->myTable->setItemDelegateForColumn(0, DateDelegate.displayText(QVariant:ate, QLocale::Slovenian));

    What I did wrong?

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

    Default Re: How to format QDate in QTableWidgetWiev

    The arguments for QTableView::setItemDelegateForColumn() are a column number and pointer to an instance of a QItemDelegate. The view later calls the functions in the delegate with the appropriate arguments at the appropriate time.
    Qt Code:
    1. class DateDelegate: public QStyledItemDelegate {
    2. Q_OBJECT
    3. public:
    4. DateDelegate(QObject * parent = 0):
    5. QStyledItemDelegate(parent)
    6. { }
    7. // ^^^ I have given the delegate class a constructor to ensure object ownership is captured.
    8.  
    9. QString displayText(const QVariant &value, const QLocale &locale) const {
    10. if (value.type() == QVariant::Date) {
    11. return value.toDate().toString("...");
    12. }
    13. return QStyledItemDelegate::displayText(value, locale);
    14. }
    15. };
    16.  
    17. ...
    18.  
    19. DateDelegate *delegate = new DateDelegate(this);
    20. ui->myTable->setItemDelegateForColumn(0, delegate);
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to ChrisW67 for this useful post:

    omci (15th April 2012)

  8. #7
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to format QDate in QTableWidgetWiev

    Works like a charm! Thank you

Similar Threads

  1. Best way to represent struct using Qt containers
    By rajil.s in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2012, 18:30
  2. What do pointer values actually represent?
    By hackerNovitiate in forum General Programming
    Replies: 2
    Last Post: 26th February 2011, 21:57
  3. Represent a bit vector
    By danilodsp in forum Newbie
    Replies: 3
    Last Post: 20th October 2010, 16:10
  4. represent svg in tab view
    By Dilshad in forum Newbie
    Replies: 5
    Last Post: 27th July 2010, 09:47
  5. how to represent a graph?
    By harakiri in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2009, 14:37

Tags for this Thread

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.