pyqt5-QListWidget
Posted wd404
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyqt5-QListWidget相关的知识,希望对你有一定的参考价值。
1、介绍
list组件,或者说列表组件。
2、类和初始化
class QListWidget(QListView):
快速查询:
QListWidget(parent: typing.Optional[QWidget] = None)
addItem(self, aitem: QListWidgetItem)
addItem(self, label: str)
addItems(self, labels: Iterable[str])
clear(self)
closePersistentEditor(self, item: QListWidgetItem)
count(self) -> int
currentItem(self) -> QListWidgetItem
currentRow(self) -> int
dropMimeData(self, index: int, data: QMimeData, action: Qt.DropAction) -> bool
editItem(self, item: QListWidgetItem)
findItems(self, text: str, flags: Union[Qt.MatchFlags, Qt.MatchFlag]) -> List[QListWidgetItem]
indexFromItem(self, item: QListWidgetItem) -> QModelIndex
insertItem(self, row: int, item: QListWidgetItem)
insertItem(self, row: int, label: str)
insertItems(self, row: int, labels: Iterable[str])
isPersistentEditorOpen(self, item: QListWidgetItem) -> bool
isSortingEnabled(self) -> bool
item(self, row: int) -> QListWidgetItem
itemAt(self, p: QPoint) -> QListWidgetItem
itemAt(self, ax: int, ay: int) -> QListWidgetItem
itemFromIndex(self, index: QModelIndex) -> QListWidgetItem
items(self, data: QMimeData) -> List[QListWidgetItem]
itemWidget(self, item: QListWidgetItem) -> QWidget
mimeData(self, items: Iterable[QListWidgetItem]) -> QMimeData
mimeTypes(self) -> List[str]
openPersistentEditor(self, item: QListWidgetItem)
removeItemWidget(self, aItem: QListWidgetItem)
row(self, item: QListWidgetItem) -> int
scrollToItem(self, item: QListWidgetItem, hint: QAbstractItemView.ScrollHint = QAbstractItemView.EnsureVisible)
selectedItems(self) -> List[QListWidgetItem]
setCurrentItem(self, item: QListWidgetItem)
setCurrentItem(self, item: QListWidgetItem, command: Union[QItemSelectionModel.SelectionFlags, QItemSelectionModel.SelectionFlag])
setCurrentRow(self, row: int)
setCurrentRow(self, row: int, command: Union[QItemSelectionModel.SelectionFlags, QItemSelectionModel.SelectionFlag])
setItemWidget(self, item: QListWidgetItem, widget: QWidget)
setSelectionModel(self, selectionModel: QItemSelectionModel)
setSortingEnabled(self, enable: bool)
sortItems(self, order: Qt.SortOrder = Qt.AscendingOrder)
supportedDropActions(self) -> Qt.DropActions
takeItem(self, row: int) -> QListWidgetItem
visualItemRect(self, item: QListWidgetItem) -> QRect
Return len(self).
---------------
dropEvent(self, event: QDropEvent)
event(self, e: QEvent) -> bool
---------------
currentItemChanged(self, current: QListWidgetItem, previous: QListWidgetItem) [signal]
currentRowChanged(self, currentRow: int) [signal]
currentTextChanged(self, currentText: str) [signal]
itemActivated(self, item: QListWidgetItem) [signal]
itemChanged(self, item: QListWidgetItem) [signal]
itemClicked(self, item: QListWidgetItem) [signal]
itemDoubleClicked(self, item: QListWidgetItem) [signal]
itemEntered(self, item: QListWidgetItem) [signal]
itemPressed(self, item: QListWidgetItem) [signal]
itemSelectionChanged(self) [signal]
3、方法
基本可以参考QComboBox组件的相关使用
4、事件
PyQt5 QListWidget自定义项目[重复]
【中文标题】PyQt5 QListWidget自定义项目[重复]【英文标题】:PyQt5 QListWidget custom items [duplicate] 【发布时间】:2020-02-05 10:48:09 【问题描述】:我对 PyQt5 非常陌生,我正在尝试为 python3+ 中的 qlistwidget 制作自定义 qlistwidgetitem,但到目前为止没有成功。
我喜欢并尝试移植的原始代码来自此链接。不幸的是,它在 PyQt4 中:
PyQt QListWidget custom items
import sys
from PyQt4 import QtGui
class QCustomQWidget (QtGui.QWidget):
def __init__ (self, parent = None):
super(QCustomQWidget, self).__init__(parent)
self.textQVBoxLayout = QtGui.QVBoxLayout()
self.textUpQLabel = QtGui.QLabel()
self.textDownQLabel = QtGui.QLabel()
self.textQVBoxLayout.addWidget(self.textUpQLabel)
self.textQVBoxLayout.addWidget(self.textDownQLabel)
self.allQHBoxLayout = QtGui.QHBoxLayout()
self.iconQLabel = QtGui.QLabel()
self.allQHBoxLayout.addWidget(self.iconQLabel, 0)
self.allQHBoxLayout.addLayout(self.textQVBoxLayout, 1)
self.setLayout(self.allQHBoxLayout)
# setStyleSheet
self.textUpQLabel.setStyleSheet('''
color: rgb(0, 0, 255);
''')
self.textDownQLabel.setStyleSheet('''
color: rgb(255, 0, 0);
''')
def setTextUp (self, text):
self.textUpQLabel.setText(text)
def setTextDown (self, text):
self.textDownQLabel.setText(text)
def setIcon (self, imagePath):
self.iconQLabel.setPixmap(QtGui.QPixmap(imagePath))
class exampleQMainWindow (QtGui.QMainWindow):
def __init__ (self):
super(exampleQMainWindow, self).__init__()
# Create QListWidget
self.myQListWidget = QtGui.QListWidget(self)
for index, name, icon in [
('No.1', 'Meyoko', 'icon.png'),
('No.2', 'Nyaruko', 'icon.png'),
('No.3', 'Louise', 'icon.png')]:
# Create QCustomQWidget
myQCustomQWidget = QCustomQWidget()
myQCustomQWidget.setTextUp(index)
myQCustomQWidget.setTextDown(name)
myQCustomQWidget.setIcon(icon)
# Create QListWidgetItem
myQListWidgetItem = QtGui.QListWidgetItem(self.myQListWidget)
# Set size hint
myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint())
# Add QListWidgetItem into QListWidget
self.myQListWidget.addItem(myQListWidgetItem)
self.myQListWidget.setItemWidget(myQListWidgetItem, myQCustomQWidget)
self.setCentralWidget(self.myQListWidget)
app = QtGui.QApplication([])
window = exampleQMainWindow()
window.show()
sys.exit(app.exec_())
欢迎任何帮助。
【问题讨论】:
【参考方案1】:PyQt4 和 PyQt5 的区别https://docs.huihoo.com/pyqt/PyQt5/pyqt4_differences.html#differences-between-pyqt4-and-pyqt5
import sys
#from PyQt4 import QtGui
from PyQt5 import QtGui, QtWidgets
class QCustomQWidget (QtWidgets.QWidget): # QtWidgets
def __init__ (self, parent = None):
super(QCustomQWidget, self).__init__(parent)
self.textQVBoxLayout = QtWidgets.QVBoxLayout() # QtWidgets
self.textUpQLabel = QtWidgets.QLabel() # QtWidgets
self.textDownQLabel = QtWidgets.QLabel() # QtWidgets
self.textQVBoxLayout.addWidget(self.textUpQLabel)
self.textQVBoxLayout.addWidget(self.textDownQLabel)
self.allQHBoxLayout = QtWidgets.QHBoxLayout() # QtWidgets
self.iconQLabel = QtWidgets.QLabel() # QtWidgets
self.allQHBoxLayout.addWidget(self.iconQLabel, 0)
self.allQHBoxLayout.addLayout(self.textQVBoxLayout, 1)
self.setLayout(self.allQHBoxLayout)
# setStyleSheet
self.textUpQLabel.setStyleSheet('''
color: rgb(0, 0, 255);
''')
self.textDownQLabel.setStyleSheet('''
color: rgb(255, 0, 0);
''')
def setTextUp (self, text):
self.textUpQLabel.setText(text)
def setTextDown (self, text):
self.textDownQLabel.setText(text)
def setIcon (self, imagePath):
self.iconQLabel.setPixmap(QtGui.QPixmap(imagePath).scaled(60, 60)) # + .scaled(60, 60
class exampleQMainWindow (QtWidgets.QMainWindow): # QtWidgets
def __init__ (self):
super(exampleQMainWindow, self).__init__()
# Create QListWidget
self.myQListWidget = QtWidgets.QListWidget(self) # QtWidgets
for index, name, icon in [
('No.1', 'Meyoko', 'lena-2.png'),
('No.2', 'Nyaruko', 'im.png'),
('No.3', 'Louise', 'Ok.png')]:
# Create QCustomQWidget
myQCustomQWidget = QCustomQWidget()
myQCustomQWidget.setTextUp(index)
myQCustomQWidget.setTextDown(name)
myQCustomQWidget.setIcon(icon)
# Create QListWidgetItem
myQListWidgetItem = QtWidgets.QListWidgetItem(self.myQListWidget) # QtWidgets
# Set size hint
myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint())
# Add QListWidgetItem into QListWidget
self.myQListWidget.addItem(myQListWidgetItem)
self.myQListWidget.setItemWidget(myQListWidgetItem, myQCustomQWidget)
self.setCentralWidget(self.myQListWidget)
if __name__ == '__main__':
app = QtWidgets.QApplication([]) # QtWidgets
window = exampleQMainWindow()
window.show()
sys.exit(app.exec_())
【讨论】:
myQListWidgetItem = QtWidgets.QListWidgetItem(self.myQListWidget) 这是我不明白的唯一一行。这会将项目实例化为列表、列表的子项还是其他内容? @MichaelEffraimidis doc.qt.io/qt-5/qlistwidgetitem.html#details 我已成功创建自定义小部件列表。我该如何继续以有效的方式删除它(列表是动态的)?我尝试了 myQListWidget.removeItemWidget(myQCustomQWidget) 但它丢弃了错误“参数 1 具有意外类型'myQCustomQWidget'。我必须保留并使用 myQListWidgetItem 变量还是有更有效的方法? @MichaelEffraimidis 如果您有其他问题,您应该创建一个新问题。以上是关于pyqt5-QListWidget的主要内容,如果未能解决你的问题,请参考以下文章