歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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

Copyright © Linux教程網 All Rights Reserved