📅  最后修改于: 2023-12-03 15:18:48.968000             🧑  作者: Mango
当使用PyQt5中的QListWidget来显示多个元素时,有时需要获取批量大小以便于进一步处理。本文将介绍如何使用PyQt5 QListWidget来获取批量大小。
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QListWidget Example")
self.setGeometry(100, 100, 300, 200)
layout = QVBoxLayout()
list_widget = QListWidget()
layout.addWidget(list_widget)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QListWidget Example")
self.setGeometry(100, 100, 300, 200)
layout = QVBoxLayout()
list_widget = QListWidget()
for i in range(10):
item = QListWidgetItem("Item {}".format(i))
list_widget.addItem(item)
layout.addWidget(list_widget)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
batch_size = list_widget.count()
print("Batch size:", batch_size)
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QListWidget Example")
self.setGeometry(100, 100, 300, 200)
layout = QVBoxLayout()
list_widget = QListWidget()
layout.addWidget(list_widget)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
for i in range(10):
item = QListWidgetItem("Item {}".format(i))
list_widget.addItem(item)
batch_size = list_widget.count()
print("Batch size:", batch_size)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
使用PyQt5中的QListWidget可以轻松地获取批量大小。只需使用count()
方法即可获取当前批量大小。