加载中…
个人资料
一去二三里
一去二三里
  • 博客等级:
  • 博客积分:0
  • 博客访问:2,322,840
  • 关注人气:898
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

Qt之QTreeView(二)

(2014-12-23 18:01:45)
标签:

qt

qtreeview

qtableview

qabstractitemmodel

qstyleditemdelegate

分类: Qt
    继上一章内容,讲解委托的处理,这里使用QStyledItemDelegate来实现!主要实现鼠标滑过节点及选中样式、QTreeView中绘制图标、按钮,并添加响应等!
    如下:
Qt之QTreeView(二)


#include "ItemDelegate.h"
#include 
#include 
#include 
#include 
#include 

ItemDelegate::ItemDelegate(QObject * parent)
    : QStyledItemDelegate(parent),
      m_treeView(NULL),
      m_pixmapHeight(9),
      m_pixmapWidth(9)
{
    m_expandIconName = QCoreApplication::applicationDirPath() + "/Resources/Images/arrowBottom.png";
    m_collapseIconName = QCoreApplication::applicationDirPath() + "/Resources/Images/arrowTop.png";
}

void ItemDelegate::setIconSize(QSize size)
{
    m_pixmapHeight = size.height();
    m_pixmapWidth = size.width();
}

void ItemDelegate::setExpandIcon(QString expandIconName)
{
    m_expandIconName = expandIconName;
}

void ItemDelegate::setcollapseIcon(QString collapseIconName)
{
    m_collapseIconName = collapseIconName;
}

void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem  viewOption(option);
    if (viewOption.state & QStyle::State_HasFocus)
    {
        viewOption.state = viewOption.state ^ QStyle::State_HasFocus;
    }

    QStyledItemDelegate::paint(painter, viewOption, index);

    bool bExpanded = false;
    if (m_treeView != NULL)
    {
        const QAbstractItemModel *model = index.model();
        if (!model->hasChildren(index))
        {
            return;
        }
        bExpanded = m_treeView->isExpanded(index);
    }

    int height = (viewOption.rect.height() - m_pixmapWidth) / 2;
    QPixmap pixmap = bExpanded ? QPixmap(m_expandIconName) : QPixmap(m_collapseIconName);
    QRect decorationRect = QRect(viewOption.rect.left() + viewOption.rect.width() - 30, viewOption.rect.top() + height, m_pixmapHeight, m_pixmapWidth);
    painter->drawPixmap(decorationRect, pixmap);
}

bool ItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    int height = (option.rect.height() - m_pixmapWidth) / 2;
    QRect decorationRect = QRect(option.rect.left() + option.rect.width() - 30, option.rect.top() + height, m_pixmapHeight, m_pixmapWidth);

    QMouseEvent *mouseEvent = static_cast(event);
    if (event->type() == QEvent::MouseButtonPress && decorationRect.contains(mouseEvent->pos()))
    {
        emit expanded(index);
    }

    if (event->type() == QEvent::MouseMove && decorationRect.contains(mouseEvent->pos()))
    {
        bool bExpanded = false;
        if (m_treeView != NULL)
        {
            const QAbstractItemModel *model = index.model();
            if (!model->hasChildren(index))
            {
                return false;
            }
            bExpanded = m_treeView->isExpanded(index);
        }
        QCursor cursor(Qt::PointingHandCursor);
        QApplication::setOverrideCursor(cursor);
        QString strText = bExpanded ? QStringLiteral("折叠") : QStringLiteral("展开");
        QToolTip::showText(mouseEvent->globalPos(), strText);
    }
    else
    {
        QCursor cursor(Qt::ArrowCursor);
        QApplication::setOverrideCursor(cursor);
    }

    return QStyledItemDelegate::editorEvent(event, model, option, index);
}

void ItemDelegate::setView(QTreeView *treeView)
{
    m_treeView = treeView;
}

    paint函数主要根据当前的节点展开/折叠状态来绘制按钮(三角形),展开为下三角,折叠为上三角。
    editorEvent函数主要处理按钮事件,并设置鼠标滑过按钮区域时的ToolTip信息。


注:
    技术在于交流、沟通,转载请注明出处并保持作品的完整性。

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有