Hello to all. How to display checkBox in TableWidget header? read its state, and if it is true (it’s checked), for example, output the contents of this particular column to a file.
1 answer
This can be achieved with QHeaderView , and the QTableWidget->setHorizontalHeader() method.
You can follow these steps:
Create a header class for your table, for example:
class MyTableHeader : public QHeaderView { Q_OBJECT public: MyHeaderView(QWidget* parent) : QHeaderView(Qt::Horizontal, parent) {} // ... }; This class will be used as a heading for your table:
MyHeaderView* header = new MyHeaderView(); tableWidget->setHorizontalHeader(header); In order for your header to appear as a checkbox, you need to redefine the QHeaderView::paintSection() method, and mark there where: and what type you want to display (hint: use the QStyleOptionButton class and also the QStyle::State_On and QStyle::State_Off properties QStyle::State_Off ).
To read states, add a private variable, for example, private: bool _isOn; , and each time a user clicks on the breaks of your checkbox, change the variable to the opposite. To do this, you need to re-define the class QHeaderView::mousePressEvent() . This variable must also be used in the paintSelection() method to draw a checkbox with the correct state.
In order to have some specific effect on the appearance of a tick, use the system of slots and signals, like this:
void MyTableHeader::mousePressEvent(QMouseEvent *event){ // ... if (/* произошло нажатие, и галочка включенна */) emit checkIsOn(); } Remember to connect the checkIsOn() signal to the slot where you output the contents to a file.
- Thank! I will try) - Disastricks