通过重写QHeaderVIew类,实现表头添加复选框;代码如下

class HeaderView : public QHeaderView
{
Q_OBJECT

public:
explicit HeaderView(Qt::Orientation orientation, QWidget* parent = 0)
: QHeaderView(orientation, parent)
{
cbx_header= new QCheckBox(this);
cbx_header->setVisible(true);
}

protected:
void updateGeometries()
{
cbx_header->move(sectionPosition(0) + 3, 6);
}

private:
QCheckBox *cbx_header;
};

重写QHeaderView之后,使用重写的类,设置tableview即可,代码如下

QTableView *tmp_tableview;
tmp_tableview = new QTableView();
tmp_tableview->setRowCount(3);
tmp_tableview->setColumnCount(4);

HeaderView* header = new HeaderView(Qt::Horizontal, tmp_tableview);
tmp_tableview->setHorizontalHeader(header);
tmp_tableview->show();