欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Qt自定義表頭實現(xiàn)過濾功能的方法

 更新時間:2019年07月08日 10:23:09   作者:隨性者也  
這篇文章主要個給大家介紹了關(guān)于Qt自定義表頭實現(xiàn)過濾功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Qt具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

1. 寫在前面

過濾功能源自項目上交互優(yōu)化用戶體驗,在表頭添加過濾符號實現(xiàn)過濾,替換以往在表格上方占用一行過濾項進(jìn)行過濾。

2. 過濾提示

過濾提示就是三態(tài)圖標(biāo)(normal,hover,press)。這三種狀態(tài)的實現(xiàn)通過鼠標(biāo)移動事件和鼠標(biāo)點擊事件來實現(xiàn)。具體實現(xiàn)如下:

  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)大小進(jìn)行計算過濾提示的區(qū)域。只有當(dāng)鼠標(biāo)在過濾區(qū)域位置上方,hover和press才有效,否則仍然是normal狀態(tài)。過濾區(qū)域繪制的位置可以從外面獲取,也可以使用默認(rèn)位置。最后style()->drawItemPixmap進(jìn)行繪制。

調(diào)用基類paintSection方法前后調(diào)用QPainter::save()和QPainter::restore()是必要的。如果不調(diào)用,style()->drawItemPixmap是不會起作用的。

4)過濾提示點擊信號

點擊過濾提示會發(fā)出信號,連接此信號可以進(jìn)行過濾功能的實現(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。

4. 寫在最后

實現(xiàn)過濾表頭的原理如上所訴。具體完整的項目路徑參考如下地址:

https://github.com/zhugp125/QtDemo/tree/master/FilterHeaderView

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

您可能感興趣的文章:

相關(guān)文章

  • 判斷一個無向圖是否為連通圖的方法

    判斷一個無向圖是否為連通圖的方法

    今天小編就為大家分享一篇關(guān)于判斷一個無向圖是否為連通圖的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • C++中auto_ptr智能指針的用法詳解

    C++中auto_ptr智能指針的用法詳解

    這篇文章主要介紹了C++中auto_ptr智能指針的用法詳解的相關(guān)資料,需要的朋友可以參考下
    2016-07-07
  • Linux中rm命令使用以及C/C++代碼實現(xiàn)

    Linux中rm命令使用以及C/C++代碼實現(xiàn)

    m 是remove 的縮寫,Linux中 rm 命令的功能為刪除一個目錄中的一個或多個文件或目錄,它也可以將某個目錄及其下的所有文件及子目錄均刪除,這篇文章主要給大家介紹了關(guān)于Linux中rm命令使用以及C/C++代碼實現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • C++宏函數(shù)和內(nèi)聯(lián)函數(shù)的使用

    C++宏函數(shù)和內(nèi)聯(lián)函數(shù)的使用

    本文主要介紹了C++宏函數(shù)和內(nèi)聯(lián)函數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • C++語言數(shù)據(jù)結(jié)構(gòu) 串的基本操作實例代碼

    C++語言數(shù)據(jù)結(jié)構(gòu) 串的基本操作實例代碼

    這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu) 串的基本操作實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • java 中ArrayList與LinkedList性能比較

    java 中ArrayList與LinkedList性能比較

    這篇文章主要介紹了java 中ArrayList與LinkedList性能比較的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • C語言char s[]和char* s的區(qū)別

    C語言char s[]和char* s的區(qū)別

    本文主要介紹了C語言char s[]和char* s的區(qū)別,詳細(xì)講述了數(shù)組,指針的使用,具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • MFC中Radio Button的用法詳解

    MFC中Radio Button的用法詳解

    這篇文章主要介紹了MFC中Radio Button的用法,需要的朋友可以參考下
    2014-07-07
  • 深入理解C++移位運算符

    深入理解C++移位運算符

    下面小編就為大家?guī)硪黄钊肜斫釩++移位運算符。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • C語言數(shù)據(jù)結(jié)構(gòu)與算法之排序總結(jié)(二)

    C語言數(shù)據(jù)結(jié)構(gòu)與算法之排序總結(jié)(二)

    這篇文章住要介紹的是選擇類排序中的簡單、樹形和堆排序,歸并排序、分配類排序的基數(shù)排序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2021-12-12

最新評論