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);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwwwyd.html