QT 列表类型控件(QListWidget等)的代理使用记录

最近项目中经常使用QListView,QListWidget等列表类型的空间来做。刚开始时不太了解,现在已经搞得比较熟悉了,不过为了防止如果时间久了不看的话会忘,所以暂时把它写到这里。同时也是为了以后方便查看,当然若有需要了解的人,希望他看了后也得到点收获。具体的代码如下:

#include "setupItemDelegate.h"

#define ICON_PATH  QString(qgetenv("PATH"))
//static const int textMargin = 3;
#define SPACE_FOR_ADD_TRIANGLE 20

SettingItem::SettingItemDelegate(QWidget *parent,bool enableTextScrolling)
{
    parentWidget = parent;
    isEnableTextScrolling = enableTextScrolling;
    pixmp.load(QString("%1/setup_ico_focus.png").arg(QString(PATH)));
}
void SettingItem::drawFocus( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect  , QPixmap& pixmap) const
{

}

void SettingItem::paint(QPainter *painter,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
    Q_ASSERT(index.isValid());

    QStyleOptionViewItem opt = option;
    //QPen prepen = painter->pen();
    //painter->save();

    // decoration
    QVariant value = index.data(Qt::DecorationRole);
    opt.state = QStyle::State_None;
    QPixmap pixmap = decoration(opt, value);
    QRect pixmapRect = (pixmap.isNull() ? QRect(0, 0, 0, 0)
                                        : QRect(QPoint(0, 0), option.decorationSize));

    // display
    QRect textRect;
    QString text = index.data(Qt::DisplayRole).toString();
    //force textRect = option.rect
    textRect = option.rect;

    QRect checkRect = QRect(0, 0, 0, 0);

    doLayout(option, &checkRect, &pixmapRect, &textRect, false);

    if (pixmapRect.isValid())
    {
        drawDecoration(painter, opt, pixmapRect, pixmap);
        if((option.state & QStyle::State_HasFocus) || (option.state & QStyle::State_Selected))
        {
            //painter->drawPixmap(pixmapRect, pixmp);
            QRect rect = option.rect;
            rect = rect.adjusted(0, 2, 0, -2);
            itemSelFocusGradient.setStart(rect.x(),rect.y());
            itemSelFocusGradient.setFinalStop(rect.x(), rect.y()+rect.height());

            itemSelFocusGradient.setColorAt(0,QColor(21,69,160));
            itemSelFocusGradient.setColorAt(0.3,QColor(33,51,82,50));
            itemSelFocusGradient.setColorAt(0.31,QColor(40,40,39,60));
            itemSelFocusGradient.setColorAt(0.7,QColor(38,38,39,60));
            itemSelFocusGradient.setColorAt(0.71,QColor(24,58,119,50));
            itemSelFocusGradient.setColorAt(1,QColor(0,55,170));
            QBrush brushsel = itemSelFocusGradient;
            painter->save();
            painter->setBrush(brushsel);
            painter->setPen(QPen(QColor(228,236,244),2,Qt::SolidLine));
            painter->drawRoundedRect(rect,10,10);
            painter->restore();
        }
    }


    if (!text.isEmpty())
    {
        //drawDisplay(painter, opt, textRect, text);
        if(option.state & QStyle::State_HasFocus)
        {
            painter->save();
            painter->setPen(QColor(247,148,33));
            painter->drawText(textRect, Qt::AlignVCenter,text);
            painter->restore();
        }
        /*else if(option.state & QStyle::State_Selected)
        {
            painter->save();
            painter->setPen(QColor(92,118,205));
            painter->drawText(textRect, Qt::AlignVCenter,text);
            painter->restore();
        }*/
        else
            painter->drawText(textRect, Qt::AlignVCenter,text);

    }
}
void SettingItem::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option,
                                       const QRect &rect, const QString &text) const
{
    QItemDelegate::drawDisplay(painter,option,rect,text);
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值