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. An example of QStyledItemDelegate formatting doubles

An example of QStyledItemDelegate formatting doubles

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.5k Views
  • 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.
  • starryeyedS Offline
    starryeyedS Offline
    starryeyed
    wrote on last edited by
    #1

    Hello, I tried approaching formatting through telling the QSqlQueryModel on which columns to return Qt::AlignRight for Qt::TextAlignmentRole. Now I understand that I need to subclass QStyledItemDelegate. All I need to know is how to subclass in a minimalistic way (according to this post, I only need to override displayText() method). I tried googling examples and only found star-rating editor example which is not helpful in my case. I tried googling QSID source code, but didn't get any idea.
    Please, clarify how do I change formatting (of toString conversion) and alignment of content.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      change formatting is easy, as you already hinted is just about reimplementing displayText and return a QString representation of the data you have. The alignment is completely different. You need to reimplement paint, and make a small change to the original one:

      void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
      {
          Q_ASSERT(index.isValid());
          QStyleOptionViewItem opt = option;
          initStyleOption(&opt, index);
      opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; // only change vs the original
          const QWidget *widget = option.widget;
          QStyle *style = widget ? widget->style() : QApplication::style();
          style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
      }
      

      alternatively you can subclass QSqlQueryModel and reimplement data:

      class AlignedQueryModel : public QSqlQueryModel{
      Q_OBJECT
      Q_DISABLE_COPY(AlignedQueryModel)
      public:
      explicit AlignedQueryModel(QObjetc* parent = Q_NULLPTR) : QSqlQueryModel(parent){}
      QVariant data(const QModelIndex &item, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE{
      if(role == Qt::TextAlignmentRole && item.column()<2) //the first 2 columns are right aligned
      return Qt::AlignRight | Qt::AlignVCenter;
      return QSqlQueryModel::data(item,role);
      }
      };
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      starryeyedS 1 Reply Last reply
      2
      • VRoninV VRonin

        change formatting is easy, as you already hinted is just about reimplementing displayText and return a QString representation of the data you have. The alignment is completely different. You need to reimplement paint, and make a small change to the original one:

        void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
            Q_ASSERT(index.isValid());
            QStyleOptionViewItem opt = option;
            initStyleOption(&opt, index);
        opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; // only change vs the original
            const QWidget *widget = option.widget;
            QStyle *style = widget ? widget->style() : QApplication::style();
            style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
        }
        

        alternatively you can subclass QSqlQueryModel and reimplement data:

        class AlignedQueryModel : public QSqlQueryModel{
        Q_OBJECT
        Q_DISABLE_COPY(AlignedQueryModel)
        public:
        explicit AlignedQueryModel(QObjetc* parent = Q_NULLPTR) : QSqlQueryModel(parent){}
        QVariant data(const QModelIndex &item, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE{
        if(role == Qt::TextAlignmentRole && item.column()<2) //the first 2 columns are right aligned
        return Qt::AlignRight | Qt::AlignVCenter;
        return QSqlQueryModel::data(item,role);
        }
        };
        
        starryeyedS Offline
        starryeyedS Offline
        starryeyed
        wrote on last edited by
        #3

        @VRonin Hello, for now I can't unfortunately get a single of those methods to work. I've started debugging with simpler one, displayText() and it just doesn't work. My other delegates which are inherited from QItemDelegate have a QWidget argument in the constructor. This one doesn't, I hope it's not what makes the difference
        Here's doubledelegate .h/.cpp

        JonBJ 1 Reply Last reply
        0
        • starryeyedS starryeyed

          @VRonin Hello, for now I can't unfortunately get a single of those methods to work. I've started debugging with simpler one, displayText() and it just doesn't work. My other delegates which are inherited from QItemDelegate have a QWidget argument in the constructor. This one doesn't, I hope it's not what makes the difference
          Here's doubledelegate .h/.cpp

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @starryeyed said in An example of QStyledItemDelegate formatting doubles:

          My other delegates which are inherited from QItemDelegate have a QWidget argument in the constructor. This one doesn't,

          What one doesn't?

          Only @VRonin's first example uses QItemDelegate, and he's only given you the paint() override, so you can have what you like in your constructor for MyDelegate, like the base QItemDeletegate.

          His second example doesn't use a QItemDelegate, it just sub-classes QSqlQueryModel.

          Both QItemDelegate & QSqlQueryModel constructors take an optional QObject *parent = nullptr. Like many Qt classes, this is just to set the parent. I don't see how that's relevant to whether they work or not.

          starryeyedS 1 Reply Last reply
          2
          • JonBJ JonB

            @starryeyed said in An example of QStyledItemDelegate formatting doubles:

            My other delegates which are inherited from QItemDelegate have a QWidget argument in the constructor. This one doesn't,

            What one doesn't?

            Only @VRonin's first example uses QItemDelegate, and he's only given you the paint() override, so you can have what you like in your constructor for MyDelegate, like the base QItemDeletegate.

            His second example doesn't use a QItemDelegate, it just sub-classes QSqlQueryModel.

            Both QItemDelegate & QSqlQueryModel constructors take an optional QObject *parent = nullptr. Like many Qt classes, this is just to set the parent. I don't see how that's relevant to whether they work or not.

            starryeyedS Offline
            starryeyedS Offline
            starryeyed
            wrote on last edited by
            #5

            @JonB I'm sorry for digging in wrong direction, I mistook compiler's errors about "overriding private parts of class", thought that it was related to constructor because the error was on constructor's definition line.
            So everything works. Blessings go out to the patience of local gurus

            JonBJ 1 Reply Last reply
            1
            • starryeyedS starryeyed

              @JonB I'm sorry for digging in wrong direction, I mistook compiler's errors about "overriding private parts of class", thought that it was related to constructor because the error was on constructor's definition line.
              So everything works. Blessings go out to the patience of local gurus

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @starryeyed You want to be careful about "overriding private parts", it can be painful ;-)

              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