Results 1 to 8 of 8

Thread: How to dispaly date in format dd/mm/yyyy using QSqlTableModel

  1. #1
    Join Date
    Nov 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile How to dispaly date in format dd/mm/yyyy using QSqlTableModel

    Hi,
    How to dispaly date in format dd/mm/yyyy using QSqlTableModel
    and through QtableView.
    By default it displays date in yyyy-mm-dd format.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to dispaly date in format dd/mm/yyyy using QSqlTableModel

    QWidget has a property called locale since Qt 4.3. This is used to localize dates and times in items views. In earlier versions, you can do it the way our wiki describes: Localizing dates and times in item views.
    J-P Nurmi

  3. #3
    Join Date
    Nov 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile Re: How to dispaly date in format dd/mm/yyyy using QSqlTableModel

    Hi,
    thanks for your help.
    Please tell me how to use
    "Qt::EditRole" with the model.
    I have stored the date in mysql database.
    And while displaying it in the view it
    need to be in rhe format dd/mm/yyyy.
    please help me.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to dispaly date in format dd/mm/yyyy using QSqlTableModel

    Which version of Qt are you using?
    J-P Nurmi

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

    bala (17th November 2007)

  6. #5
    Join Date
    Nov 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to dispaly date in format dd/mm/yyyy using QSqlTableModel

    Hi,
    I am using Qt 4.2.3.

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to dispaly date in format dd/mm/yyyy using QSqlTableModel

    Quote Originally Posted by bala View Post
    I am using Qt 4.2.3.
    Alright, so you might want to try the approach shown in our wiki. Start with subclassing QSqlTableModel and reimplementing data(). First, you'll get the actual value from base class. Then, you do some adjustments when appropriate, when certain conditions are met:
    Qt Code:
    1. QVariant MySqlTableModel::data(const QModelIndex& index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QModelIndex();
    5.  
    6. // get the actual value from base class
    7. QVariant value = QSqlTableModel::data(index, role);
    8.  
    9. // do adjustments if necessary
    10. if (role == Qt::DisplayRole && index.column() == theColumnWhichContainsDates)
    11. {
    12. QDate date = value.toDate();
    13. value = date.toString("dd/MM/yyyy");
    14. }
    15.  
    16. return value;
    17. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  8. #7
    Join Date
    Nov 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to dispaly date in format dd/mm/yyyy using QSqlTableModel

    Hi,
    I have implemented the code.
    It was compiling fine but while
    running it shows segmentation fault.

  9. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to dispaly date in format dd/mm/yyyy using QSqlTableModel

    Compile the app in debug mode ("qmake -config debug"), run it via debugger ("gdb ./app"), make it crash, and paste backtrace ("bt") here.
    J-P Nurmi

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.