向应用程序的用户提供视觉反馈

问题描述 投票:1回答:1

我正在制作一个包含Qlistwidget的应用。用户可以双击列表中的一个条目,或者在已经选择一个项目时按Enter键。当用户执行上述任何一项操作时,运行的是执行某项操作的功能,实际上什么都没有关系。

我想向用户提供视觉反馈,即确实已执行了预期的操作,但要尽可能简单。现在,我不是UI / UX使用者,但是我能想到的直观地向用户显示操作已发生的最佳方法是使item selection shadow闪烁/闪烁。

我不想显示一条短信,因为它会占用应用程序中的空间,我正在专门设计它以尽可能少地占用屏幕空间。

这可能吗?如果没有,还有其他我认为没有的方法会一样好。

如标题中所述,我正在使用PyQt5并在Windows上进行开发。

我浏览了PyQt文档并对其进行了Google搜索,但找不到任何内容。

python pyqt pyqt5
1个回答
2
投票

您可以创建一个QVariantAnimation来更改项目的颜色:

from PyQt5 import QtCore, QtGui, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.m_list_widget = QtWidgets.QListWidget()
        self.m_list_widget.itemDoubleClicked.connect(self.on_itemDoubleClicked)
        self.setCentralWidget(self.m_list_widget)

        for i in range(10):
            it = QtWidgets.QListWidgetItem(f"Item-{i}")
            self.m_list_widget.addItem(it)

    @QtCore.pyqtSlot(QtWidgets.QListWidgetItem)
    def on_itemDoubleClicked(self, it):
        self.m_list_widget.clearSelection()
        animation = QtCore.QVariantAnimation(self, duration=5 * 1000)
        animation.setProperty("item", it)

        color1 = QtGui.QColor("white")
        color2 = QtGui.QColor("red")

        colors = []
        number = 5

        color = color1 
        for _ in range(2*number+1):
            colors.append(color)
            color = color1 if color == color2 else color2

        numbers_of_colors = len(colors)
        for i, color in enumerate(colors):
            step = i / numbers_of_colors
            animation.setKeyValueAt(step, color)

        animation.valueChanged.connect(self.on_valueChanged)
        animation.start(QtCore.QAbstractAnimation.DeleteWhenStopped)

    @QtCore.pyqtSlot("QVariant")
    def on_valueChanged(self, value):
        animation = self.sender()
        it = animation.property("item")
        if isinstance(value, QtGui.QColor):
            it.setBackground(value)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    w = MainWindow()
    w.show()

    sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.