是否有更好的方法可以使用PYQT6创建文件浏览器?

发布于 2025-01-24 13:55:21 字数 1292 浏览 2 评论 0原文

我正在使用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:

  • My apologies, it's not just any file browser, it's an image file browser.
  • Example image of what I'm thinking of creating:
    enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半岛未凉 2025-01-31 13:55:21

一个基本的可能性是在添加项目时使用QListWidget,并具有一些自定义的设置和预防措施:

class ImageListView(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.view = QtWidgets.QListWidget()
        self.view.setIconSize(QtCore.QSize(64, 64))
        bigFont = self.font()
        bigFont.setPointSize(24)
        self.view.setFont(bigFont)

        self.addButton = QtWidgets.QPushButton('Add image(s)')

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.view)
        layout.addWidget(self.addButton)

        self.addButton.clicked.connect(self.addImage)

    def addImage(self):
        paths, _ = QtWidgets.QFileDialog.getOpenFileNames(self, 
            'Select image(s)', '', 'Images (*.png *.jpg *.jpeg)')

        size = self.view.iconSize()
        for path in paths:
            source = QtGui.QPixmap(path)
            if source.isNull():
                continue

            if source.width() > size.width() or source.height() > size.height():
                source = source.scaled(size, QtCore.Qt.KeepAspectRatio, 
                    QtCore.Qt.SmoothTransformation)

            # create an empty squared image to keep vertical alignment
            square = QtGui.QPixmap(size)
            square.fill(QtCore.Qt.transparent)
            qp = QtGui.QPainter(square)
            rect = source.rect()
            rect.moveCenter(square.rect().center())
            qp.drawPixmap(rect, source)
            qp.end()

            name = QtCore.QFileInfo(path).baseName()
            item = QtWidgets.QListWidgetItem(name)
            item.setIcon(QtGui.QIcon(square))
            item.setToolTip(path)
            item.setSizeHint(size)
            self.view.addItem(item)

对于更高级的自定义,您仍然可以使用QListWidget,但是您还需要设置自定义项目委托并覆盖其 paint() 方法。

A basic possibility is to use a QListWidget, with some customized settings and precautions when adding items:

  • the iconSize() must be big enough to show the thumbnails;
  • a bigger font for the view
  • the sizeHint() of each item must be specified in order to always respect the same row height and provide text elision;
  • the image must be scaled and "enlarged" to the icon size in order to keep vertical alignment of the text, otherwise images that have different widths will show the text starting at different positions;
class ImageListView(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.view = QtWidgets.QListWidget()
        self.view.setIconSize(QtCore.QSize(64, 64))
        bigFont = self.font()
        bigFont.setPointSize(24)
        self.view.setFont(bigFont)

        self.addButton = QtWidgets.QPushButton('Add image(s)')

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.view)
        layout.addWidget(self.addButton)

        self.addButton.clicked.connect(self.addImage)

    def addImage(self):
        paths, _ = QtWidgets.QFileDialog.getOpenFileNames(self, 
            'Select image(s)', '', 'Images (*.png *.jpg *.jpeg)')

        size = self.view.iconSize()
        for path in paths:
            source = QtGui.QPixmap(path)
            if source.isNull():
                continue

            if source.width() > size.width() or source.height() > size.height():
                source = source.scaled(size, QtCore.Qt.KeepAspectRatio, 
                    QtCore.Qt.SmoothTransformation)

            # create an empty squared image to keep vertical alignment
            square = QtGui.QPixmap(size)
            square.fill(QtCore.Qt.transparent)
            qp = QtGui.QPainter(square)
            rect = source.rect()
            rect.moveCenter(square.rect().center())
            qp.drawPixmap(rect, source)
            qp.end()

            name = QtCore.QFileInfo(path).baseName()
            item = QtWidgets.QListWidgetItem(name)
            item.setIcon(QtGui.QIcon(square))
            item.setToolTip(path)
            item.setSizeHint(size)
            self.view.addItem(item)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文