PDA

View Full Version : styling Checkbox in header of Qtablewidget



virendra yadav
29th March 2021, 13:51
Hi,
I have few issues in my project. First thing first , I have inserted check box in Qtablewidget by flowing this link "https://wiki.qt.io/Technical_FAQ#How_can_I_insert_a_checkbox_into_the _header_of_my_view.3F" also successfully implement the process of selecting other checkboxes by checking the header checkbox.
now problem :

1:- I need to color the border of the check box to black color
2:- making check mark appear or disappear on other checkbox using header check box has bug that it is implemented by clicking anywhere on horizontal header which certainly not requirement.
3:-Also check mark appear or disappear when mouse cursor is removed from header's check box after clicking on check box . please follow lost two image to get idea what i am saying.

13617
1361813619




#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,logicalInde x);
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::Backg round,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(sectionPresse d(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);
}