自定义带checkbox的QHeadView及HeaderView的Style

#include <QPainter>
#include <QWindowsStyle>
#include <QStyleOptionHeader>


class Style: public QWindowsStyle 
{
public:
Style () {}


void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const
{
if (element == PE_IndicatorHeaderArrow )
{
const QStyleOptionHeader *headerOption = qstyleoption_cast<const QStyleOptionHeader *>(option);
int x, y, width, height;
option->rect.getRect(&x, &y, &width, &height);
QPixmap pixmap;
if (headerOption->sortIndicator == QStyleOptionHeader::SortUp)
pixmap.load(PNG_BG_ARROWUP);
else
pixmap.load(PNG_BG_ARROWDOWN);
painter->save();
painter->drawPixmap(x, y, pixmap);
painter->restore();
}
else
QWindowsStyle::drawPrimitive(element, option, painter, widget);
}
};


class CHeaderView: public QHeaderView
{
public:
CHeaderView(Qt::Orientation orientation, QWidget * parent = 0) : QHeaderView(orientation, parent)
{}


protected:
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);  
painter->restore();
if(logicalIndex == 0)
{
QStyleOptionButton option;
option.rect = QRect(rect.x()+40, rect.y()+10, 16, 30);
QPixmap pixmap;
if (isOn)
{
pixmap.load(PNG_BG_ARROWUP);
option.state = QStyle::State_On;
}
else
{
pixmap.load(PNG_BG_ARROWDOWN);
option.state = QStyle::State_Off;
}
painter->save();
painter->drawPixmap(rect.width()-7, 5, pixmap);
painter->restore();


this->style()->drawPrimitive(QStyle::PE_Frame, &option, painter);
}
}
void mousePressEvent(QMouseEvent *event)
{
if (isOn)
isOn = false;
else
isOn = true;
this->update();
QHeaderView::mousePressEvent(event);
}


private:
bool isOn;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值