当前位置:网站首页>QTableWidget / QTableView实用技巧
QTableWidget / QTableView实用技巧
2022-06-10 17:26:00 【rainbow_lucky0106】
表头增加复选框QCheckBox(单列)
- 重写QHeaderView
DCheckBoxHeaderView::DCheckBoxHeaderView(Qt::Orientation orientation, QWidget *parent)
:QHeaderView(orientation, parent)
{
m_bPressed = false; // 鼠标按下
m_bChecked = false; // 是否选中
m_bTristate = false; // 三状态框
m_bNoChange = false;
m_bMoving = false;
m_iCheckboxColumn = 0; // 待添加复选框的列
m_sizeIconSize = QSize(20, 20);
m_strText = "";
setSectionsClickable(true); // 响应表头事件
}
void DCheckBoxHeaderView::SetCheckBoxColumn(int iCheckBoxColumn)
{
m_iCheckboxColumn = iCheckBoxColumn;
}
void DCheckBoxHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
// 要画复选框的列
if (logicalIndex == m_iCheckboxColumn)
{
QStyleOptionButton option;
option.initFrom(this);
option.text = m_strText;
if (m_bChecked)
option.state |= QStyle::State_Sunken;
if (m_bTristate && m_bNoChange)
option.state |= QStyle::State_NoChange;
else
option.state |= m_bChecked ? QStyle::State_On : QStyle::State_Off;
// if (testAttribute(Qt::WA_Hover) && underMouse()) {
// if (m_bMoving)
// option.state |= QStyle::State_MouseOver;
// else
// option.state &= ~QStyle::State_MouseOver;
// }
option.iconSize = m_sizeIconSize;
option.rect = rect;
// style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter, &checkBox);
// style()->drawItemPixmap(painter, rect, Qt::AlignCenter, QPixmap(":/images/checkBoxChecked"));
style()->drawControl(QStyle::CE_CheckBox, &option, painter, this);
}
}
// 鼠标按下表头
void DCheckBoxHeaderView::mousePressEvent(QMouseEvent *event)
{
int nColumn = logicalIndexAt(event->pos());
if ((event->buttons() & Qt::LeftButton) && (nColumn == m_iCheckboxColumn))
m_bPressed = true;
else
QHeaderView::mousePressEvent(event);
}
// 鼠标从表头释放,发送信号,更新model数据
void DCheckBoxHeaderView::mouseReleaseEvent(QMouseEvent *event)
{
if (m_bPressed)
{
if (m_bTristate && m_bNoChange)
{
m_bChecked = true;
m_bNoChange = false;
}
else
{
m_bChecked = !m_bChecked;
}
update();
Qt::CheckState state = m_bChecked ? Qt::Checked : Qt::Unchecked;
emit stateChanged(state);
}
else
{
QHeaderView::mouseReleaseEvent(event);
}
m_bPressed = false;
}
// 槽函数,用于更新复选框状态
void DCheckBoxHeaderView::onStateChanged(int state)
{
if (state == Qt::PartiallyChecked) {
m_bTristate = true;
m_bNoChange = true;
} else {
m_bNoChange = false;
}
m_bChecked = (state != Qt::Unchecked);
update();
}
// 鼠标滑过、离开,更新复选框状态
bool DCheckBoxHeaderView::event(QEvent *event)
{
if (event->type() == QEvent::Enter || event->type() == QEvent::Leave)
{
QMouseEvent *pEvent = static_cast<QMouseEvent *>(event);
int nColumn = logicalIndexAt(pEvent->x());
if (nColumn == m_iCheckboxColumn)
{
m_bMoving = (event->type() == QEvent::Enter);
update();
return true;
}
}
return QHeaderView::event(event);
}
- 设进QTableWidget
QStringList m_Header2;
m_Header2<<QString("1")<<QString("2")<<QString("3")<<QString("4")<<QString("5");
DCheckBoxHeaderView *headerview = new DCheckBoxHeaderView();
headerview->SetCheckBoxColumn(3); // 前四列增加复选框
this->setHorizontalHeader(headerview);
this->setHorizontalHeaderLabels(m_Header2);//添加横向表头
https://blog.csdn.net/liang19890820/article/details/50772562
https://blog.csdn.net/weixin_44429308/article/details/106093685
边栏推荐
- 2022 version of idea graphical interface GUI garbled code solution super detailed simple version
- LeetCode 321. Maximum number of splices***
- 开源项目 PM 浅谈如何设计官网
- 分享我做Dotnet9博客网站时积累的一些资料
- 训练时添加进度条的库--tqdm
- 使用IdentityServer出现过SameSite Cookie这个问题吗?
- 云计算搭建全部内容总结,保证可以搭建一个完整的云计算服务器,包括节点安装、实例的分配和网络的配置等内容
- CodeCraft-22 and Codeforces Round #795 (Div. 2)
- Linear mobile chess
- Leetcode 321. Nombre maximum de raccords
猜你喜欢

Canvas fire burning H5 animation JS special effects

com. netflix. client. ClientException: Load balancer does not have available server for client: userser

Canvas大火燃烧h5动画js特效

YML file configuration parameter definition dictionary and list

牛客网:两数之和

【AXI】解读AXI协议双向握手机制的原理

蓝桥杯_挑选子串_组合数学_乘法原理_ / 尺取法

Library for adding progress bar during training --tqdm

numpy——记录

【技术分析】探讨大世界游戏的制作流程及技术——前期流程篇
随机推荐
Why 0.1+0.2=0.3000000000000004
Flutter在数字生活的发展与天翼云盘落地实践
最新好文 | 基于因果推断的可解释对抗防御
The relationship between trees, forests and binary trees
蓝桥杯_挑选子串_组合数学_乘法原理_ / 尺取法
基于注解和反射生成xml
2022上半年信息系统项目管理师论文真题
PMP考生,深圳2022年6月PMP考试地点有这些
js锚点定位可以扩展很多功能
高数_第6章无穷级数__正项级数的性质
Linear mobile chess
Wireshark learning notes (I) common function cases and skills
PCA principal component analysis tutorial (origin analysis & drawing, without R language)
仅需三步学会使用低代码ThingJS与森数据DIX数据对接
聊聊远程办公那些事儿,参与征文领稿费拿大奖!
js手机端复制文本到剪切板代码
Protocol Gen go grpc 'is not an internal or external command, nor is it a runnable program or batch file
Can the "no password era" that apple is looking forward to really come true?
mmcv之Config类介绍
领导提拔你的原因,只有这点最真实,其他都是瞎扯!