Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView and floating numbers editing.
Forum Updated to NodeBB v4.3 + New Features

QTableView and floating numbers editing.

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 6.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    unikoid
    wrote on last edited by
    #1

    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: QDoubleSpinBox

    So, 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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • U Offline
        U Offline
        unikoid
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • U Offline
          U Offline
          unikoid
          wrote on last edited by
          #4

          Ok, I've read SQLite documentation again and understood that it's sqlite-related issue.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved