python - 如何在QTest中的QGraphicsWidget上生成鼠标点击?

标签 python pyside2 qttest qgraphicswidget qtest

我想测试从面板上按下按钮的功能。 问题是该按钮不是 QPushButtons,而是 QGraphicWidget 元素。

如何生成此按钮,单击鼠标来测试其行为?

按钮不是简单的 QPushButton,因为它们在鼠标悬停时有特殊的行为,有动画等。 这些按钮将添加到场景中,然后将场景添加到 View 中,并将 View 作为小部件添加到布局中。 我可以看到可以使用以下方式单击 View :

QTest.mouseClick(self.panel_with_buttons.view.viewport(), Qt.LeftButton)

但这并没有点击按钮。 我还尝试指定按钮的位置:

rect = self.panel_with_buttons.button2.boundingRect()
QTest.mouseClick(self.panel_with_buttons.view.viewport(), Qt.LeftButton, pos = rect.center())

但由于某种原因,这是不受支持的论点

Button 类和 PanelWithButtons 类的代码 (panel_with_buttons.py):

from PySide2 import QtWidgets, QtCore, QtGui


class Button(QtWidgets.QGraphicsWidget):
    pressed = QtCore.Signal()

    def __init__(self, image_path: str, parent=None):
        super().__init__(parent)
        self._pixmap = QtGui.QPixmap(image_path)

    def paint(self, painter, option, widget=None):
        painter.setPen(QtCore.Qt.NoPen)
        painter.drawPixmap(0, 0, self._pixmap)

    def mousePressEvent(self, event: QtWidgets.QGraphicsSceneMouseEvent):
        self.pressed.emit()


class PanelWithButtons(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.button = QtWidgets.QPushButton('button1')
        self.button.clicked.connect(self.button_clicked)

        pix = 'home.png'
        self.button2 = Button(pix)
        self.button2.pressed.connect(self.button2_pressed)

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.button)
        self.scene = QtWidgets.QGraphicsScene()
        self.scene.addItem(self.button2)
        self.view = QtWidgets.QGraphicsView(self.scene)
        layout.addWidget(self.view)

        self.setLayout(layout)

    @QtCore.Slot()
    def button_clicked(self):
        print('button1 clicked')

    @QtCore.Slot()
    def button2_pressed(self):
        print('button2 pressed')

测试.py:

from unittest import TestCase
from PySide2 import QtWidgets
from PySide2.QtTest import QTest
from PySide2.QtCore import Qt, QPoint
from test.panel_with_buttons import PanelWithButtons


class TestPanel(TestCase):
    def setUp(self) -> None:
        app = QtWidgets.QApplication()
        self.panel_with_buttons = PanelWithButtons()

    def test_go_cargo(self):
        QTest.mouseClick(self.panel_with_buttons.button, Qt.LeftButton)
        rect = self.panel_with_buttons.button2.boundingRect()
        QTest.mouseClick(self.panel_with_buttons.view.viewport(), Qt.LeftButton)

作为输出,我收到了对第一个按钮的鼠标点击(我可以看到“button1 clicked”),但我不知道如何生成对第二个按钮的点击。

最佳答案

panel_with_buttons.py 文件存在以下错误:

  • 您必须创建资源的绝对路径,在本例中为图像的路径。当您运行 .py 文件时,如果传递了相对路径,则这将是启动脚本的位置,这可能会带来问题(假设图像位于 panel_with_buttons.py 旁边)。

  • 您必须使用 resize() 方法将按钮的大小设置为图像的大小。

考虑到该文件应如下所示:

import os
from PySide2 import QtWidgets, QtCore, QtGui

current_dir = os.path.dirname(os.path.realpath(__file__))


class Button(QtWidgets.QGraphicsWidget):
    pressed = QtCore.Signal()

    def __init__(self, image_path: str, parent=None):
        super().__init__(parent)
        self._pixmap = QtGui.QPixmap(image_path)
        self.resize(self._pixmap.size())

    def paint(self, painter, option, widget=None):
        painter.setPen(QtCore.Qt.NoPen)
        painter.drawPixmap(0, 0, self._pixmap)

    def mousePressEvent(self, event: QtWidgets.QGraphicsSceneMouseEvent):
        self.pressed.emit()
        super().mousePressEvent(event)


class PanelWithButtons(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.button = QtWidgets.QPushButton("button1")
        self.button.clicked.connect(self.button_clicked)

        print(os.getcwd())
        pix = os.path.join(current_dir, "home.png")
        self.button2 = Button(pix)
        self.button2.pressed.connect(self.button2_pressed)

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.button)
        self.scene = QtWidgets.QGraphicsScene()
        self.scene.addItem(self.button2)
        self.view = QtWidgets.QGraphicsView(self.scene)
        layout.addWidget(self.view)

        self.setLayout(layout)

    @QtCore.Slot()
    def button_clicked(self):
        print("button1 clicked")

    @QtCore.Slot()
    def button2_pressed(self):
        print("button2 pressed")


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = PanelWithButtons()
    w.show()
    sys.exit(app.exec_())

另一方面,您必须使用Button的mapToScene和QGraphicsView的mapFromScene方法获得属于Button的点相对于视口(viewport)的位置,在这种情况下,该点将是Button的中心。

from unittest import TestCase
from PySide2 import QtCore, QtCore, QtWidgets, QtTest

from test.panel_with_buttons import PanelWithButtons


class TestPanel(TestCase):
    def setUp(self) -> None:
        app = QtWidgets.QApplication()
        self.panel_with_buttons = PanelWithButtons()
        self.panel_with_buttons.resize(640, 480)

    def test_go_cargo(self):
        QtTest.QTest.mouseClick(
            self.panel_with_buttons.button, QtCore.Qt.LeftButton
        )
        it = self.panel_with_buttons.button2
        sp = it.mapToScene(it.rect().center())
        p = self.panel_with_buttons.view.mapFromScene(sp)
        QtTest.QTest.mouseClick(
            self.panel_with_buttons.view.viewport(), QtCore.Qt.LeftButton, pos=p
        )

关于python - 如何在QTest中的QGraphicsWidget上生成鼠标点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57007556/

相关文章:

python - PySide2 与 qml 作为 ui

c++ - 使用 QTestLib 测试文件的打开

python - 从 PostgreSQL 存储过程导入 Python 文件

python - 如何停止从打印到 stderr 的 Django 单元测试记录?

python - 我的 QPainter 既不画线也不抛出错误,我该如何解决这个问题?

c++ - QtTest单元测试,如何添加位于另一个项目中的头文件?

unit-testing - 我应该为 Qt 使用什么单元测试框架?

Python - Pandas - 导入 Excel 文件,遍历每一行,添加新值,并添加到数据框

python - 傅立叶变换逆傅立叶变换python

python - QAbstratctTableModel - 删除行