我想测试从面板按下按钮的功能。问题是按钮不是 QPushButtons,而是 QGraphicWidget 元素。
如何生成此按钮鼠标单击以测试其行为?
按钮不是简单的 QPushButtons,因为它们在鼠标悬停时具有特殊行为、动画等。这些按钮被添加到场景中,然后将场景添加到视图中,并将视图作为小部件添加到布局中。我可以看到可以使用以下方法单击视图:
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'),但我不知道如何生成点击第二个按钮。