Results 1 to 4 of 4

Thread: Display data from database after preprocessing

  1. #1
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Display data from database after preprocessing

    Hi,

    I would like to display a data from a database after some preprocessing.

    I am storing some QDateTime values in the form of qint64 variables(by using toMSecsSinceEpoch() ) in the pgsql database. But when displaying the data from database , I would like to display a beautiful date-time string( by using fromMSecsSinceEpoch() )

    I am simply setting the sql query model to my tableview. I couldnt figure out how and where should i convert the qint64 value to a date string to be displayed in my table view ?

    Thanks in advance...

  2. #2
    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: Display data from database after preprocessing

    You can do that in the delegate or in the query itself (by asking the database to do it).
    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.


  3. The following user says thank you to wysota for this useful post:

    yagabey (14th November 2014)

  4. #3
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Display data from database after preprocessing

    You can do this in many places and it depends on your preferences and how you have designed your program. Consider the following options:

    1. As already mentioned, you can choose to format your data when you extract it, but, then it is always extracted as a string. I prefer to pull in the data into the correct expected data type, so, for me, that would mean a QDateTime object when I read it.
    2. I have a model class that feeds data from the data( const QModelIndex &index, int role ) const method. If the role is Qt:isplayRole, you can choose to format the date and return a string for display and then return a QDateTime in the variant for the edit role.
    3. I use my own delegate class. This is very easy to write your own based on what you want to accomplish.


    Considering the final option, I have something like this in my delegate class. Consider this snippet!

    Qt Code:
    1. QString LinkBackFilterDelegate::displayText(const QVariant & value, const QLocale & locale ) const
    2. {
    3. if (value.type() == QVariant::Date && !getDateFormatString().isEmpty())
    4. {
    5. return value.toDate().toString(getDateFormatString());
    6. }
    7. else if (value.type() == QVariant::DateTime && !getDateTimeFormatString().isEmpty())
    8. {
    9. return value.toDateTime().toString(getDateTimeFormatString());
    10. }
    11. else if (value.type() == QVariant::Time && !getTimeFormatString().isEmpty())
    12. {
    13. return value.toTime().toString(getTimeFormatString());
    14. }
    15. else if (value.type() == QVariant::Bool)
    16. {
    17. if (value.toBool())
    18. {
    19. if (!getBoolTrue().isEmpty()) {
    20. return getBoolTrue(); // "True"
    21. }
    22. }
    23. else if (!getBoolFalse().isEmpty()) {
    24. return getBoolFalse(); // "False"
    25. }
    26. }
    27.  
    28. return QStyledItemDelegate::displayText(value, locale);
    29. }
    To copy to clipboard, switch view to plain text mode 

    I also want the formatting when I edit

    Qt Code:
    1. QWidget *LinkBackFilterDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & option,
    2. const QModelIndex &index) const
    3. {
    4. if (!index.isValid())
    5. {
    6. return nullptr;
    7. }
    8. QVariant qvar = index.model()->data(index, Qt::EditRole);
    9. if (qvar.type() == QVariant::StringList)
    10. {
    11. return new QComboBox(parent);
    12. }
    13. else if (qvar.type() == QVariant::Bool)
    14. {
    15. QCheckBox* checkBox = new QCheckBox(parent);
    16. checkBox->setTristate(false);
    17. return checkBox;
    18. }
    19. else if (qvar.type() == QVariant::Date && !getDateFormatString().isEmpty())
    20. {
    21. QDateEdit* editor = new QDateEdit(parent);
    22. editor->setDisplayFormat(getDateFormatString());
    23. // Annoying that I need this!
    24. editor->setAutoFillBackground(true);
    25. return editor;
    26. }
    27. else if (qvar.type() == QVariant::DateTime && !getDateTimeFormatString().isEmpty())
    28. {
    29. QDateTimeEdit* editor = new QDateTimeEdit(parent);
    30. editor->setDisplayFormat(getDateTimeFormatString());
    31. // Annoying that I need this!
    32. editor->setAutoFillBackground(true);
    33. return editor;
    34. }
    35. else if (qvar.type() == QVariant::Time && !getTimeFormatString().isEmpty())
    36. {
    37. QTimeEdit* editor = new QTimeEdit(parent);
    38. editor->setDisplayFormat(getTimeFormatString());
    39. // Annoying that I need this!
    40. editor->setAutoFillBackground(true);
    41. return editor;
    42. }
    43.  
    44. //widget = new QLineEdit(parent);
    45. // Returns an expanding line editor.
    46. QWidget* widget = QStyledItemDelegate::createEditor(parent, option, index);
    47.  
    48. // If I do not set this to true, then the "view" data shows through the text while editing.
    49. // Also, the expanding editor grows over existing controls, and you can see those through
    50. // the edit display as well (distracting).
    51. widget->setAutoFillBackground(true);
    52. return widget;
    53. }
    To copy to clipboard, switch view to plain text mode 

    Note that I have a specific delegate that I use for check boolean / check boxes. I also have more code to handle setting editor data and such, but, not worth pasting it unless I know you will bother to use is.

  5. The following user says thank you to pitonyak for this useful post:

    yagabey (14th November 2014)

  6. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Display data from database after preprocessing

    If you don't need to edit the SQL model in the view (in other words, it is for display only), then you can derive a new proxy model from QIdentityProxyModel and override the QIdentityProxyModel::data() method to format your dates. You set the proxy model on your view, and the SQL model as the source model for the proxy. The Qt docs give exactly that case as an example.

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

    yagabey (14th November 2014)

Similar Threads

  1. Replies: 2
    Last Post: 6th April 2014, 11:07
  2. Replies: 7
    Last Post: 30th August 2009, 22:58
  3. Regarding Database Display through user interface
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2008, 10:32

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.