使用PyQt5如何在QListWidget中自定義Item-創(chuàng)新互聯(lián)

使用PyQt5 如何在QListWidget中自定義Item?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),安平企業(yè)網(wǎng)站建設(shè),安平品牌網(wǎng)站建設(shè),網(wǎng)站定制,安平網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,安平網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

自定義一個Item

新建一個QWidget對象

在QWidget內(nèi)添加Layout

在Layout內(nèi)添加要的控件

為QWidget設(shè)置Layout

新建一個QListWidgetItem并調(diào)整大小

為QListWidgetItem設(shè)置QWidget

創(chuàng)建布局

首先我們創(chuàng)建一個最基本的布局, 只有一個listWidget和一個pushButton

實(shí)現(xiàn)點(diǎn)擊button后在listWidget中添加數(shù)據(jù)

使用PyQt5 如何在QListWidget中自定義Item

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_())

確定布局

使用PyQt5 如何在QListWidget中自定義Item

可以看出此布局總體是一個橫向布局(QHBoxLayout), 再其右邊是一個縱向(QVBoxLayout), 下面的布局又是一個橫向布局(QHBoxLayout)

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

{
 "ship_name": "胡德",
 "ship_country": "E國",
 "ship_star": "5",
 "ship_index": "1",
 "ship_photo": "1.png",
 "ship_type": "戰(zhàn)巡"
}
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

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

顯示效果:

使用PyQt5 如何在QListWidget中自定義Item

全部代碼

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):
 """
 自動生成的代碼, 請不要修改
 """
 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事件

我就廢話不多說了,大家還是直接看代碼吧~

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_())

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

文章標(biāo)題:使用PyQt5如何在QListWidget中自定義Item-創(chuàng)新互聯(lián)
瀏覽路徑:http://muchs.cn/article30/dcpgpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、建站公司、做網(wǎng)站、網(wǎng)站營銷、定制開發(fā)、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司