Files

74 lines
1.7 KiB
C++
Raw Permalink Normal View History

#include "CheckBoxHeaderView.h"
CheckBoxHeaderView::CheckBoxHeaderView(int checkColumnIndex, Qt::Orientation orientation, QWidget* parent)
:QHeaderView(orientation, parent)
{
m_checkColIdx = checkColumnIndex;
m_isChecked = false;
}
void CheckBoxHeaderView::setCheckStaus(bool isChecked)
{
m_isChecked = isChecked;
this->updateSection(m_checkColIdx);
}
bool CheckBoxHeaderView::getCheckStaus()
{
return m_isChecked;
}
/// <summary>
/// 绘制区域事件
/// </summary>
/// <param name="painter">画笔</param>
/// <param name="rect">区域</param>
/// <param name="logicalIndex">索引</param>
void CheckBoxHeaderView::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
{
// 保存当前状态
painter->save();
// 绘制区域
QHeaderView::paintSection(painter, rect, logicalIndex);
// 恢复状态
painter->restore();
// 判断是否为所选行列
if (logicalIndex == m_checkColIdx)
{
QStyleOptionButton option;
int width = 10;
option.rect = QRect((rect.right() - rect.left()) / 2 - width / 2 + rect.left() - 1, (rect.bottom() - rect.top()) / 2 - width / 2 + rect.top() - 1, width, width);
if (m_isChecked)
{
option.state = QStyle::State_Enabled | QStyle::State_On;
}
else
{
option.state = QStyle::State_Enabled | QStyle::State_Off;
}
this->style()->drawControl(QStyle::CE_CheckBox, &option, painter);
}
}
/// <summary>
/// 鼠标按下事件
/// </summary>
/// <param name="event">事件</param>
void CheckBoxHeaderView::mousePressEvent(QMouseEvent* event)
{
if (visualIndexAt(event->pos().x()) == m_checkColIdx)
{
if (m_isChecked)
m_isChecked = false;
else
m_isChecked = true;
this->updateSection(m_checkColIdx);
emit checkStausChange(m_isChecked);
}
QHeaderView::mousePressEvent(event);
}