How to implement delegate in QHeaderView

痞子三分冷 提交于 2019-12-12 04:45:55

问题


I have created one table by using QTableview and QAbstractTableModel . i have added some vertical header by using QHeaderView . In one of the header cell i want to use delegate ..

I am using the delegate but it does not have any impact ..

Is anywhere i am doing wrong ?


回答1:


Had this issue myself. The answer from the Qt documentation is simple and annoying:

Note: Each header renders the data for each section itself, and does not rely on a delegate. As a result, calling a header's setItemDelegate() function will have no effect.

In other words you cannot use delegates with QHeaderView.




回答2:


For the record, if you want to style a QHeaderView section, you'll have to do it either via the header data model (changing Qt::FontRole, etc.) or derive your own QHeaderView (don't forget to pass it to your table with "setVerticalHeader()") and overwrite the its paintSection()-function. i.e.:

void YourCustomHeaderView::paintSection(QPainter* in_p_painter, const QRect& in_rect, int in_section) const
{
    if (nullptr == in_p_painter)
        return;

    // Paint default sections
    in_p_painter->save();
    QHeaderView::paintSection(in_p_painter, in_rect, in_section);
    in_p_painter->restore();

    // Paint your custom section content OVER a specific, finished
    // default section (identified by index in this case)
    if (m_your_custom_section_index == in_section)
    {
        QPen pen = in_p_painter->pen();
        pen.setWidthF(5.5);
        pen.setColor(QColor(m_separator_color));

        in_p_painter->setPen(pen);
        in_p_painter->drawLine(in_rect.right(), in_rect.top(), in_rect.right(), in_rect.bottom());
    }
}

This simplified example could of course easily be done via a stylesheet instead, but you could theoretically draw whatever you like using this method.



来源:https://stackoverflow.com/questions/11895388/how-to-implement-delegate-in-qheaderview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!