欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyQt5 在QListWidget自定義Item的操作

 更新時間:2021年03月09日 14:31:32   作者:Simon菌  
這篇文章主要介紹了PyQt5 在QListWidget自定義Item的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

效果圖

自定義一個Item

新建一個QWidget對象

在QWidget內(nèi)添加Layout

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

為QWidget設(shè)置Layout

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

為QListWidgetItem設(shè)置QWidget

創(chuàng)建布局

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

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

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)備實現(xiàn)的功能
  pass
app = QtWidgets.QApplication(sys.argv)
windows = Windows()
windows.show()
sys.exit(app.exec_())

確定布局

可以看出此布局總體是一個橫向布局(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

顯示效果:

全部代碼

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

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • python實現(xiàn)將視頻按幀讀取到自定義目錄

    python實現(xiàn)將視頻按幀讀取到自定義目錄

    今天小編就為大家分享一篇python實現(xiàn)將視頻按幀讀取到自定義目錄,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 使用Python編寫腳本來生成隨機(jī)密碼

    使用Python編寫腳本來生成隨機(jī)密碼

    在數(shù)字化時代,密碼已成為我們保護(hù)個人信息和數(shù)據(jù)安全的重要手段,然而,手動創(chuàng)建復(fù)雜且難以猜測的密碼是一項既繁瑣又容易出錯的任務(wù),本文將詳細(xì)介紹如何使用Python編寫腳本來生成隨機(jī)密碼,并探討其背后的原理和技術(shù)細(xì)節(jié),需要的朋友可以參考下
    2024-10-10
  • python實現(xiàn)的登錄和操作開心網(wǎng)腳本分享

    python實現(xiàn)的登錄和操作開心網(wǎng)腳本分享

    這篇文章主要介紹了python實現(xiàn)的登錄和操作開心網(wǎng)腳本分享,可以登錄開心網(wǎng),登錄后發(fā)送信息等功能,需要的朋友可以參考下
    2014-07-07
  • scrapy-redis源碼分析之發(fā)送POST請求詳解

    scrapy-redis源碼分析之發(fā)送POST請求詳解

    這篇文章主要給大家介紹了關(guān)于scrapy-redis源碼分析之發(fā)送POST請求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用scrapy-redis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Python實現(xiàn)的彩票機(jī)選器實例

    Python實現(xiàn)的彩票機(jī)選器實例

    這篇文章主要介紹了Python實現(xiàn)彩票機(jī)選器的方法,可以模擬彩票號碼的隨機(jī)生成功能,需要的朋友可以參考下
    2015-06-06
  • Python爬蟲使用Selenium+PhantomJS抓取Ajax和動態(tài)HTML內(nèi)容

    Python爬蟲使用Selenium+PhantomJS抓取Ajax和動態(tài)HTML內(nèi)容

    這篇文章主要介紹了Python爬蟲使用Selenium+PhantomJS抓取Ajax和動態(tài)HTML內(nèi)容,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • python之cur.fetchall與cur.fetchone提取數(shù)據(jù)并統(tǒng)計處理操作

    python之cur.fetchall與cur.fetchone提取數(shù)據(jù)并統(tǒng)計處理操作

    這篇文章主要介紹了python之cur.fetchall與cur.fetchone提取數(shù)據(jù)并統(tǒng)計處理操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Django模型層實現(xiàn)多表關(guān)系創(chuàng)建和多表操作

    Django模型層實現(xiàn)多表關(guān)系創(chuàng)建和多表操作

    使用django ORM可以創(chuàng)建多表關(guān)系,并且也支持多張表之間的操作,以創(chuàng)建表關(guān)系和查詢兩部分說明django ORM的多表操作,本文就詳細(xì)的介紹一下,感興趣的可以了解一下
    2021-07-07
  • 在cmd中查看python的安裝路徑方法

    在cmd中查看python的安裝路徑方法

    在本篇文章里小編給大家整理的是關(guān)于怎樣在cmd中查看python的安裝路徑的相關(guān)內(nèi)容,有興趣的朋友們學(xué)習(xí)參考下。
    2019-07-07
  • Python實現(xiàn)線程池工作模式的案例詳解

    Python實現(xiàn)線程池工作模式的案例詳解

    這篇文章給大家介紹Python實現(xiàn)線程池工作模式的相關(guān)知識,本文基于Socket通信方法,自定義數(shù)據(jù)交換協(xié)議,圍繞蘋果樹病蟲害識別需求,迭代構(gòu)建了客戶機(jī)/服務(wù)器模式的智能桌面App,感興趣的朋友跟隨小編一起看看吧
    2022-06-06

最新評論