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. Formatting numbers in QTableView [SOLVED]
Qt 6.11 is out! See what's new in the release blog

Formatting numbers in QTableView [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 11.6k 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.
  • P Offline
    P Offline
    puterk
    wrote on last edited by
    #1

    Hello,

    I have a QTableView that is populated by a QSqlQueryModel. The data in the model has columns that must be formatted this way: "#,##0.00". Is it possible to format some columns without subclassing the model or the widget? The QTableView would be read-only so there is no editing needed. The values would have to be retrieved though.

    This is how I usually populate a QTableView:

    @
    QSqlQuery query;
    query.prepare("SELECT * FROM tcustomer");
    query.exec();
    QSqlQueryModel model;
    model.setQuery(query);
    ui->tableView->setModel(model);
    @

    1 Reply Last reply
    0
    • P Offline
      P Offline
      panosk
      wrote on last edited by
      #2

      Hi,

      Maybe you could use a custom "delegate":http://qt-project.org/doc/qt-5.0/qtwidgets/qstyleditemdelegate.html to define the display you like and then add it only to the columns you want.

      1 Reply Last reply
      1
      • P Offline
        P Offline
        puterk
        wrote on last edited by
        #3

        Thank you.

        I used QStyledItemDelegate. Here is what I did:

        numberformatdelegate.h
        @
        #ifndef NUMBERFORMATDELEGATE_H
        #define NUMBERFORMATDELEGATE_H

        #include <QStyledItemDelegate>

        class NumberFormatDelegate : public QStyledItemDelegate
        {
        Q_OBJECT
        public:
        explicit NumberFormatDelegate(QObject *parent = 0);
        virtual QString displayText(const QVariant &value, const QLocale &locale) const;

        signals:

        public slots:

        };

        #endif // NUMBERFORMATDELEGATE_H@

        numberformatdelegate.cpp
        @#include "numberformatdelegate.h"

        NumberFormatDelegate::NumberFormatDelegate(QObject *parent) :
        QStyledItemDelegate(parent)
        {
        }

        QString NumberFormatDelegate::displayText(const QVariant &value, const QLocale &locale) const
        {
        QString formattedNum = locale.toString(value.toDouble(), 'f', 2);
        return formattedNum;
        }
        @

        To use it on a tableview:
        @
        ui->tableView->setItemDelegateForColumn(3, new NumberFormatDelegate(this));
        @

        1 Reply Last reply
        1
        • P Offline
          P Offline
          panosk
          wrote on last edited by
          #4

          Great, don't forget to edit your post and add [solved] :-)

          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