ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-07-21 15:04:35  阅读:161  来源: 互联网

标签:QHeaderView qt 示例 int 表头 table include protocolCANdata Qt


 

 

#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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有