1、实现QListWidget按照N像素进行滑动;
2、实现自定义QWidget作为QListWidgetItem;
代码实现:
from PyQt5.QtWidgets import QWidget, QLabel, QVBoxLayout, QHBoxLayout, QTextEdit, QListWidget, QListWidgetItem, QScrollArea
from PyQt5.QtGui import QFont
from PyQt5.Qt import Qt, QSize
from model.info_json_data import InfoJsonclass InfoPanel(QWidget):def __init__(self, parent, info_dict):super(InfoPanel, self).__init__(parent)self.info_dict = info_dict#设置QListWidgetself.info_item_s = QListWidget(self)self.info_item_s.setHorizontalScrollMode(QListWidget.ScrollPerPixel)self.info_item_s.setVerticalScrollMode(QListWidget.ScrollPerPixel)self.info_item_s.verticalScrollBar().setSingleStep(5)self.info_item_s.horizontalScrollBar().setSingleStep(5)self.hobby_widget = QWidget()self.matter_widget = QWidget()self.hobby_label = self.create_label(self.hobby_widget)self.matter_label = self.create_label(self.matter_widget)self.hobby_layout = QVBoxLayout()self.hobby_edit = self.create_edit(self.hobby_widget)self.hobby_layout.addWidget(self.hobby_label)self.hobby_layout.addWidget(self.hobby_edit)self.matter_layout = QVBoxLayout()self.matter_edit = self.create_edit(self.matter_widget)self.matter_layout.addWidget(self.matter_label)self.matter_layout.addWidget(self.matter_edit)# self.info_item_s.setLayout(self.hlayout)self.hobby_widget.setLayout(self.hobby_layout)self.matter_widget.setLayout(self.matter_layout)self.hobby_item = QListWidgetItem()self.hobby_item.setSizeHint(QSize(self.hobby_widget.width(), 300))self.info_item_s.addItem(self.hobby_item)self.info_item_s.setItemWidget(self.hobby_item, self.hobby_widget)self.matter_item = QListWidgetItem()self.matter_item.setSizeHint(QSize(self.matter_widget.width(), 300))self.info_item_s.addItem(self.matter_item)self.info_item_s.setItemWidget(self.matter_item, self.matter_widget)self.layout = QVBoxLayout()self.layout.addWidget(self.info_item_s)self.setLayout(self.layout)# signal -> slotself.matter_edit.textChanged.connect(self.save_matter_context)self.hobby_edit.textChanged.connect(self.save_hobby_context)@staticmethoddef create_label(parent):font = QFont()font.setFamily('LiSu')font.setPointSize(16)label = QLabel(parent)label.setFont(font)label.setAlignment(Qt.AlignLeft)return label@staticmethoddef create_edit(parent):font = QFont()font.setFamily('LiSu')font.setPointSize(14)edit = QTextEdit(parent)edit.setFont(font)edit.setAlignment(Qt.AlignLeft)return edit