Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QStyledItemDelagete DecorationAlignment Do Not Work



  • Hi everyone,

    in qtableview, I want decoration role to be displayed in the center of the cell
    However, decorationalignment of QStyledItemDelegate do not work.

    I have a custom delegate class and the code about decoration role is like below.
    It is weird that decorationPosition is working, but it does not have centering the content (top right bottom left). decorationAlignment do not work. Also, I tried it in paint event but it also fails.

    void StyledItemDelegate::initStyleOption(QStyleOptionViewItem *in_p_option, const QModelIndex &in_index) const
    {
        QStyledItemDelegate::initStyleOption(in_p_option,in_index);
        if (m_features & DecorationCenter)
        {
            in_p_option->decorationAlignment = Qt::AlignCenter;
        }
    
    }
    


  • Hi everyone,

    I solved the problem.

    in_p_option->decorationSize plays key role in this. before centering alignment decoration size should be changed also.

    By code is like below.

    QTableView *p_table = dynamic_cast<QTableView *>(in_index.model()->parent());
    in_p_option->decorationSize = QSize(p_table->columnWidth(in_index.column()),p_table->verticalHeader()->defaultSectionSize());
            in_p_option->decorationAlignment = Qt::AlignCenter;
    

Log in to reply