Adding Vertical headers to a QTreeView

大兔子大兔子 提交于 2020-01-01 17:10:46

问题


I have a QTreeView subclass (and QAbstractItemModel subclass) which has a nice horizontal header. I would like to add vertical headers (going down the left side) to match. But unlike QTableView which has separate vertical (setVerticalHeader()) and horizontal headers (setHorizontalHeader()), QTreeView only allows a single header (setHeader()).

I know that I can just pretend that the leftmost column is the header and render it with a different background color (I don't need the ability to resize or rearrange the rows, so I wouldn't have to implement any of those special behaviors). But that doesn't take into account local styles. For example, on my system the headers have a slight gradient, giving them an almost 3D look.

I am therefore looking for either a way to add a proper vertical header (I am guessing it would be a subclass of QHeaderView) or else rendering a header down the leftmost column, but rendering it the Right Way.


回答1:


Your probably right that you will have to "pretend" but you should be able to draw it to look as good as your horizontal headers.

What you need to do is be in charge of the paintEvent for those cells and then use the current style to draw a custom control. Something like:

//QWidget* w is the widget who's style you want
QPainter painter(this);
QStyleOptionHeader opt;
opt.initFrom(this);
opt.state = QStyle::State_None;
opt.orientation = Qt::Vertical;
opt.state |= QStyle::State_Vertical;
if (w->isEnabled())
    opt.state |= QStyle::State_Enabled;
opt.state |= QStyle::State_Active;
w->style()->drawControl(QStyle::CE_Header, &opt, &painter, w);

The easiest way to be in charge of a paint event for the cells you want would be to create a custom item delegate and reimplement the virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const = 0 method. You just gate your painting to the indexes you care about and pass the rest to the super class (see Qt's docs for this class).



来源:https://stackoverflow.com/questions/1967210/adding-vertical-headers-to-a-qtreeview

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