溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-03-09 17:21:40 來(lái)源:億速云 閱讀:540 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

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

自定義一個(gè)Item

新建一個(gè)QWidget對(duì)象

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

使用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

可以看出此布局總體是一個(gè)橫向布局(QHBoxLayout), 再其右邊是一個(gè)縱向(QVBoxLayout), 下面的布局又是一個(gè)橫向布局(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國(guó)",
 "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對(duì)象
 item.setSizeHint(QSize(200, 50)) # 設(shè)置QListWidgetItem大小
 widget = get_item_wight(ship_data) # 調(diào)用上面的函數(shù)獲取對(duì)應(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):
 """
 自動(dòng)生成的代碼, 請(qǐ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對(duì)象
   item.setSizeHint(QSize(200, 50)) # 設(shè)置QListWidgetItem大小
   widget = get_item_wight(ship_data) # 調(diào)用上面的函數(shù)獲取對(duì)應(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事件

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

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)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI