#ifndef CUSTOMHEADER_H
#define CUSTOMHEADER_H
#include<QtGui>
#include<QHeaderView>
#include <QtCore>
#include <QLibrary>
{
Q_OBJECT
public:
CustomHeader
(Qt
::Orientation orientation ,
QWidget *parent
=0) : QHeaderView(orientation,parent
) {}
signals:
void clickCheckBox_SG(bool);
protected:
void paintSection
(QPainter *painter,
const QRect &rect,
int logicalIndex
) const;
private:
bool isOn;
};
//CPP file
void CustomHeader
::paintSection(QPainter *painter,
const QRect &rect,
int logicalIndex
) const {
painter->save();
qDebug()<<logicalIndex<<__LINE__<<__FUNCTION__;
painter->restore();
if(logicalIndex == 0)
{
option.
rect = QRect(2,
5,
20,
20);
option.rect.moveLeft(0);
option.rect.moveTop(5);
// tries to change the border color of CheckBox
option.
palette.
setColor(QPalette::ColorRole::Background,color_b
);
if(isOn)
{
option.
state = QStyle::State_On;
qDebug()<<isOn<<__LINE__<<__FUNCTION__;
}
else
{
option.
state = QStyle::State_Off;
qDebug()<<isOn<<__LINE__<<__FUNCTION__;
}
this
->style
()->drawPrimitive
(QStyle::PE_IndicatorCheckBox,
&option,painter
);
}
}
{
if(isOn)
{
isOn = false;
qDebug()<<isOn<<__LINE__<<__FUNCTION__;
emit clickCheckBox_SG(isOn);
}
else
{
isOn = true;
qDebug()<<isOn<<__LINE__<<__FUNCTION__;
emit clickCheckBox_SG(isOn);
}
this->update();
}
in mainwindow.cpp
void MainWindow::setTableWidgetGui()
{
list << "" << "A" << "B" << "C" << "D" << "";
ui->tableWidget->setColumnCount(6);
customHeader = new CustomHeader(Qt::Horizontal,ui->tableWidget);
//QObject::connect(customHeader,SIGNAL(sectionPressed(int)),this , SLOT(UdateAllRowCheckBox(int)));
//this signal slot is used to check other checkboxes
qDebug()<<connect(customHeader , SIGNAL(clickCheckBox_SG(bool)) , this , SLOT(UdateAllRowCheckBox(bool)));
// UdateAllRowCheckBox(0);
ui->tableWidget->setHorizontalHeader(customHeader);
ui->tableWidget->setHorizontalHeaderLabels(list);
}
#ifndef CUSTOMHEADER_H
#define CUSTOMHEADER_H
#include<QtGui>
#include<QHeaderView>
#include <QtCore>
#include <QLibrary>
class CustomHeader : public QHeaderView
{
Q_OBJECT
public:
CustomHeader(Qt::Orientation orientation , QWidget *parent =0) : QHeaderView(orientation,parent)
{}
signals:
void clickCheckBox_SG(bool);
protected:
void paintSection(QPainter *painter,const QRect &rect, int logicalIndex) const;
void mousePressEvent(QMouseEvent *event);
private:
bool isOn;
};
//CPP file
void CustomHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
QColor color_b(255,255,0,0);
painter->save();
qDebug()<<logicalIndex<<__LINE__<<__FUNCTION__;
QHeaderView::paintSection(painter,rect,logicalIndex);
painter->restore();
if(logicalIndex == 0)
{
QStyleOptionButton option;
option.rect = QRect(2,5,20,20);
option.rect.moveLeft(0);
option.rect.moveTop(5);
// tries to change the border color of CheckBox
option.palette = QPalette();
option.palette.setColor(QPalette::ColorRole::Background,color_b);
if(isOn)
{
option.state = QStyle::State_On;
qDebug()<<isOn<<__LINE__<<__FUNCTION__;
}
else
{
option.state = QStyle::State_Off;
qDebug()<<isOn<<__LINE__<<__FUNCTION__;
}
this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox,&option,painter);
}
}
void CustomHeader::mousePressEvent(QMouseEvent *event)
{
if(isOn)
{
isOn = false;
qDebug()<<isOn<<__LINE__<<__FUNCTION__;
emit clickCheckBox_SG(isOn);
}
else
{
isOn = true;
qDebug()<<isOn<<__LINE__<<__FUNCTION__;
emit clickCheckBox_SG(isOn);
}
this->update();
QHeaderView::mousePressEvent(event);
}
in mainwindow.cpp
void MainWindow::setTableWidgetGui()
{
QStringList list ;
list << "" << "A" << "B" << "C" << "D" << "";
ui->tableWidget->setColumnCount(6);
customHeader = new CustomHeader(Qt::Horizontal,ui->tableWidget);
//QObject::connect(customHeader,SIGNAL(sectionPressed(int)),this , SLOT(UdateAllRowCheckBox(int)));
//this signal slot is used to check other checkboxes
qDebug()<<connect(customHeader , SIGNAL(clickCheckBox_SG(bool)) , this , SLOT(UdateAllRowCheckBox(bool)));
// UdateAllRowCheckBox(0);
ui->tableWidget->setHorizontalHeader(customHeader);
ui->tableWidget->setHorizontalHeaderLabels(list);
}
To copy to clipboard, switch view to plain text mode
Bookmarks