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. More than one delegate for a column ?
Forum Updated to NodeBB v4.3 + New Features

More than one delegate for a column ?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 444 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.
  • M Offline
    M Offline
    mehmety888
    wrote on last edited by
    #1

    Hi,

    We have created a table using Qtableview. We will use various types of models inside this table. We will provide some functions for editing the style of this table.

    We cannot use model to edit style because we can only be sure that the table's model is cast to QAbstractItemModel. Therefore we are using subclasses of QStyledItemDelegate to change style of the columns.

    • list itemOne of subclass of QStyledItemDelegate is for changing the alignment of text TextAlignmentDelegate

    • list itemOne of subclass of QStyledItemDelegateis for setting text bold ColumnBoldDelegate

    However, in future we are planning to add more.

    The overridden paint methods are below.

    void TextAlignmentDelegate::paint(QPainter *in_p_painter, const QStyleOptionViewItem &in_option, const QModelIndex &in_index) const
    {
        QStyleOptionViewItem aligned_option(in_option);
        aligned_option.displayAlignment = m_alignment;
        QStyledItemDelegate::paint(in_p_painter, aligned_option, in_index);
    }
    
    void ColumnBoldDelegate::paint(QPainter *in_p_painter, const QStyleOptionViewItem &in_option, const QModelIndex &in_index) const
    {
        QStyleOptionViewItem bold_option(in_option);
        bold_option.font.setBold(m_is_bold);
        QStyledItemDelegate::paint(in_p_painter, bold_option, in_index);
    }
    

    We will have

    • columns with custom text alignment
    • columns with bold text
    • columns with custom text alignment and bold text

    When we use these delegates inside QTableview for each column, the last one cancels the previous delegate. For example when we align text of a column centered then set the text bold, the text aligned left. If we do the opposite then text becomes not bold.

    If we create a delegate for doing both, we are worried about the combination of delegates we need to create because as we said above, there will be lots of style changes.

    Is there a way to solve this problem ?

    Thank you in advance.

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

      1st of all, don't reimplement paint(), reimplement initStyleOption otherwise you risk QStyledItemDelegate::paint overwriting your settings.

      Now to the main point, I'd just merge those delegates in one and set flags to activate/deactivate features:

      class SuperDelegate : public QStyledItemDelegate{
          Q_OBJECT
          Q_DISABLE_COPY_MOVE(SuperDelegate)
      public:
          enum Feature{
          fNone =0x0
          , fBold = 0x1
          , fAlignRight = 0x2
          // etc.
          };
          Q_DECLARE_FLAGS(Features, Feature)
          SuperDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent), m_features(fNone){}
          const Features& features() const;
          void setFeatures(const Features& fts){m_features=fts;}
      private:
          Features m_features;
      protected:
          void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override{
              QStyledItemDelegate::initStyleOption(option,index);
              if(m_features & fBold)
                  option->font.setBold(true);
              if(m_features & fAlignRight)
                  option->displayAlignment = Qt::AlignRight;
              // etc.
          }
      };
      Q_DECLARE_OPERATORS_FOR_FLAGS(SuperDelegate::Features)
      

      Now you can use setFeatures to enable/disable individual features

      "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

      D 1 Reply Last reply
      10
      • VRoninV VRonin

        1st of all, don't reimplement paint(), reimplement initStyleOption otherwise you risk QStyledItemDelegate::paint overwriting your settings.

        Now to the main point, I'd just merge those delegates in one and set flags to activate/deactivate features:

        class SuperDelegate : public QStyledItemDelegate{
            Q_OBJECT
            Q_DISABLE_COPY_MOVE(SuperDelegate)
        public:
            enum Feature{
            fNone =0x0
            , fBold = 0x1
            , fAlignRight = 0x2
            // etc.
            };
            Q_DECLARE_FLAGS(Features, Feature)
            SuperDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent), m_features(fNone){}
            const Features& features() const;
            void setFeatures(const Features& fts){m_features=fts;}
        private:
            Features m_features;
        protected:
            void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override{
                QStyledItemDelegate::initStyleOption(option,index);
                if(m_features & fBold)
                    option->font.setBold(true);
                if(m_features & fAlignRight)
                    option->displayAlignment = Qt::AlignRight;
                // etc.
            }
        };
        Q_DECLARE_OPERATORS_FOR_FLAGS(SuperDelegate::Features)
        

        Now you can use setFeatures to enable/disable individual features

        D Offline
        D Offline
        DzCode
        wrote on last edited by DzCode
        #3
        This post is deleted!
        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