Results 1 to 2 of 2

Thread: QTableView, sqlite, wrong data type

  1. #1
    Join Date
    Oct 2011
    Posts
    1

    Default QTableView, sqlite, wrong data type

    I have a problem with QTableView not showing fractional part of a floating point number retrieved from sqlite query (i.e. QSqlQueryModel).

    I've tested it with QSqlQuery - q.value(0).toDouble() returns correct floating point value. Yet QTableView shows only integer part.

    How can I change QTableView or QSqlQueryModel column type?

    edit: Here is sample code. Requires tableView object on MainWindow.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    8. db.setDatabaseName(":memory:");
    9. db.open();
    10.  
    11. q.exec("create table t (col1)");
    12. q.exec("insert into t (col1) values (161726.36)");
    13.  
    14. m->setQuery("select col1 from t");
    15.  
    16. ui->tableView->setModel(m);
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by bzqt; 10th October 2011 at 21:44.

  2. #2
    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: QTableView, sqlite, wrong data type

    The default delegate handles the display of the DisplayRole of the model. For doubles:
    Qt Code:
    1. case QVariant::Double:
    2. text = locale.toString(value.toReal());
    3. break;
    To copy to clipboard, switch view to plain text mode 
    The default formatting for QLocale::toString() with a double argument is 6 significant digits (not digits after the decimal point, see QString::number() argument formats). You want to provide your own delegate subclass that overrides displayText() or customise the model to return a formatted string in the DisplayRole (still return a Double in EditRole).

Similar Threads

  1. Update changes in QTableView to sqlite data base
    By nagabathula in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2011, 01:18
  2. Embedding a SQLite database in a custom file type
    By sortaSean in forum Qt Programming
    Replies: 0
    Last Post: 7th December 2010, 01:35
  3. Replies: 0
    Last Post: 21st April 2010, 16:25
  4. QVariant not returning proper type for SQLite REAL
    By rakkar in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2009, 20:25
  5. wrong sort in qtableview
    By toem in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2009, 10:30

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.