Solved More than one delegate for a column ?
-
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.
-
-
1st of all, don't reimplement
paint()
, reimplementinitStyleOption
otherwise you riskQStyledItemDelegate::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 -
1st of all, don't reimplement
paint()
, reimplementinitStyleOption
otherwise you riskQStyledItemDelegate::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 -
This post is deleted!