简体   繁体   English

PyQt:如何在 QListWidget 中向右滚动?

[英]PyQt: How to scroll to right in a QListWidget?

In a PyQt QListWidget I drag and drop files with their full path.在 PyQt QListWidget 中,我使用完整路径拖放文件。 Now I want the scrollbar to scroll to the right everytime I drag something in. However, there seems to be only scrollToTop() and scrollToBottom() , but I couldn't find scrollToLeft() or scrollToRight() or anything similar.现在我希望滚动条在每次拖动某些东西时向右滚动。但是,似乎只有scrollToTop()scrollToBottom() ,但我找不到scrollToLeft()scrollToRight()或类似的东西。 Right alignment of the list items' text does not help, apparently I need to scroll the horizontal scrollbar to the right.列表项文本的右 alignment 没有帮助,显然我需要向右滚动水平滚动条。

How can I automatically scroll to right, such that I see the filenames, not the beginning of the path?如何自动向右滚动,以便我看到文件名,而不是路径的开头? It must be a simple setting, but I haven't found helpful documentation or examples so far.它必须是一个简单的设置,但到目前为止我还没有找到有用的文档或示例。

Code:代码:

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QListWidget, QListWidgetItem, QApplication, QWidget, QAbstractItemView, QVBoxLayout

class MyListWidget(QListWidget):
    def __init__(self, parent):
        super(MyListWidget, self).__init__(parent)
        # self.setDragDropMode(QAbstractItemView.InternalMove)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls() or event.mimeData().hasFormat("text/plain"):
            event.acceptProposedAction()
        else:
            super(MyListWidget, self).dragEnterEvent(event)

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            for url in event.mimeData().urls():
                item = QListWidgetItem(url.toLocalFile())
                self.addItem(item)
            event.acceptProposedAction()
        elif event.mimeData().hasFormat("text/plain"):
            self.addItem(event.mimeData().text())
        else:
            super(myListWidget,self).dropEvent(event)
        self.scrollToBottom()                            ### but I want scrollToRight()  !!! 
        

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.left = 50 
        self.top = 50
        self.width = 900
        self.height = 500
        self.initUI()

    def initUI(self):
        self.vbox = QVBoxLayout()
        self.lw_myList = MyListWidget(self)
        self.vbox.addWidget(self.lw_myList)
        self.setLayout(self.vbox)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    # app.setStyle("plastique")
    window = MyWindow()
    # window.show()
    sys.exit(app.exec_())

Result:结果:

在此处输入图像描述

Programmatical scrolling of all widgets that inherit from QAbstractScrollArea can be achieved (and should only be done) by setting the value of its scrollbars (even if they are not visible).从 QAbstractScrollArea 继承的所有小部件的编程滚动可以通过设置其滚动条的值(即使它们不可见)来实现(并且应该完成)。

Since item views like QListWidget (which inherits from QListView and QAbstractItemView) need some time in order to correctly lay out new items, it's usually preferred to call QCoreApplication.processEvents() and, just to be safe, updateGeometries() :由于像 QListWidget(继承自 QListView 和 QAbstractItemView)这样的项目视图需要一些时间才能正确布置新项目,因此通常首选调用QCoreApplication.processEvents()并且为了安全起见, updateGeometries()

    def dropEvent(self, event):
        # ...

        # ensure that the event queue is correctly processed, which includes
        # laying out new items
        QApplication.processEvents()
        # ensure that the geometry of child widgets is correctly updated too
        self.updateGeometries()
        # finally, set the horizontal scroll bar to its maximum
        self.horizontalScrollBar().setValue(self.horizontalScrollBar().maximum())

You can even provide a "monkey patched" function for all item views, by putting this at the beginning of the first script that imports QtWidgets:您甚至可以为所有项目视图提供“猴子补丁”function,方法是将其放在导入 QtWidgets 的第一个脚本的开头:

def itemViewScrollToRight(self):
    QApplication.processEvents()
    self.updateGeometries()
    self.horizontalScrollBar().setValue(
        self.horizontalScrollBar().maximum())

QAbstractItemView.scrollToRight = itemViewScrollToRight

# ...
    def dropEvent(self, event):
        # ...
        self.scrollToRight()

PS: note that you should always implement dragMoveEvent() and accept the event for custom drag implementations, as dragEnterEvent() doesn't always suffice. PS:请注意,您应该始终实现dragMoveEvent()并接受自定义拖动实现的事件,因为dragEnterEvent()并不总是足够的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM