文章来源:https://www.toymoban.com/news/detail-611231.html
1、实现QListWidget按照N像素进行滑动;
2、实现自定义QWidget作为QListWidgetItem;
代码实现:文章来源地址https://www.toymoban.com/news/detail-611231.html
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 InfoJson
class InfoPanel(QWidget):
def __init__(self, parent, info_dict):
super(InfoPanel, self).__init__(parent)
self.info_dict = info_dict
#设置QListWidget
self.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 -> slot
self.matter_edit.textChanged.connect(self.save_matter_context)
self.hobby_edit.textChanged.connect(self.save_hobby_context)
@staticmethod
def create_label(parent):
font = QFont()
font.setFamily('LiSu')
font.setPointSize(16)
label = QLabel(parent)
label.setFont(font)
label.setAlignment(Qt.AlignLeft)
return label
@staticmethod
def create_edit(parent):
font = QFont()
font.setFamily('LiSu')
font.setPointSize(14)
edit = QTextEdit(parent)
edit.setFont(font)
edit.setAlignment(Qt.AlignLeft)
return edit
到了这里,关于QListWidget设置QWidget作为QListWidgetItem的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!