效果圖
自定義一個(gè)item
新建一個(gè)qwidget對象
在qwidget內(nèi)添加layout
在layout內(nèi)添加要的控件
為qwidget設(shè)置layout
新建一個(gè)qlistwidgetitem并調(diào)整大小
為qlistwidgetitem設(shè)置qwidget
創(chuàng)建布局
首先我們創(chuàng)建一個(gè)最基本的布局, 只有一個(gè)listwidget和一個(gè)pushbutton
實(shí)現(xiàn)點(diǎn)擊button后在listwidget中添加數(shù)據(jù)
1
2
3
4
5
6
7
8
9
10
11
12
|
class windows(qmainwindow, ui_mainwindow): def __init__(self): super(windows, self).__init__() self.setupui(self) self.pushbutton.clicked.connect(self.deal) def deal(self): # 準(zhǔn)備實(shí)現(xiàn)的功能 pass app = qtwidgets.qapplication(sys.argv) windows = windows() windows.show() sys. exit (app.exec_()) |
確定布局
可以看出此布局總體是一個(gè)橫向布局(qhboxlayout), 再其右邊是一個(gè)縱向(qvboxlayout), 下面的布局又是一個(gè)橫向布局(qhboxlayout)
1
2
3
4
5
6
7
8
9
10
|
def get_item(): # 總widget wight = qwidget() # 布局 layout_main = qhboxlayout() # 總體橫向布局 layout_right = qvboxlayout() # 右邊的縱向布局 layout_right_down = qhboxlayout() # 右下的橫向布局 layout_right.addlayout(layout_right_down) # 右下布局填充到右邊布局中 layout_main.addlayout(layout_right) # 右邊布局填充入總布局 wight.setlayout(layout_main) # 為widget設(shè)置總布局 |
添加數(shù)據(jù)
1
2
3
4
5
6
7
8
|
{ "ship_name" : "胡德" , "ship_country" : "e國" , "ship_star" : "5" , "ship_index" : "1" , "ship_photo" : "1.png" , "ship_type" : "戰(zhàn)巡" } |
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
|
def get_item_wight(data): # 讀取屬性 ship_name = data[ 'ship_name' ] ship_photo = data[ 'ship_photo' ] ship_index = data[ 'ship_index' ] ship_type = data[ 'ship_type' ] ship_country = data[ 'ship_country' ] ship_star = data[ 'ship_star' ] # 總widget wight = qwidget() # 總體橫向布局 layout_main = qhboxlayout() map_l = qlabel() # 頭像顯示 map_l.setfixedsize(40, 25) maps = qpixmap(ship_photo).scaled(40, 25) map_l.setpixmap(maps) # 右邊的縱向布局 layout_right = qvboxlayout() # 右下的的橫向布局 layout_right_down = qhboxlayout() # 右下的橫向布局 layout_right_down.addwidget(qlabel(ship_type)) layout_right_down.addwidget(qlabel(ship_country)) layout_right_down.addwidget(qlabel(str(ship_star) + "星" )) layout_right_down.addwidget(qlabel(ship_index)) # 按照從左到右, 從上到下布局添加 layout_main.addwidget(map_l) # 最左邊的頭像 layout_right.addwidget(qlabel(ship_name)) # 右邊的縱向布局 layout_right.addlayout(layout_right_down) # 右下角橫向布局 layout_main.addlayout(layout_right) # 右邊的布局 wight.setlayout(layout_main) # 布局給wight return wight # 返回wight |
設(shè)置qlistwidgetitem
1
2
3
4
5
6
|
for ship_data in your_data: item = qlistwidgetitem() # 創(chuàng)建qlistwidgetitem對象 item.setsizehint(qsize(200, 50)) # 設(shè)置qlistwidgetitem大小 widget = get_item_wight(ship_data) # 調(diào)用上面的函數(shù)獲取對應(yīng) self.listwidget.additem(item) # 添加item self.listwidget.setitemwidget(item, widget) # 為item設(shè)置widget |
顯示效果:
全部代碼
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
|
import sys import json from pyqt5.qtwidgets import * from pyqt5.qtgui import * from pyqt5.qtcore import * from pyqt5 import qtcore, qtgui, qtwidgets class ui_mainwindow(object): "" " 自動(dòng)生成的代碼, 請不要修改 "" " def setupui(self, mainwindow): mainwindow.setobjectname( "mainwindow" ) mainwindow.resize(455, 357) self.centralwidget = qtwidgets.qwidget(mainwindow) self.centralwidget.setobjectname( "centralwidget" ) self.listwidget = qtwidgets.qlistwidget(self.centralwidget) self.listwidget.setgeometry(qtcore.qrect(10, 10, 341, 341)) self.listwidget.setobjectname( "listwidget" ) self.pushbutton = qtwidgets.qpushbutton(self.centralwidget) self.pushbutton.setgeometry(qtcore.qrect(360, 10, 81, 31)) self.pushbutton.setobjectname( "pushbutton" ) mainwindow.setcentralwidget(self.centralwidget) self.retranslateui(mainwindow) qtcore.qmetaobject.connectslotsbyname(mainwindow) def retranslateui(self, mainwindow): _translate = qtcore.qcoreapplication.translate mainwindow.setwindowtitle(_translate( "mainwindow" , "mainwindow" )) self.pushbutton.settext(_translate( "mainwindow" , "pushbutton" )) class windows(qmainwindow, ui_mainwindow): def __init__(self): super(windows, self).__init__() self.setupui(self) self.pushbutton.clicked.connect(self.deal) def deal(self): all_data = json.loads( '[{"ship_name":"\u80e1\u5fb7","ship_country":"e\u56fd","ship_star":"5","ship_index":"1","ship_photo":"icon/1.png","ship_type":"\u6218\u5de1"},{"ship_name":"\u6d4b\u8bd5","ship_country":"e\u56fd","ship_star":"5","ship_index":"1","ship_photo":"icon/2.png","ship_type":"\u6218\u5de1"},{"ship_name":"\u6d4b\u8bd52","ship_country":"e\u56fd","ship_star":"5","ship_index":"1","ship_photo":"icon/3.png","ship_type":"\u6218\u5de1"},{"ship_name":"\u6d4b\u8bd53","ship_country":"e\u56fd","ship_star":"5","ship_index":"1","ship_photo":"icon/4.png","ship_type":"\u6218\u5de1"}]' ) def get_item_wight(data): # 讀取屬性 ship_name = data[ 'ship_name' ] ship_photo = data[ 'ship_photo' ] ship_index = data[ 'ship_index' ] ship_type = data[ 'ship_type' ] ship_country = data[ 'ship_country' ] ship_star = data[ 'ship_star' ] # 總widget wight = qwidget() # 總體橫向布局 layout_main = qhboxlayout() map_l = qlabel() # 頭像顯示 map_l.setfixedsize(40, 25) maps = qpixmap(ship_photo).scaled(40, 25) map_l.setpixmap(maps) # 右邊的縱向布局 layout_right = qvboxlayout() # 右下的的橫向布局 layout_right_down = qhboxlayout() # 右下的橫向布局 layout_right_down.addwidget(qlabel(ship_type)) layout_right_down.addwidget(qlabel(ship_country)) layout_right_down.addwidget(qlabel(str(ship_star) + "星" )) layout_right_down.addwidget(qlabel(ship_index)) # 按照從左到右, 從上到下布局添加 layout_main.addwidget(map_l) # 最左邊的頭像 layout_right.addwidget(qlabel(ship_name)) # 右邊的縱向布局 layout_right.addlayout(layout_right_down) # 右下角橫向布局 layout_main.addlayout(layout_right) # 右邊的布局 wight.setlayout(layout_main) # 布局給wight return wight # 返回wight for ship_data in all_data: item = qlistwidgetitem() # 創(chuàng)建qlistwidgetitem對象 item.setsizehint(qsize(200, 50)) # 設(shè)置qlistwidgetitem大小 widget = get_item_wight(ship_data) # 調(diào)用上面的函數(shù)獲取對應(yīng) self.listwidget.additem(item) # 添加item self.listwidget.setitemwidget(item, widget) # 為item設(shè)置widget app = qtwidgets.qapplication(sys.argv) windows = windows() windows.show() sys. exit (app.exec_()) |
補(bǔ)充:pyqt5 qlistwiget點(diǎn)擊item事件
我就廢話不多說了,大家還是直接看代碼吧~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
from pyqt4.qtcore import qcoreapplication, qt from pyqt4.qtgui import qlistwidget, qlistwidgetitem, qapplication import sys class mylist(qlistwidget): def __init__(self): qlistwidget.__init__(self) self.add_items() self.itemclicked.connect(self.item_click) def add_items(self): for item_text in [ 'item1' , 'item2' , 'item3' ]: item = qlistwidgetitem(item_text) self.additem(item) def item_click(self, item): print item, str(item.text()) if __name__ == '__main__' : app = qapplication([]) mylist = mylist() mylist.show() sys. exit (app.exec_()) |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/qq_42436176/article/details/88917897