C++/QT:;“运行”;行动 我需要使用Qt、C++、QTestin(在Eclipse中)实现GUI测试自动化 我有一个动态创建的菜单,其中包含动态创建的QActions,我需要从中测试一个“new tab”QAction(在菜单内),这就是创建QAction的方式: m_pNewTabAction = new QAction(QIcon(":/images/add.png"), tr("&New Tab"), this); m_pNewTabAction->setShortcut(tr("Ctrl+N")); m_pNewTabAction->setStatusTip(tr("Open a new tab")); connect(m_pNewTabAction, SIGNAL(triggered()), this, SLOT(NewTab()));

C++/QT:;“运行”;行动 我需要使用Qt、C++、QTestin(在Eclipse中)实现GUI测试自动化 我有一个动态创建的菜单,其中包含动态创建的QActions,我需要从中测试一个“new tab”QAction(在菜单内),这就是创建QAction的方式: m_pNewTabAction = new QAction(QIcon(":/images/add.png"), tr("&New Tab"), this); m_pNewTabAction->setShortcut(tr("Ctrl+N")); m_pNewTabAction->setStatusTip(tr("Open a new tab")); connect(m_pNewTabAction, SIGNAL(triggered()), this, SLOT(NewTab()));,c++,qt,C++,Qt,在我的testclass中,我使用“findChildren”函数成功地访问了私有QAction对象(m_pNewTabAction),现在我不知道如何“执行”QAction(或者说“添加新选项卡”) 我的testclass: //Get the actions available for the filemenu QList<QAction *> fileactions = filemenu->findChildren<QAction *>();

在我的testclass中,我使用“findChildren”函数成功地访问了私有QAction对象(m_pNewTabAction),现在我不知道如何“执行”QAction(或者说“添加新选项卡”) 我的testclass:

    //Get the actions available for the filemenu
    QList<QAction *> fileactions = filemenu->findChildren<QAction *>();
    //Execute an action??
    fileactions.front()-> //how do I execute my QAction?
//获取可用于文件菜单的操作
QList fileactions=filemenu->findChildren();
//执行一个动作??
fileactions.front()->//如何执行QAction?

我相信您正在寻找:


ActionEvent
QAction::Trigger
QAction::Hover
之一。您可能需要
QAction::Trigger

如果菜单项列表是动态填充的,您可能需要对需要使用findChild()或findChildren()查找的任何项调用setObjectName()。添加以下内容:

m_pNewTabAction->setObjectName("NewTabAction"); 
(字符串不需要tr(),因为它只是内部的。)然后在测试中,使用findChild()并调用:

QVERIFY(文件菜单);
QAction*action=filemenu->findChild(“NewTabAction”);
//或者,如果名称是唯一的,您也可以从主窗口中查找:
//QAction*action=mainWindow->findChild(“NewTabAction”);
验证(行动);
动作->触发器();

从代码运行QAction,将我的代码分成一部分

void MainWindow::on_pushButtonExit_clicked()
{        
    quitAction = new QAction(this);
    connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
    quitAction->trigger();
}

这也是我的想法,但它不起作用:我尝试了触发器和悬停,但函数“NewTab()”似乎没有在任何时候被调用,换句话说:没有新的选项卡提交up@RandomDisplayName那一定是另一个问题,在你没有展示的代码中。的Qt文档显式声明在调用
activate(Trigger)
时调用它。作为一种理智检查,当用户触发该操作时,该选项卡是否会出现?我最大的担心刚刚成为现实。不管怎样,谢谢你的帮助,我会接受你的回答,因为它在技术上确实回答了这个问题,但是代码太大了,无法显示here@RandomDisplayName您确定触发了正确的操作吗?也许你应该创建一个方法来返回你想要的
QAction
对象,而不是使用
findChildren
方法。我必须在文件菜单中测试5个QAction,findChildren返回5个对象,我可以从中获取任何一个并进行测试,但它们都不起作用,所以我猜正如Angew所提到的,它的另一个问题这没有太大变化,我的意思是我现在得到一个具有正确对象名的QAction,但这并不能解决trigger()的问题;不起作用。正如Angew之前提到的,它必须在代码中的其他地方。这应该可以工作,所以如果不工作,您还有一些其他问题。我添加了几个QVERIFY()调用,可能会帮助您诊断问题。+1对于findChild的澄清,我现在用另一种方法解决了它(解决方案与我的问题没有任何共同之处,因此我不会发布),感谢您的帮助
QVERIFY( filemenu );
QAction* action = filemenu->findChild<QAction*>( "NewTabAction" );
// OR you could look it up from the main window if the names are unique: 
//    QAction* action = mainWindow->findChild<QAction*>( "NewTabAction" );
QVERIFY( action );
action->trigger();
void MainWindow::on_pushButtonExit_clicked()
{        
    quitAction = new QAction(this);
    connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
    quitAction->trigger();
}