如何使用QtTest库访问QAction? [英] How to access a QAction using the QtTest lib?

查看:238
本文介绍了如何使用QtTest库访问QAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在QTableWidget(结果表)中有一个弹出菜单.在我的类的构造函数中,我设置了上下文菜单策略:

I have a pop-up menu in a QTableWidget (resultTable). In the constructor of my class I set the context menu policy:

resultTable->setContextMenuPolicy(Qt::CustomContextMenu);
connect(resultTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(popUpMenuResultTable(QPoint)));

popUpMenuResultTable函数:

void MyClass::popUpMenuResultTable(QPoint pos)
{
    QMenu menu;
    QAction* actionExport = menu.addAction(QIcon(":/new/prefix1/FileIcon.png"), tr("Export"));
    connect(actionExport, SIGNAL(triggered()), this, SLOT(exportResultsTable()));
    menu.popup(pos);
    menu.exec(QCursor::pos());
}

现在,我需要使用 QtTest 库实现一个功能来测试我的GUI.

Now, I need to implement a function to test my GUI using the QtTest lib.

如何通过右键单击 resultTable 来产生与用户相同的结果?基本上,我需要访问actionExport(QAction)并触发它.

How can I produce the same result as a user by right clicking on my resultTable? Basically, I need to get access to the actionExport (QAction) and trigger it.

例如:

我已经尝试过:

QTest::mouseClick(resultTable, Qt::RightButton, Qt::NoModifier, pos, delay);

,但不显示QMenu.

我正在使用Qt 5.3.2.

I'm using Qt 5.3.2.

推荐答案

也许不完全是您所追求的,而是一种更易于测试的替代方法.

Maybe not entirely what you are after but an alternative approach that is easier to test.

您可以使用小部件注册动作并使用Qt::ActionContextMenu:

Instead of creating the menu manually you register the actions with the widgets and use Qt::ActionContextMenu:

// e.g. in the widget's constructor
resultTable->setContextMenuPolicy(Qt::ActionsContextMenu);

QAction* actionExport = menu.addAction(QIcon(":/new/prefix1/FileIcon.png"), tr("Export"));
connect(actionExport, SIGNAL(triggered()), this, SLOT(exportResultsTable()));
resultTable->addAction(actionExport);

然后,您可以向返回resultTable->actions()的窗口小部件添加访问器,或者只是将actionExport设置为类的成员. 一旦您的测试代码可以访问该操作,就可以简单地调用其触发器trigger()方法.

Then you either add an accessor to your widget that returns resultTable->actions() or just make actionExport a member of your class. Once your test code has access to the action it can simply call its trigger trigger() method.

这篇关于如何使用QtTest库访问QAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆