├── README.md
├── ZListView.pro
├── ZListView.pro.user
├── icons.qrc
├── images
├── Screenshot1.png
└── Screenshot2.png
├── zlistwidget.cpp
├── zlistwidget.h
├── zlistwidget.pri
├── zlistwidgetplugin.cpp
└── zlistwidgetplugin.h
/README.md:
--------------------------------------------------------------------------------
1 | # zlistwidget
2 | QT控件,继承自QListWidget。本项目可以编译出插件,直接在QTCreator的Design中使用。
3 | 主要的源码文件只有zlistwidget.h/zlistwidget.cpp,其它文件是插件项目文件。
4 | ```cpp
5 | void MainWindow::on_button3_clicked()
6 | {
7 | ZListWidget * w = new ZListWidget(this);
8 | w->setGeometry(QRect(100, 100, 200, 300));
9 |
10 | QString text;
11 | for (int i = 0; i < 5; i++) {
12 | text = QString("New Item %1").arg(i);
13 | QListWidgetItem * pItem = new QListWidgetItem(text, w);
14 | QIcon icon;
15 | if (i == 0) {
16 | icon.addFile(":/images/images/flash.png", QSize(16, 16));
17 | icon.addFile(":/images/images/favorite.png", QSize(16, 16), QIcon::Selected, QIcon::Off);
18 | pItem->setIcon(icon);//QIcon(":/images/images/favorite.png"));
19 | }
20 | else if (i == 1) {
21 | pItem->setIcon(QIcon(":/images/images/flash.png"));
22 | }
23 | else if (i == 2) {
24 | pItem->setIcon(QIcon(":/images/images/history.png"));
25 | }
26 | }
27 | w->setItemHeight(40);
28 | w->show();
29 | }
30 | ```
31 | 界面截图:
32 | 
33 |
34 | 下面是该控件在一个项目中的使用案例:
35 | 
36 |
--------------------------------------------------------------------------------
/ZListView.pro:
--------------------------------------------------------------------------------
1 | CONFIG += plugin debug_and_release
2 | TARGET = $$qtLibraryTarget(zlistwidgetplugin)
3 | TEMPLATE = lib
4 |
5 | HEADERS = zlistwidgetplugin.h
6 | SOURCES = zlistwidgetplugin.cpp
7 | RESOURCES = icons.qrc
8 | LIBS += -L.
9 |
10 | greaterThan(QT_MAJOR_VERSION, 4) {
11 | QT += designer
12 | } else {
13 | CONFIG += designer
14 | }
15 |
16 | target.path = $$[QT_INSTALL_PLUGINS]/designer
17 | INSTALLS += target
18 |
19 | include(zlistwidget.pri)
20 |
--------------------------------------------------------------------------------
/ZListView.pro.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | EnvironmentId
7 | {cbb8690d-e1c1-4cd3-bf28-fd95ea77c02a}
8 |
9 |
10 | ProjectExplorer.Project.ActiveTarget
11 | 0
12 |
13 |
14 | ProjectExplorer.Project.EditorSettings
15 |
16 | true
17 | false
18 | true
19 |
20 | Cpp
21 |
22 | CppGlobal
23 |
24 |
25 |
26 | QmlJS
27 |
28 | QmlJSGlobal
29 |
30 |
31 | 2
32 | UTF-8
33 | false
34 | 4
35 | false
36 | 80
37 | true
38 | true
39 | 1
40 | true
41 | false
42 | 0
43 | true
44 | true
45 | 0
46 | 8
47 | true
48 | 1
49 | true
50 | true
51 | true
52 | false
53 |
54 |
55 |
56 | ProjectExplorer.Project.PluginSettings
57 |
58 |
59 |
60 | ProjectExplorer.Project.Target.0
61 |
62 | Desktop Qt 5.7.0 GCC 64bit
63 | Desktop Qt 5.7.0 GCC 64bit
64 | qt.57.gcc_64_kit
65 | 1
66 | 0
67 | 0
68 |
69 | /home/bruce/build/ZListView-Debug
70 |
71 |
72 | true
73 | qmake
74 |
75 | QtProjectManager.QMakeBuildStep
76 | true
77 |
78 | false
79 | false
80 | false
81 |
82 |
83 | true
84 | Make
85 |
86 | Qt4ProjectManager.MakeStep
87 |
88 | -w
89 | -r
90 |
91 | false
92 |
93 |
94 |
95 | 2
96 | Build
97 |
98 | ProjectExplorer.BuildSteps.Build
99 |
100 |
101 |
102 | true
103 | Make
104 |
105 | Qt4ProjectManager.MakeStep
106 |
107 | -w
108 | -r
109 |
110 | true
111 | clean
112 |
113 |
114 | 1
115 | Clean
116 |
117 | ProjectExplorer.BuildSteps.Clean
118 |
119 | 2
120 | false
121 |
122 | Debug
123 |
124 | Qt4ProjectManager.Qt4BuildConfiguration
125 | 2
126 | true
127 |
128 |
129 | /home/bruce/build/ZListView-Release
130 |
131 |
132 | true
133 | qmake
134 |
135 | QtProjectManager.QMakeBuildStep
136 | false
137 |
138 | false
139 | false
140 | false
141 |
142 |
143 | true
144 | Make
145 |
146 | Qt4ProjectManager.MakeStep
147 |
148 | -w
149 | -r
150 |
151 | false
152 |
153 |
154 |
155 | 2
156 | Build
157 |
158 | ProjectExplorer.BuildSteps.Build
159 |
160 |
161 |
162 | true
163 | Make
164 |
165 | Qt4ProjectManager.MakeStep
166 |
167 | -w
168 | -r
169 |
170 | true
171 | clean
172 |
173 |
174 | 1
175 | Clean
176 |
177 | ProjectExplorer.BuildSteps.Clean
178 |
179 | 2
180 | false
181 |
182 | Release
183 |
184 | Qt4ProjectManager.Qt4BuildConfiguration
185 | 0
186 | true
187 |
188 |
189 | /home/bruce/build/ZListView-Profile
190 |
191 |
192 | true
193 | qmake
194 |
195 | QtProjectManager.QMakeBuildStep
196 | true
197 |
198 | false
199 | true
200 | false
201 |
202 |
203 | true
204 | Make
205 |
206 | Qt4ProjectManager.MakeStep
207 |
208 | -w
209 | -r
210 |
211 | false
212 |
213 |
214 |
215 | 2
216 | Build
217 |
218 | ProjectExplorer.BuildSteps.Build
219 |
220 |
221 |
222 | true
223 | Make
224 |
225 | Qt4ProjectManager.MakeStep
226 |
227 | -w
228 | -r
229 |
230 | true
231 | clean
232 |
233 |
234 | 1
235 | Clean
236 |
237 | ProjectExplorer.BuildSteps.Clean
238 |
239 | 2
240 | false
241 |
242 | Profile
243 |
244 | Qt4ProjectManager.Qt4BuildConfiguration
245 | 0
246 | true
247 |
248 | 3
249 |
250 |
251 | 0
252 | Deploy
253 |
254 | ProjectExplorer.BuildSteps.Deploy
255 |
256 | 1
257 | Deploy locally
258 |
259 | ProjectExplorer.DefaultDeployConfiguration
260 |
261 | 1
262 |
263 |
264 | false
265 | false
266 | 1000
267 |
268 | true
269 |
270 | false
271 | false
272 | false
273 | false
274 | true
275 | 0.01
276 | 10
277 | true
278 | 1
279 | 25
280 |
281 | 1
282 | true
283 | false
284 | true
285 | valgrind
286 |
287 | 0
288 | 1
289 | 2
290 | 3
291 | 4
292 | 5
293 | 6
294 | 7
295 | 8
296 | 9
297 | 10
298 | 11
299 | 12
300 | 13
301 | 14
302 |
303 | 2
304 |
305 |
306 |
307 | %{buildDir}
308 | Custom Executable
309 |
310 | ProjectExplorer.CustomExecutableRunConfiguration
311 | 3768
312 | false
313 | true
314 | false
315 | false
316 | true
317 |
318 | 1
319 |
320 |
321 |
322 | ProjectExplorer.Project.TargetCount
323 | 1
324 |
325 |
326 | ProjectExplorer.Project.Updater.FileVersion
327 | 18
328 |
329 |
330 | Version
331 | 18
332 |
333 |
334 |
--------------------------------------------------------------------------------
/icons.qrc:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/images/Screenshot1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brucezhao/zlistwidget/7aa837e34e8006eefdf814e5c16991f3f111c496/images/Screenshot1.png
--------------------------------------------------------------------------------
/images/Screenshot2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brucezhao/zlistwidget/7aa837e34e8006eefdf814e5c16991f3f111c496/images/Screenshot2.png
--------------------------------------------------------------------------------
/zlistwidget.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include "zlistwidget.h"
5 |
6 | //文字开始位置
7 | const int C_TEXT_LEFT_MARGIN = 50;
8 | //指示条宽度
9 | const int C_INDICATOR_WIDTH = 5;
10 |
11 | ZListWidget::ZListWidget(QWidget *parent, int paintType) :
12 | QListWidget(parent), m_paintType(paintType)
13 | {
14 | this->setFrameShape(NoFrame);
15 | this->setSpacing(0);
16 | connect(this, &ZListWidget::itemSelectionChanged, this, &ZListWidget::onItemSelectionChanged);
17 |
18 | m_itemHeight = 35;
19 |
20 | m_color = QColor(198, 198, 198);
21 | m_backgroundColor = QColor(76, 83, 89);
22 | m_selectionColor = QColor(99, 176, 72);
23 | m_selectionBackgroundColor = QColor(76, 83, 89);
24 | m_gridLineColor = QColor(87, 95, 103);
25 | this->setIconSize(QSize(16, 16));
26 | }
27 |
28 | int ZListWidget::itemHeight()
29 | {
30 | return m_itemHeight;
31 | }
32 |
33 | void ZListWidget::setItemHeight(int height)
34 | {
35 | m_itemHeight = height;
36 |
37 | QListWidgetItem * pItem = NULL;
38 | QSize size;
39 | for (int i = 0; i < this->count(); i++) {
40 | pItem = this->item(i);
41 | if (i == 0) {
42 | size = pItem->sizeHint();
43 | size.setHeight(m_itemHeight);
44 | }
45 | pItem->setSizeHint(size);
46 | }
47 | }
48 |
49 | const QColor &ZListWidget::color() const
50 | {
51 | return m_color;
52 | }
53 |
54 | void ZListWidget::setColor(const QColor &color)
55 | {
56 | if (color != m_color) {
57 | m_color = color;
58 | this->update();
59 | }
60 | }
61 |
62 | const QColor &ZListWidget::backgroundColor()
63 | {
64 | return m_backgroundColor;
65 | }
66 |
67 | void ZListWidget::setBackgroundColor(const QColor &color)
68 | {
69 | if (color != m_backgroundColor) {
70 | m_backgroundColor = color;
71 | this->update();
72 | }
73 | }
74 |
75 | const QColor &ZListWidget::gridLineColor() const
76 | {
77 | return m_gridLineColor;
78 | }
79 |
80 | void ZListWidget::setGridLineColor(const QColor &color)
81 | {
82 | if (color != m_gridLineColor) {
83 | m_gridLineColor = color;
84 | this->update();
85 | }
86 | }
87 |
88 | const QColor &ZListWidget::selectionColor() const
89 | {
90 | return m_selectionColor;
91 | }
92 |
93 | void ZListWidget::setSelectionColor(const QColor &color)
94 | {
95 | if (color != m_selectionColor) {
96 | m_selectionColor = color;
97 | this->update();
98 | }
99 | }
100 |
101 | const QColor &ZListWidget::selectionBackgroundColor() const
102 | {
103 | return m_selectionBackgroundColor;
104 | }
105 |
106 | void ZListWidget::setSelectionBackgroundColor(const QColor &color)
107 | {
108 | if (color != m_selectionBackgroundColor) {
109 | m_selectionBackgroundColor = color;
110 | this->update();
111 | }
112 | }
113 |
114 | void ZListWidget::showEvent(QShowEvent *event)
115 | {
116 | if (this->count() > 0) {
117 | QListWidgetItem * pItem = this->item(0);
118 | if (pItem->sizeHint().height() != m_itemHeight) {
119 | setItemHeight(m_itemHeight);
120 | }
121 | }
122 |
123 | QListWidget::showEvent(event);
124 | }
125 |
126 |
127 | void ZListWidget::onItemSelectionChanged()
128 | {
129 | //当spacing大于0时,如果不repaint,spacing区域不会更新,导致显示不正常
130 | if (this->spacing() > 0)
131 | this->viewport()->repaint();
132 | }
133 |
134 |
135 | void ZListWidget::paintEvent(QPaintEvent * event)
136 | {
137 | if (m_paintType != 0) {
138 | QListWidget::paintEvent(event);
139 | return;
140 | }
141 |
142 | QPainter painter(this->viewport());
143 | QRect paintRect = this->rect();
144 | QListWidgetItem * pItem = NULL;
145 | QSize size(paintRect.width(), m_itemHeight);
146 | QPoint point;
147 |
148 | //绘制背景色
149 | painter.fillRect(paintRect.x(), paintRect.y(), paintRect.width(), paintRect.height(), m_backgroundColor);
150 |
151 | //设置图标区域
152 | int iconX = (C_TEXT_LEFT_MARGIN - this->iconSize().width() - C_INDICATOR_WIDTH) / 2 + C_INDICATOR_WIDTH;
153 | int iconY = 0;
154 | int iSpacing = this->spacing();
155 | int y = iSpacing;
156 |
157 | point.setX(C_TEXT_LEFT_MARGIN);
158 |
159 | //确定要显示的item范围
160 | int iFrom = this->indexAt(QPoint(paintRect.x() + iSpacing, paintRect.y() + iSpacing)).row();
161 | int iTo = this->indexAt(QPoint(paintRect.x() + iSpacing, paintRect.height() - iSpacing)).row();
162 | iTo = iTo == -1 ? this->count()-1 : iTo;
163 | iTo = iTo < iFrom ? iFrom : iTo;
164 | QFont font = this->font();
165 |
166 | for (int i = iFrom; i <= iTo ; i++) {
167 | pItem = this->item(i);
168 | //画背景
169 | if (pItem->isSelected()) {
170 | painter.fillRect(0, y, size.width(), size.height(), m_selectionBackgroundColor);
171 | painter.fillRect(0, y, C_INDICATOR_WIDTH, size.height(), m_selectionColor);
172 | painter.setPen(m_selectionColor);
173 | font.setBold(true);
174 | }
175 | else {
176 | painter.fillRect(0, y, size.width(), size.height(), m_backgroundColor);
177 | painter.setPen(m_color);
178 | font.setBold(false);
179 | }
180 |
181 | //画图标
182 | QIcon icon = pItem->icon();
183 | if (!icon.isNull()) {
184 | iconY = (m_itemHeight - this->iconSize().height()) / 2 + y;
185 | QRect iconRect(QPoint(iconX, iconY), this->iconSize());
186 | if (pItem->isSelected()) {
187 | icon.paint(&painter, iconRect, Qt::AlignCenter, QIcon::Selected, QIcon::Off);
188 | }
189 | else {
190 | icon.paint(&painter, iconRect);
191 | }
192 | }
193 |
194 | //写字
195 | point.setY(y);
196 | painter.setFont(font);
197 | QRect rectItem(point, size);
198 | painter.drawText(rectItem, Qt::AlignLeft | Qt::AlignVCenter, pItem->text(), NULL);
199 |
200 | //画分隔线
201 | if (i > 0) {
202 | painter.setPen(m_gridLineColor);
203 | painter.drawLine(0, y - this->spacing(), paintRect.width(), y - this->spacing());
204 | }
205 |
206 | y += size.height() + this->spacing() * 2;
207 | }
208 |
209 | painter.end();
210 | }
211 |
--------------------------------------------------------------------------------
/zlistwidget.h:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | // 赵亦平 2016.12.08
3 | // ZListWidget 1.0
4 | // 继承自QListWidget,可以自定义item的高度,可以设置配色(无须通过QSS来实现);
5 | // 可以集成到QCreator中,在Design中使用;
6 | //------------------------------------------------------------------------------
7 | #ifndef ZLISTWIDGET_H
8 | #define ZLISTWIDGET_H
9 |
10 | #include
11 | #include
12 | #include
13 |
14 | class ZListWidget : public QListWidget
15 | {
16 | Q_OBJECT
17 |
18 | Q_PROPERTY(int itemHeight READ itemHeight WRITE setItemHeight)
19 | Q_PROPERTY(QColor color READ color WRITE setColor)
20 | Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
21 | Q_PROPERTY(QColor gridLineColor READ gridLineColor WRITE setGridLineColor)
22 | Q_PROPERTY(QColor selectionColor READ selectionColor WRITE setSelectionColor)
23 | Q_PROPERTY(QColor selectionBackgroundColor READ selectionBackgroundColor WRITE setSelectionBackgroundColor)
24 | public:
25 | ZListWidget(QWidget *parent = 0, int paintType = 0);
26 |
27 | //设置每个item的高度
28 | int itemHeight();
29 | void setItemHeight(int height);
30 | //设置颜色
31 | const QColor & color() const;
32 | void setColor(const QColor & color);
33 | const QColor & backgroundColor();
34 | void setBackgroundColor(const QColor & color);
35 | const QColor & gridLineColor() const;
36 | void setGridLineColor(const QColor & color);
37 | const QColor & selectionColor() const;
38 | void setSelectionColor(const QColor & color);
39 | const QColor & selectionBackgroundColor() const;
40 | void setSelectionBackgroundColor(const QColor & color);
41 | private:
42 | int m_paintType;
43 | int m_itemHeight;
44 | QColor m_color;
45 | QColor m_backgroundColor;
46 | QColor m_gridLineColor;
47 | QColor m_selectionColor;
48 | QColor m_selectionBackgroundColor;
49 |
50 | protected:
51 | void paintEvent(QPaintEvent *event);
52 | void showEvent(QShowEvent * event);
53 |
54 | void onItemSelectionChanged();
55 | };
56 |
57 | #endif
58 |
--------------------------------------------------------------------------------
/zlistwidget.pri:
--------------------------------------------------------------------------------
1 | HEADERS += zlistwidget.h
2 | SOURCES += zlistwidget.cpp
3 |
--------------------------------------------------------------------------------
/zlistwidgetplugin.cpp:
--------------------------------------------------------------------------------
1 | #include "zlistwidget.h"
2 | #include "zlistwidgetplugin.h"
3 |
4 | #include
5 |
6 | ZListWidgetPlugin::ZListWidgetPlugin(QObject *parent)
7 | : QObject(parent)
8 | {
9 | m_initialized = false;
10 | }
11 |
12 | void ZListWidgetPlugin::initialize(QDesignerFormEditorInterface * /* core */)
13 | {
14 | if (m_initialized)
15 | return;
16 |
17 | // Add extension registrations, etc. here
18 |
19 | m_initialized = true;
20 | }
21 |
22 | bool ZListWidgetPlugin::isInitialized() const
23 | {
24 | return m_initialized;
25 | }
26 |
27 | QWidget *ZListWidgetPlugin::createWidget(QWidget *parent)
28 | {
29 | return new ZListWidget(parent);
30 | }
31 |
32 | QString ZListWidgetPlugin::name() const
33 | {
34 | return QLatin1String("ZListWidget");
35 | }
36 |
37 | QString ZListWidgetPlugin::group() const
38 | {
39 | return QLatin1String("");
40 | }
41 |
42 | QIcon ZListWidgetPlugin::icon() const
43 | {
44 | return QIcon();
45 | }
46 |
47 | QString ZListWidgetPlugin::toolTip() const
48 | {
49 | return QLatin1String("");
50 | }
51 |
52 | QString ZListWidgetPlugin::whatsThis() const
53 | {
54 | return QLatin1String("");
55 | }
56 |
57 | bool ZListWidgetPlugin::isContainer() const
58 | {
59 | return false;
60 | }
61 |
62 | QString ZListWidgetPlugin::domXml() const
63 | {
64 | return QLatin1String("\n\n");
65 | }
66 |
67 | QString ZListWidgetPlugin::includeFile() const
68 | {
69 | return QLatin1String("zlistwidget.h");
70 | }
71 | #if QT_VERSION < 0x050000
72 | Q_EXPORT_PLUGIN2(zlistwidgetplugin, ZListWidgetPlugin)
73 | #endif // QT_VERSION < 0x050000
74 |
--------------------------------------------------------------------------------
/zlistwidgetplugin.h:
--------------------------------------------------------------------------------
1 | #ifndef ZLISTWIDGETPLUGIN_H
2 | #define ZLISTWIDGETPLUGIN_H
3 |
4 | #include
5 |
6 | class ZListWidgetPlugin : public QObject, public QDesignerCustomWidgetInterface
7 | {
8 | Q_OBJECT
9 | Q_INTERFACES(QDesignerCustomWidgetInterface)
10 | #if QT_VERSION >= 0x050000
11 | Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface")
12 | #endif // QT_VERSION >= 0x050000
13 |
14 | public:
15 | ZListWidgetPlugin(QObject *parent = 0);
16 |
17 | bool isContainer() const;
18 | bool isInitialized() const;
19 | QIcon icon() const;
20 | QString domXml() const;
21 | QString group() const;
22 | QString includeFile() const;
23 | QString name() const;
24 | QString toolTip() const;
25 | QString whatsThis() const;
26 | QWidget *createWidget(QWidget *parent);
27 | void initialize(QDesignerFormEditorInterface *core);
28 |
29 | private:
30 | bool m_initialized;
31 | };
32 |
33 | #endif
34 |
--------------------------------------------------------------------------------