Qt中怎么通過自定義表頭實現(xiàn)過濾功能,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
1)hover狀態(tài)在鼠標(biāo)移動事件中實現(xiàn)
void CFilterHeaderView::mouseMoveEvent(QMouseEvent *e){ m_hover = logicalIndexAt(e->pos()); if (m_hover != -1) updateSection(m_hover); QHeaderView::mouseMoveEvent(e);}bool CFilterHeaderView::event(QEvent *e){ switch(e->type()) { case QEvent::Leave: case QEvent::HoverLeave: if (m_hover != -1) updateSection(m_hover); m_hover = -1; break; default: break; } return QHeaderView::event(e);}
如果懸浮在某一列上,hover值等于該列的index,否則等于-1。如果hover值不等于-1,則刷新該列(updateSection)。
mouseMoveEvent中檢測鼠標(biāo)懸浮在那個表格列上。event函數(shù)中監(jiān)聽Leave和HoverLeave事件。
2)press狀態(tài)在鼠標(biāo)點擊事件中實現(xiàn)
void CFilterHeaderView::mousePressEvent(QMouseEvent *e){ m_press = logicalIndexAt(e->pos()); if (m_press != -1) updateSection(m_press); QHeaderView::mousePressEvent(e);}void CFilterHeaderView::mouseReleaseEvent(QMouseEvent *e){ m_press = -1; QHeaderView::mouseReleaseEvent(e);}
press的實現(xiàn)較為簡單,鼠標(biāo)點擊更新press,鼠標(biāo)釋放press置為-1。
3)過濾提示的實現(xiàn)。
過濾提示在paintSection函數(shù)中實現(xiàn),首先是調(diào)用基類paintSection實現(xiàn)表頭的繪制,然后是檢測有沒有定義過濾角色。如果有定義過濾角色,則根據(jù)三態(tài)選擇對應(yīng)的圖標(biāo),繪制位置默認(rèn)水平靠右垂直居住,也可以自己指定位置。最后是繪制過濾提示。具體實現(xiàn)如下:
void CFilterHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const{ painter->save(); QHeaderView::paintSection(painter, rect, logicalIndex); painter->restore(); QVariant filterVar = model()->headerData(logicalIndex, orientation(), FilterRole); if (filterVar.isValid() && filterVar.toBool()) { QPixmap pix = m_norFilterPix; bool b_contain = getFilterRect(rect).contains(cursor().pos()); if (logicalIndex == m_hover && b_contain) { pix = m_hovFilterPix; } if (logicalIndex == m_press && b_contain) { pix = m_preFilterPix; } int align = Qt::AlignRight | Qt::AlignVCenter; QVariant alignVar = model()->headerData(logicalIndex, orientation(), FilterAlignmentRole); if (alignVar.isValid()) { align = alignVar.toInt(); } style()->drawItemPixmap(painter, rect, align, pix); }}
表格繪制的區(qū)域和過濾提示繪制的區(qū)域不一致,要根據(jù)過濾圖標(biāo)大小進行計算過濾提示的區(qū)域。只有當(dāng)鼠標(biāo)在過濾區(qū)域位置上方,hover和press才有效,否則仍然是normal狀態(tài)。過濾區(qū)域繪制的位置可以從外面獲取,也可以使用默認(rèn)位置。最后style()->drawItemPixmap進行繪制。
調(diào)用基類paintSection方法前后調(diào)用QPainter::save()和QPainter::restore()是必要的。如果不調(diào)用,style()->drawItemPixmap是不會起作用的。
4)過濾提示點擊信號
點擊過濾提示會發(fā)出信號,連接此信號可以進行過濾功能的實現(xiàn)。具體實現(xiàn)如下:
void CFilterHeaderView::mouseReleaseEvent(QMouseEvent *e){ if (e->button() == Qt::LeftButton) { int section = logicalIndexAt(e->pos()); QVariant filterVar = model()->headerData(section, orientation(), FilterRole); if (filterVar.isValid() && filterVar.toBool()) { QRect rect(sectionViewportPosition(section), 0, sectionSize(section), height()); if (getFilterRect(rect).contains(cursor().pos())) { emit filterClicked(section); } } } QHeaderView::mouseReleaseEvent(e);}
過濾信號發(fā)出的條件:1. 左鍵點擊,2. 定義了過濾功能,3. 鼠標(biāo)在過濾提示區(qū)域中
3. 使用過濾功能
使用過濾表頭的方法如下:
m_tableView = new QTableView(this); m_model = new QStandardItemModel(this); m_filterModel = new QSortFilterProxyModel(this); m_filterModel->setSourceModel(m_model); m_filterModel->setSortRole(Qt::ToolTipRole); m_tableView->setModel(m_filterModel); QHBoxLayout* mainLayout = new QHBoxLayout(this); mainLayout->setMargin(0); mainLayout->addWidget(m_tableView); setLayout(mainLayout); m_tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); m_tableView->setSelectionMode(QAbstractItemView::SingleSelection); m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows); m_tableView->verticalHeader()->hide(); CFilterHeaderView* pHeader = new CFilterHeaderView(this); connect(pHeader, &CFilterHeaderView::filterClicked, this, &Widget::onFilterClicked); m_tableView->setHorizontalHeader(pHeader);
使用過濾表頭和使用普通表頭沒有太大的差別。這里過濾功能有QSortFilterProxyModel實現(xiàn),水平表頭替換成自定義的CFilterHeaderView。
關(guān)于Qt中怎么通過自定義表頭實現(xiàn)過濾功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
文章名稱:Qt中怎么通過自定義表頭實現(xiàn)過濾功能-創(chuàng)新互聯(lián)
文章URL:http://vcdvsql.cn/article20/ddgdco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、App開發(fā)、服務(wù)器托管、云服務(wù)器、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容