Results 1 to 6 of 6

Thread: How to represent QDateTime data in QTableView in needed format?

  1. #1
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How to represent QDateTime data in QTableView in needed format?

    I have a source QStandardItemModel in which I have stored data for an object. Its first column has QDateTime values (modelTable->setData(modelTable->index(i, 2, QModelIndex()), timedate).
    I want to create a new model for a QTableView by copying modelTable data into modelChart model, but I also want to have the QDateTime variable displayed in different format, e.g. ISO or localized.

    How can I do this? I haven't found the method for this.
    I played with DisplayRole, but didn't help.

    Qt Code:
    1. for (int i=0; i<modelTable->rowCount(); i++)
    2. {
    3. modelChart->setItem(i, 0, new QStandardItem());
    4. modelChart->setData(modelChart->index(i, 0, QModelIndex()), modelTable->item(i, 2)->data(Qt::EditRole));
    5. modelChart->setData(modelChart->index(i, 0, QModelIndex()), modelTable->item(i, 2)->data(Qt::EditRole), Qt::DisplayRole);
    6. modelChart->setItem(i, 1, new QStandardItem(modelTable->item(i, 3)->text()));
    7. modelChart->setData(modelChart->index(i, 1, QModelIndex()),QColor(r, g, b),Qt::DecorationRole);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance!

  2. #2
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to represent QDateTime data in QTableView in needed format?

    This may, or may not, be the answer you are looking for, but you can set a QDate/Time to a string: http://doc.qt.nokia.com/4.7/qdatetime.html#toString

    If you need to manipulate it, you can set it back to a QDate: http://doc.qt.nokia.com/4.7/qdate.html#fromString

  3. #3
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How to represent QDateTime data in QTableView in needed format?

    ~ We are nothing in this universe ~

  4. #4
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to represent QDateTime data in QTableView in needed format?

    Thank you, but it looks like these doesn't keep QDateTime type, but changes to QString. In this case, user cannot edit the cells as QDateTime (arrows to inc/dec values) and the Chart module (ChartXY) I'm using won't accept other format than QDateTime.
    Any idea how to keep QDateTime with proper representation?

  5. #5
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How to represent QDateTime data in QTableView in needed format?

    Try to keep QDateTime data type while setting the data for editing purpose (Qt::EditRole), and set the data for displaying (Qt::DisplayRole) with converted type (QString).

    Edit: disable smilies
    ~ We are nothing in this universe ~

  6. The following user says thank you to viulskiez for this useful post:

    falconium (15th April 2011)

  7. #6
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to represent QDateTime data in QTableView in needed format?

    QVariant QStandardItem::data ( int role = Qt::UserRole + 1 ) const [virtual]
    Returns the item's data for the given role, or an invalid QVariant if there is no data for the role.
    Note: The default implementation treats Qt::EditRole and Qt::DisplayRole as referring to the same data.

    Does it mean QStandardItem must be inherited to a new class that separates edit and display roles?
    How can I do that if it is the case?

    Thank you!

Similar Threads

  1. Qt to Matlab (mat format) export data to Matlab .mat format -v4
    By windsword in forum Qt-based Software
    Replies: 4
    Last Post: 26th February 2013, 21:01
  2. QList custom data format
    By sureshkvl in forum Qt Programming
    Replies: 1
    Last Post: 13th October 2010, 14:26
  3. How can you display binary data in hex format?
    By Cruz in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2009, 15:40
  4. Error format a cell in QTableView
    By estanisgeyer in forum Qt Programming
    Replies: 3
    Last Post: 4th September 2008, 15:22
  5. Defining data format in c++
    By locus in forum General Programming
    Replies: 3
    Last Post: 13th April 2007, 20:40

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.