1 回答
TA贡献1828条经验 获得超3个赞
好吧,我已经找到了一种行之有效的方法。我在QListWidgetItem中添加了一些空白文本,并使字体变得非常大。这带回了项目的突出显示。
media_item.setText(" ") # set the item with a dummy string
media_item.setFont(QFont('Verdana', 180)) # make the font big so it covers the whole widget
经过一些研究,我发现使用QListView和Q代表是做到这一点的方法。我找不到使用PyQt5的好例子/教程,所以我现在只使用它。
这是代码
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from sys import exit as sysExit
from datetime import datetime as dtDateTime
class EntryWidget(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setCursor(QCursor(Qt.PointingHandCursor))
self.setFocusPolicy(Qt.StrongFocus) # Sets the Highlight when it has focus
# Controls
self.thumbnail = QLabel()
self.version = QLabel()
self.date = QLabel()
self.name = QLabel()
self.name.setAlignment(Qt.AlignCenter)
self.author = QLabel()
self.author.setAlignment(Qt.AlignRight)
self.dummy = QLabel(" ")
# Layout Container
VBox = QVBoxLayout()
VBox.setContentsMargins(0, 0, 0, 0)
VBox.setSpacing(0)
VBox.addWidget(self.name)
VBox.addWidget(self.thumbnail)
VBox.addWidget(self.version)
VBox.addWidget(self.date)
VBox.addWidget(self.author)
VBox.addWidget(self.dummy)
VBox.addStretch()
self.setLayout(VBox)
def set_size(self, w, h):
self.thumbnail.setFixedSize(w, h)
def set_version(self, name):
self.version.setText(" Version:" + str(name))
def set_date(self, name):
date_string = " Date: {0}/{1}/{2}\n Time: {3}:{4}:{5}".format(
str(name.day).zfill(2),
str(name.month).zfill(2),
name.year,
name.hour,
name.minute,
name.second)
self.date.setText(date_string)
def set_name(self, name):
self.name.setText(name)
def set_author(self, name):
self.author.setText(name + " ")
class QuickExample(QDialog):
def __init__(self):
QDialog.__init__(self)
self.resize(500, 500)
media_list = QListWidget(self)
# switch the views and select an item
media_list.setViewMode(QListWidget.IconMode)
media_list.setResizeMode(QListWidget.Adjust)
media_list.setMovement(QListWidget.Static)
media_list.setSpacing(5)
# dummy media, usually sourced from database
media = [
{"version": 1, "date": dtDateTime.now(), "name": "Entry 01", "author": "Bob"},
{"version": 2, "date": dtDateTime.now(), "name": "Entry 02", "author": "John"}
]
for i in media:
# Create media Entry
entry = EntryWidget()
entry.set_version(i["version"])
entry.set_date(i["date"])
entry.set_size(128, 72)
entry.set_name(i["name"])
entry.set_author(i["author"])
# Create QListWidgetItem
media_item = QListWidgetItem(media_list)
###########
# the fix #
###########
media_item.setText(" ") # set the item with a dummy string
media_item.setFont(QFont('Verdana', 180)) # make the font big so it covers the whole widget
# Set size hint
media_item.setSizeHint(entry.sizeHint())
# Add QListWidgetItem into QListWidget
media_list.addItem(media_item)
media_list.setItemWidget(media_item, entry)
VBox = QVBoxLayout()
VBox.addWidget(media_list)
self.setLayout(VBox)
if __name__ == "__main__":
MainEventHandler = QApplication([])
MainApplication = QuickExample()
MainApplication.show()
sysExit(MainEventHandler.exec_())
添加回答
举报