QTableView and floating numbers editing.
-
I have some QTableView to show data from SQLite db using QSqlTableModel. I have numeric field in my db table and I want to edit values in these fields using QDoubleSpinBox in QTableView. As far as I understand after searching examples in the Internet, I should create my own subclass of Q(Styled)ItemDelegate to do it. But QT documentation says:
bq. The standard views provided with Qt use instances of QItemDelegate to provide editing facilities. This default implementation of the delegate interface renders items in the usual style for each of the standard views: QListView, QTableView, and QTreeView. All the standard roles are handled by the default delegate used by the standard views. The way these are interpreted is described in the QItemDelegate documentation.
So, QTableView uses default implementation of QItemDelegate to display and edit data.
bq. When editing data in an item view, QItemDelegate provides an editor widget, which is a widget that is placed on top of the view while editing takes place. Editors are created with a QItemEditorFactory; a default static instance provided by QItemEditorFactory is installed on all item delegates.
So, QItemDelegate uses default QItemEditorFactory implementation to provide data editing.
bq. The standard factory implementation provides editors for a variety of data types. These are created whenever a delegate needs to provide an editor for data supplied by a model. The following table shows the relationship between types and the standard editors provided.
...
double: QDoubleSpinBoxSo, default implementation of QItemEditorFactory uses QDoubleSpinBox to edit floating numbers.
And what is my fault? Why there are no any QDoubleSpinBox'es in my table?
The code example:
@compModel.setTable("tbl");
compModel.setEditStrategy(QSqlTableModel::OnManualSubmit);
ui->compView->setModel(&compModel);
ui->compView->setItemDelegate(new QItemDelegate(ui->compView));
compModel.select();@P.S. Any help would be appreciated. Sorry for bad English, I hope, you'll understand what's my problem.
-
If the call to data() returns a QVariant::Double then the default delegate will give you a QDoubleSpinBox. If the model returns a QVariant::Int or QVariant::UInt then you should get a QSpinBox out of the default delegate. So, first check what your table definition is, e.g. REAL or INT and not text, and what QVariant type the model returns.
You can force the situation by providing your own delegate that always uses a QDoubleSpinBox and attaching it to the columns of interest.
-
Thank you very much!
Field in my table is declared as numeric and, as I discovered few hours ago, Qt returns data as QVariant(qlonglong) if the stored number has only integral part and hasn't fractional part. But it returns QVariant(double) if fractional part is present.
Can I change this behaviour some way and force double for all values in columns which are declared as numeric?