QTableView表頭添加CheckBox

通過重寫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();

 

發佈了135 篇原創文章 · 獲贊 59 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章