PyQt QTest 单击 QMenu

标签 pyqt pyqt4 python-unittest qtestlib

在我的 PyQt 应用程序中,我有一个主菜单,其结构如下:

Main Menu
|___
    |
    Language
    |       |
    |        Russian
    |       |
    |        English
    Exit    

我想点击俄语来测试语言是否已更改。我在 GUI 中有一个指向 language_menu 的链接

[...somewhere in MainWindow...]
self.language_menu = QtGui.QMenu()
[....]

所以

from PyQt4 import QtGui, QtCore
from PyQt4.QTest import QTest
from gui import MainWindow

class TestMainWindow(unittest.TestCase):

    def setUp(self):
        self.app = QtGui.QApplication([])
        self.ui = MainWindow()

    def tearDown(self):
        self.app.deleteLater()

    def test_translation(self):
        menu = self.ui.language_menu
        rus_lang = menu.actions()[0]
        QTest.mouseClick(rus_lang, QtCore.Qt.LeftButton)

但它告诉我

argument 1 has unexpected type 'QAction'

我该怎么做?这可能吗?

最佳答案

您可以使用rus_lang.trigger()rus_lang.toggle()来激活菜单项。

关于PyQt QTest 单击 QMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35364393/

相关文章:

python - 函数在 500 次迭代后打印 500 个值函数更新值到 listwidget

python - 如何使 QGridLayout() 在水平和垂直方向上都可调整大小?

python - QGraphicsView.fitInView() 未按预期工作

Python 模拟 : How to inject an object at a specific point in a function?

python - 单元测试 - ImportError : Start directory is not importable

python - Python 和 PySide 中带超时的线程上传

python - Ubuntu 的音量控制通知

python - 如何在 QTableWidget 中打印数据库中的数据?

python - 放大图像时控制平移(锚定点)

pandas - 在单元测试中创建 Pandas 数据框