1. 程式人生 > 其它 >QTableView 自定義表頭,新增複選框、下拉框

QTableView 自定義表頭,新增複選框、下拉框

技術標籤:qtqt5

主要作用:自定義表頭,新增一個複選框,一個下拉選項框;用於表格選項的選擇和篩選功能;

效果圖:

主要程式碼說明:

void CustomHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    QHeaderView::paintSection(painter,rect,logicalIndex);

    //logicalIndex 當前第幾列,也可以自定義顯示其他控制元件;
    if(logicalIndex == 0)
    {
        QRect tmp;
        tmp.setSize(QSize(20,20));
        tmp.moveCenter(rect.center());
        m_checkbox->setGeometry(tmp);
    }
    else if(logicalIndex ==2)
    {
        m_combox->setGeometry(rect);
    }
}

自定義表頭控制元件,繼承QHeaderView,主要通過paintSection自定義複選框和下拉框顯示;

程式碼下載地址:https://download.csdn.net/download/gz9456/13985527

以上;

原創,轉載請說明;