1         QTableWidget自定义表头QHeaderView加全选复选框

在使用QTableWidget时需要在表头添加全选复选框,但是默认的表头无法添加复选框,只能用图片画上去一个复选框了。并且实现点击复选框时,发出信号,让QTableWidget中每条记录的复选框都选中,取消时,QTableWidget中每条记录的复选框都取消。并且实现复选框的normal、hov、pressed三种状态的显示。三种状态需要设置背景图片。效果图如下

QT QAbstractItemModel中复选框应用 qt复选框全选_#include

                       

(1)   继承QHeaderView,重写绘制函数,绘制表头,重写鼠标移动函数,用来获取鼠标位置,鼠标移动到复选按钮上时,显示高亮。重写鼠标点击函数,点击复选框时,发送信号控制选中和取消。

头文件:

#pragma once
#include <QHeaderView>
#include <QPainter>
#include <QMouseEvent>
using namespace std;
class MyQHeaderView : public QHeaderView
{
       Q_OBJECT
public:
       explicit MyQHeaderView(Qt::Orientation orientation, QWidget *parent = 0);
signals:
       void headCheckBoxToggled(bool checked);
 
protected:
       void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;
       void mousePressEvent(QMouseEvent *event);
       void mouseMoveEvent(QMouseEvent *event);
 
private:
       bool m_isOn;//是否选中
       QPoint m_mousePoint;//鼠标位置
       mutable QRect m_RectHeaderCheckBox;//复选框的位置
};

源文件

#include "QMyHeaderView.h"
 
MyQHeaderView::MyQHeaderView(Qt::Orientation orientation, QWidget *parent)
       : QHeaderView(orientation, parent)
       , m_isOn(false)
{
       m_isOn = false;
       m_mousePoint.setX(100);
       m_mousePoint.setY(100);
}
 
/*绘制表头logicalIndex表示表头第几列,0表示第0列,也就是我们要画复选框的列,rect是表头第一列的大小*/
void MyQHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
       painter->save();
       QHeaderView::paintSection(painter, rect, logicalIndex);
       painter->restore();
       if (logicalIndex == 0)//第一列
       {
              //保存第一列的位置
              m_RectHeaderCheckBox.setX(rect.x() + 3);
              m_RectHeaderCheckBox.setY(rect.y() + 10);
              m_RectHeaderCheckBox.setWidth(14);
              m_RectHeaderCheckBox.setHeight(14);    
              QStyleOptionButton option;
              QPixmap pix;
              if (m_isOn)
              {//图片需要在资源里添加
                     pix.load(":/checkImage/image/checkImage/check-sel.png");//选中
                     option.state = QStyle::State_On;
              }
              else if (m_RectHeaderCheckBox.contains(m_mousePoint))
              {
                     pix.load(":/checkImage/image/checkImage/check-hov.png");//高亮
                     option.state = QStyle::State_HasFocus;
              }
              else
              {
                     pix.load(":/checkImage/image/checkImage/check-nor.png");//正常
                     option.state = QStyle::State_Off;
              }
              style()->drawItemPixmap(painter, rect, Qt::AlignLeft | Qt::AlignVCenter, pix);
              //方法2,画选择按钮,,无法设置背景图片
              //
              //option.rect = m_RectHeaderCheckBox;
              //QIcon icon(pix);
              //option.icon = icon;//setStyleSheet("background");
              //style()->drawControl(QStyle::CE_CheckBox, &option, painter);
       }
}
 
void MyQHeaderView::mousePressEvent(QMouseEvent *event)
{
       //表头可控制,鼠标第一个列,鼠标在矩形范围内
      if (isEnabled() && logicalIndexAt(event->pos()) == 0&&m_RectHeaderCheckBox.contains(event->pos()))
       {
              m_isOn = !m_isOn;
              updateSection(0);//刷新表头第一列
              emit headCheckBoxToggled(m_isOn);
       }
       else QHeaderView::mousePressEvent(event);
}
void MyQHeaderView::mouseMoveEvent(QMouseEvent *event)
{
       //保存鼠标的位置
       m_mousePoint = event->pos();
       if (m_RectHeaderCheckBox.contains(m_mousePoint))
       {
              updateSection(0);//重绘表头复选框
       }
        QHeaderView::mouseMoveEvent(event);
}

(2)   Qdesign中加入QTablewidget,并添加表头字段,第一个为空,用于添加选择复选框。

QT QAbstractItemModel中复选框应用 qt复选框全选_Qt_02

 

(3)   在主界面类中new一个表头对象,设置到QtableWidget

//new表头对象
MyQHeaderView*myHeader = new MyQHeaderView(Qt::Horizontal, ui.tableWidget);
myHeader->setStretchLastSection(true);
myHeader->setStyleSheet("alignment: left;");
//连接单击信号槽,在槽函数中
QObject::connect(myHeader, &MyQHeaderView::headCheckBoxToggled, this, &AlarmCenter::SetAlarmListCheckState);
ui.tableWidget->setHorizontalHeader(myHeader);//设置表头

(4)   至于QTableWidget表格中数据前面的复选框则是用CheckBox

QCheckBox *CheckBox = new QCheckBox(ui.tableWidget);
QSize size(39, 35);
CheckBox->setFixedSize(size);
CheckBox->setCheckState(Qt::Unchecked);
//创建单击选中的信号槽连接
QObject::connect(CheckBox, &QCheckBox::clicked,this, [=]()
{
       AlarmTableItemChoosed(row, 0);
});
ui.tableWidget->setCellWidget(row, 0, CheckBox);//row行,0列

 

 

 

自己编了一个股票监控软件,有如下功能,有兴趣的朋友可以下载;

(1)   个股监测。监测个股实时变化,可以监测个股大单交易、急速拉升和下降、主力入场和出场、股票最高点和最低点提醒。检测到最高点、最低点、主力进场点、主力退场点、急速拉升点、急速下跌点,给出语音或者声音提醒,不用再时刻看着大盘了,给你更多自由的时间;

(2)   大盘监测。监测大盘的走势,采用上证、深证、创业三大指数的综合指数作为大盘走势。并实时监测大盘的最高点和最低点、中间的转折点。

(3)   股票推荐。还能根据历史数据长期或短期走势进行分析,对股市3千多个股票进行分析对比,选出涨势良好的股票,按照增长速度从大到小排序,推荐给你涨势良好的股票;

下载地址:

1.0.3版本(修复大盘指数崩溃缺陷)下载地址:

链接:https://pan.baidu.com/s/1BJcTp-kdniM7VE9K5Kd3vg 提取码:003h