其他分享
首页 > 其他分享> > qt qtablewidget 表头添加多选框,示例 qtablewighet相关问题

qt qtablewidget 表头添加多选框,示例 qtablewighet相关问题

作者:互联网

 

 

#ifndef SCHECKBOXHEADERVIEW_H
#define SCHECKBOXHEADERVIEW_H
#include <QtGui>
#include <QPainter>
#include <QHeaderView>
#include <QStyleOptionButton>
#include <QStyle>
class SCheckBoxHeaderView : public QHeaderView
{
    Q_OBJECT
private:
    bool isChecked;
    int m_checkColIdx;
public:
    SCheckBoxHeaderView( int checkColumnIndex, Qt::Orientation orientation, QWidget * parent = 0) :
    QHeaderView(orientation, parent) {
        m_checkColIdx = checkColumnIndex;
        isChecked = false;
    }
signals:
    void checkStausChange(bool);
protected:
    void 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;
            for (int i=0; i<logicalIndex; ++i)
            width += sectionSize( i );
            option.rect = QRect(3, 5, 21, 21);
            if (isChecked)
                option.state = QStyle::State_On;
            else
                option.state = QStyle::State_Off;
            this->style()->drawControl(QStyle::CE_CheckBox, &option, painter);
        }
    }
    void mousePressEvent(QMouseEvent *event) {
        if (visualIndexAt(event->pos().x()) == m_checkColIdx) {
            isChecked = !isChecked;
            this->updateSection(m_checkColIdx);
            emit checkStausChange(isChecked);
        }
        QHeaderView::mousePressEvent(event);
    }
};
#endif // SCHECKBOXHEADERVIEW_H

/*
 * demo

    SCheckBoxHeaderView* m_checkHeader = new SCheckBoxHeaderView(0, Qt::Horizontal, this);
    ui->table_protocolCANdata->setHorizontalHeader(m_checkHeader);//  这个this指针的父为QTableWidget
    connect(m_checkHeader, &SCheckBoxHeaderView::checkStausChange, [=](bool check){
        qDebug() << "is:" <<check;
        if(check){
            for(int i=0;i<ui->table_protocolCANdata->rowCount();i++){
                ui->table_protocolCANdata->item(i,0)->setCheckState(Qt::Checked);;
            }
        }
        else{
            for(int i=0;i<ui->table_protocolCANdata->rowCount();i++){
                ui->table_protocolCANdata->item(i,0)->setCheckState(Qt::Unchecked);;
            }
        }
    });


    QTableWidget *table = ui->table_protocolCANdata;

    table->clearContents();
    table->setRowCount(0);
    table->clear();
    QStringList tableHead;
    tableHead<<"    "<< QString("time")<<QString("T")<<QString("P")<<QString("I")<<QString("D");

    table->setColumnCount(tableHead.count());
    table->setHorizontalHeaderLabels(tableHead);
    //table->verticalHeader()->setHidden(true);//隐藏列序号
    //表格通用操作,比如列宽,哪一列可以编辑
    table->setFont(QFont("Helvetica [Cronyx]", 13));
    //table->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
    table->setSelectionMode(QAbstractItemView::SingleSelection); //一次只能选中一行

    //table->horizontalHeader()->setSectionResizeMode( QHeaderView::Fixed );
    table->verticalHeader()->setSectionResizeMode( QHeaderView::Fixed );
    table->horizontalHeader()->setMinimumHeight(30);

    table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);    //x先自适应宽度
    //table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);     //然后设置要根据内容使用宽度的列
    table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed );
    table->setColumnWidth(0,20);//第一列设置宽度

    //数据
    //清空以前的数据
    table->clearContents();
    table->setRowCount(0);
    for(int i=0;i<10;i++){
        table->setRowCount(table->rowCount()+1);
        QTableWidgetItem *check=new QTableWidgetItem("");
        check->setCheckState(Qt::Unchecked);
        table->setItem(table->rowCount()-1,0,check);
        for(int j=1;j<tableHead.count();j++){
            QString res = tableHead.at(j);
            QTableWidgetItem *check=new QTableWidgetItem(res);
            //设置只读 check->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
            table->setItem(table->rowCount()-1,j,check);
        }
    }

*/

 

 

 

 

//

标签:QHeaderView,qt,示例,int,表头,table,include,protocolCANdata,Qt
来源: https://www.cnblogs.com/RYSBlog/p/16501911.html