是否有更好的方法可以使用PYQT6创建文件浏览器?
我正在使用PYQT6创建文件浏览器。这就是我现在正在考虑的事情:
from PyQt6 import QtWidgets as qtw
from PyQt6 import QtGui as qtg
class FileBrowserWidget(qtw.QScrollArea):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.current_items = []
self.main_widget = qtw.QWidget()
self.main_widget.setLayout(qtw.QVBoxLayout())
self.setWidget(self.main_widget)
def add_file(self, thumbnail: qtg.QPixmap, description: str):
item = qtw.QWidget()
item.setLayout(qtw.QHBoxLayout())
file_thumbnail_label = qtw.QLabel()
file_thumbnail_label.setPixmap(thumbnail)
file_description_label = qtw.QLabel(description)
item.layout().addWidget(file_thumbnail_label)
item.layout().addWidget(file_description_label)
self.current_items.append(item)
请注意,这只是小部件的粗略草图。所有代码所做的就是显示(缩略图,描述)
对可滚动窗口中目录内的文件对。我还计划为其实施分页,每页至少有25行(文件)。
我的问题是:
- 这是这样做的方法,还是创建文件浏览器的其他更好方法?
- 我将如何对文件浏览器实施分页?
edit :
I'm creating a file browser with PyQt6. This is what I'm thinking of doing right now:
from PyQt6 import QtWidgets as qtw
from PyQt6 import QtGui as qtg
class FileBrowserWidget(qtw.QScrollArea):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.current_items = []
self.main_widget = qtw.QWidget()
self.main_widget.setLayout(qtw.QVBoxLayout())
self.setWidget(self.main_widget)
def add_file(self, thumbnail: qtg.QPixmap, description: str):
item = qtw.QWidget()
item.setLayout(qtw.QHBoxLayout())
file_thumbnail_label = qtw.QLabel()
file_thumbnail_label.setPixmap(thumbnail)
file_description_label = qtw.QLabel(description)
item.layout().addWidget(file_thumbnail_label)
item.layout().addWidget(file_description_label)
self.current_items.append(item)
Note that this is just a rough sketch of the widget. All the code does is display a (thumbnail, description)
pair for files inside a directory in a scrollable window. I also plan to implement pagination for it, with at least 25 rows (files) per page.
My questions are:
- Is this the way to do it or is there some other better way to go about creating a file browser?
- How would I go about implementing pagination to the file browser?
EDIT:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个基本的可能性是在添加项目时使用QListWidget,并具有一些自定义的设置和预防措施:
iconsize()
必须足够大才能显示缩略图;对于更高级的自定义,您仍然可以使用QListWidget,但是您还需要设置自定义项目委托并覆盖其
paint()
方法。A basic possibility is to use a QListWidget, with some customized settings and precautions when adding items:
iconSize()
must be big enough to show the thumbnails;sizeHint()
of each item must be specified in order to always respect the same row height and provide text elision;For more advanced customization, you can still use a QListWidget, but you also need to set a custom item delegate and override its
paint()
method.