通过重写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();
本文介绍如何在Qt中通过重写QHeaderView类来实现在表头添加复选框的功能,提供了一段示例代码,展示了如何创建自定义的HeaderView类并将其应用于QTableView。


被折叠的 条评论
为什么被折叠?



