Customize QListWidgetItem

Study/Qt 프로그래밍2015.04.02 16:26

ListWidget 에 디자인 입히기  (배경색, 선색, 폰트 등...)

참고 : http://www.qtcentre.org/threads/27777-Customize-QListWidgetItem-how-to

사용중인 QWidget 클래스에 요렇게 넣으세요.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
 
#include "homeform.h"
#include "ui_homeform.h"
#include <QDebug>
#include <QApplication>
#include "listviewdelegate.h"
 
 
HomeForm::HomeForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomeForm)
{
 
    ListviewDelegate *m_listdelegate;
    m_listdelegate = new ListviewDelegate();
 
    ui->ListWideget1->setItemDelegate(new ListviewDelegate(ui->ListWideget1));
    QListWidgetItem *item1 = new QListWidgetItem();
    item1->setData(Qt::DisplayRole, "3,000W");
 
    QListWidgetItem *item2 = new QListWidgetItem();
    item2->setData(Qt::DisplayRole, "150V");
 
    QListWidgetItem *item3 = new QListWidgetItem();
    item3->setData(Qt::DisplayRole, "20A");
 
 
    ui->ListWideget1->setGeometry(118,438,150,104);
    ui->ListWideget1->insertItem(0,item1);
    ui->ListWideget1->insertItem(1,item2);
    ui->ListWideget1->insertItem(2,item3);
 
}
 
cs



ListViewDelegate.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
/*
* ListViewDelegate.h
*
* Created on : April 2,2015
* Author : darongyi77.tistory.com
*/
 
#ifndef LISTVIEWDELEGATE_H
#define LISTVIEWDELEGATE_H
 
#include <QPainter>
#include <QAbstractItemDelegate>
 
/*
 * To make delegate class, need to override couple of functions,
1) sizeHint(const QStyleOptionViewItem & option ,const QModelIndex & index) const
2) sizeHint return you the item width. Which can be either predefined
 or you can calculate it using the data elements.
3) paint(QPainter *painter, const QStyleOptionViewItem &option,const QModelIndex &index) const
*/
 
 
class ListviewDelegate : public QAbstractItemDelegate
{
public:
 
    enum datarole { HeaderRole = Qt::UserRole + 100, SubheaderRole};
 
    ListviewDelegate(QObject *parent = 0);
 
 
    // Paint Listview
    void paint(QPainter *painter,
               const QStyleOptionViewItem &option,
               const QModelIndex &index) const;
 
    // Set Listview Size
    QSize sizeHint(const QStyleOptionViewItem &option,
                   const QModelIndex &index) const;
 
    ~ListviewDelegate();
};
 
#endif // LISTVIEWDELEGATE_H
 
 
cs



ListViewDelegate.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
 
#include "listviewdelegate.h"
 
/*
 * ListviewDelegate.cpp
 *
 *  Created on: April 2, 2015
 *  Author: http://darongyi77.tistory.com
 */
 
//QSize ListviewDelegate::iconSize = QSize(60,60);
//int ListviewDelegate::padding =5;
 
ListviewDelegate::ListviewDelegate(QObject *parent)
{
 
}
 
 
//QStyledItemDelegate
//display and editing facilities for data items from a model.
 
void ListviewDelegate::paint(QPainter *painter,
                             const QStyleOptionViewItem &option,
                             const QModelIndex &index) const
{
    QRect r = option.rect;
 
    //Color: #C4C4C4
    QPen linePen(QColor::fromRgb(211,211,211), 1, Qt::SolidLine);
 
    //Color: #005A83
    QPen lineMarkedPen(QColor::fromRgb(0,90,131), 1, Qt::SolidLine);
 
    //Color: #333
    QPen fontPen(QColor::fromRgb(51,51,51), 1, Qt::SolidLine);
 
    //Color: #fff
    QPen fontMarkedPen(Qt::white, 1, Qt::SolidLine);
 
    if(option.state & QStyle::State_Selected)
    {
      QLinearGradient gradientSelected(r.left(),r.top(),r.left(),r.height()+r.top());
      gradientSelected.setColorAt(0.0, QColor::fromRgb(119,213,247));
      gradientSelected.setColorAt(0.9, QColor::fromRgb(27,134,183));
      gradientSelected.setColorAt(1.0, QColor::fromRgb(0,120,174));
      painter->setBrush(gradientSelected);
      painter->drawRect(r);
 
      //BORDER
      painter->setPen(lineMarkedPen);
      painter->drawLine(r.topLeft(),r.topRight());
      painter->drawLine(r.topRight(),r.bottomRight());
      painter->drawLine(r.bottomLeft(),r.bottomRight());
      painter->drawLine(r.topLeft(),r.bottomLeft());
 
      painter->setPen(fontMarkedPen);
 
      } else {
            //BACKGROUND
            //ALTERNATING COLORS
            painter->setBrush( (index.row() % 2) ? Qt::white : QColor(252,252,252) );
            painter->drawRect(r);
 
            //BORDER
            painter->setPen(linePen);
            painter->drawLine(r.topLeft(),r.topRight());
            painter->drawLine(r.topRight(),r.bottomRight());
            painter->drawLine(r.bottomLeft(),r.bottomRight());
            painter->drawLine(r.topLeft(),r.bottomLeft());
 
            painter->setPen(fontPen);
        }
 
        //GET TITLE, DESCRIPTION AND ICON
        QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
        QString title = index.data(Qt::DisplayRole).toString();
      //  QString description = index.data(Qt::UserRole + 1).toString();
 
        int imageSpace = 10;
        if (!ic.isNull()) {
            //ICON
            r = option.rect.adjusted(510-10-10);
            ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
            //imageSpace = 55;
 
        }
 
        //TITLE
        r = option.rect.adjusted(imageSpace, 0-10-10);
        painter->setFont( QFont( "Lucida Grande",11, QFont::Bold ) );
        painter->drawText(r.left(), r.top(), r.width(), r.height(), 
Qt::AlignBottom|Qt::AlignLeft, title, &r);
 
        //DESCRIPTION
//        r = option.rect.adjusted(imageSpace, 30, -10, 0);
//        painter->setFont( QFont( "Lucida Grande", 5, QFont::Bold ) );
//        painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignLeft, description, &r);
}
 
 
//allocate each item size in listview
QSize ListviewDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
   // whatever..
  return QSize(20034); // very dumb value
}
 
 
ListviewDelegate::~ListviewDelegate()
{
 
}
 
cs






'Study > Qt 프로그래밍' 카테고리의 다른 글

QList< QPair<QString, QString>> example code  (0) 2018.02.12
QDialog 의 SDI 와 MDI 개념  (0) 2016.08.23
HierarchicalHeaderView  (0) 2016.01.28
Customize QListWidgetItem  (0) 2015.04.02

작성자

Posted by 비타오백

관련 글

댓글 영역

티스토리 툴바