Results 1 to 1 of 1

Thread: styling Checkbox in header of Qtablewidget

  1. #1
    Join Date
    Jun 2019
    Posts
    1
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default styling Checkbox in header of Qtablewidget

    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.

    colorissue.PNG
    check box1.pngcheck box2.png

    Qt Code:
    1. #ifndef CUSTOMHEADER_H
    2. #define CUSTOMHEADER_H
    3. #include<QtGui>
    4. #include<QHeaderView>
    5. #include <QtCore>
    6. #include <QLibrary>
    7.  
    8.  
    9. class CustomHeader : public QHeaderView
    10. {
    11. Q_OBJECT
    12. public:
    13. CustomHeader(Qt::Orientation orientation , QWidget *parent =0) : QHeaderView(orientation,parent)
    14. {}
    15.  
    16. signals:
    17. void clickCheckBox_SG(bool);
    18. protected:
    19. void paintSection(QPainter *painter,const QRect &rect, int logicalIndex) const;
    20.  
    21. void mousePressEvent(QMouseEvent *event);
    22.  
    23.  
    24. private:
    25. bool isOn;
    26.  
    27.  
    28. };
    29.  
    30. //CPP file
    31.  
    32. void CustomHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
    33. {
    34. QColor color_b(255,255,0,0);
    35. painter->save();
    36. qDebug()<<logicalIndex<<__LINE__<<__FUNCTION__;
    37. QHeaderView::paintSection(painter,rect,logicalIndex);
    38. painter->restore();
    39. if(logicalIndex == 0)
    40. {
    41. option.rect = QRect(2,5,20,20);
    42. option.rect.moveLeft(0);
    43. option.rect.moveTop(5);
    44. // tries to change the border color of CheckBox
    45. option.palette = QPalette();
    46. option.palette.setColor(QPalette::ColorRole::Background,color_b);
    47.  
    48.  
    49. if(isOn)
    50. {
    51. option.state = QStyle::State_On;
    52. qDebug()<<isOn<<__LINE__<<__FUNCTION__;
    53. }
    54.  
    55. else
    56. {
    57. option.state = QStyle::State_Off;
    58. qDebug()<<isOn<<__LINE__<<__FUNCTION__;
    59. }
    60. this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox,&option,painter);
    61.  
    62. }
    63.  
    64. }
    65.  
    66. void CustomHeader::mousePressEvent(QMouseEvent *event)
    67. {
    68. if(isOn)
    69. {
    70. isOn = false;
    71. qDebug()<<isOn<<__LINE__<<__FUNCTION__;
    72. emit clickCheckBox_SG(isOn);
    73. }
    74. else
    75. {
    76. isOn = true;
    77. qDebug()<<isOn<<__LINE__<<__FUNCTION__;
    78. emit clickCheckBox_SG(isOn);
    79. }
    80. this->update();
    81. QHeaderView::mousePressEvent(event);
    82. }
    83.  
    84.  
    85. in mainwindow.cpp
    86.  
    87. void MainWindow::setTableWidgetGui()
    88. {
    89. QStringList list ;
    90. list << "" << "A" << "B" << "C" << "D" << "";
    91. ui->tableWidget->setColumnCount(6);
    92.  
    93. customHeader = new CustomHeader(Qt::Horizontal,ui->tableWidget);
    94. //QObject::connect(customHeader,SIGNAL(sectionPressed(int)),this , SLOT(UdateAllRowCheckBox(int)));
    95.  
    96. //this signal slot is used to check other checkboxes
    97. qDebug()<<connect(customHeader , SIGNAL(clickCheckBox_SG(bool)) , this , SLOT(UdateAllRowCheckBox(bool)));
    98. // UdateAllRowCheckBox(0);
    99. ui->tableWidget->setHorizontalHeader(customHeader);
    100. ui->tableWidget->setHorizontalHeaderLabels(list);
    101. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by virendra yadav; 29th March 2021 at 19:15.

Similar Threads

  1. Styling a QTableWidget
    By Imhotep in forum Qt Programming
    Replies: 0
    Last Post: 19th February 2018, 13:44
  2. Replies: 6
    Last Post: 27th October 2015, 13:50
  3. Replies: 5
    Last Post: 1st October 2013, 00:03
  4. Replies: 0
    Last Post: 12th January 2012, 07:41
  5. checkbox implementation in QTableWidget display issue
    By prajesh in forum Qt Programming
    Replies: 11
    Last Post: 26th February 2010, 10:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.