PDA

View Full Version : QTableView, sqlite, wrong data type



bzqt
10th October 2011, 22:15
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.



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
db.open();

QSqlQuery q;
q.exec("create table t (col1)");
q.exec("insert into t (col1) values (161726.36)");

QSqlQueryModel *m = new QSqlQueryModel();
m->setQuery("select col1 from t");

ui->tableView->setModel(m);
}

ChrisW67
11th October 2011, 00:56
The default delegate handles the display of the DisplayRole of the model. For doubles:


case QVariant::Double:
text = locale.toString(value.toReal());
break;

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).