| 113 | } |
| 114 | |
| 115 | void CCheckBoxHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const |
| 116 | { |
| 117 | if (!isCheckable(logicalIndex) || isSectionHidden(logicalIndex)) { |
| 118 | QHeaderView::paintSection(painter, rect, logicalIndex); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | // 1) 绘制默认背景 |
| 123 | painter->save(); |
| 124 | QStyleOptionHeader option; |
| 125 | option.initFrom(this); |
| 126 | option.rect = rect; |
| 127 | option.section = logicalIndex; |
| 128 | option.orientation = this->orientation(); |
| 129 | style()->drawControl(QStyle::CE_Header, &option, painter, this); |
| 130 | painter->restore(); |
| 131 | |
| 132 | // 2) 如果该列可显示复选框,绘制复选框 |
| 133 | QRect cbRect = CheckboxRect(rect); |
| 134 | DrawCheckBox(painter, cbRect, GetCheckState(logicalIndex), true); |
| 135 | |
| 136 | // 3) 绘制文本(在复选框右侧留出间距) |
| 137 | QString text; |
| 138 | if (model()) { |
| 139 | QVariant v = model()->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole); |
| 140 | text = v.isValid() ? v.toString() : QString(); |
| 141 | } |
| 142 | if (!text.isEmpty()) { |
| 143 | // 调整文本区域(避开复选框) |
| 144 | int textLeft = cbRect.right() + 4; // 复选框右边留4像素间距 |
| 145 | QRect textRect(textLeft, rect.top(), rect.width() - (textLeft - rect.left()), rect.height()); |
| 146 | // 绘制文本 |
| 147 | QVariant vAlign = model() ? model()->headerData( |
| 148 | logicalIndex, orientation(), |
| 149 | Qt::TextAlignmentRole) |
| 150 | : Qt::AlignLeft; |
| 151 | Qt::Alignment alignment = Qt::AlignCenter; |
| 152 | if(vAlign.isValid()) |
| 153 | alignment = (Qt::Alignment)vAlign.toInt(); |
| 154 | this->DrawText(painter, textRect, text, alignment); |
| 155 | } |
| 156 | |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | void CCheckBoxHeader::DrawCheckBox(QPainter* painter, const QRect& rect, |
| 161 | Qt::CheckState state, bool enabled) const |
nothing calls this directly
no test coverage detected