C++/QT:"run"一个 QAction
C++/QT: "run" a QAction
我需要使用 qt,c++,qtest 自动执行 gui 测试(在 eclipse 中(我有一个动态创建的菜单,其中包含动态创建的QActions,我需要从中测试一个"新选项卡"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()));
在我的测试类中,我设法使用"findChildren"函数访问私有 QAction 对象 (m_pNewTabAction(,现在我不知道如何"执行"QAction(或者换句话说"添加新选项卡"(我的测试类:
//Get the actions available for the filemenu
QList<QAction *> fileactions = filemenu->findChildren<QAction *>();
//Execute an action??
fileactions.front()-> //how do I execute my QAction?
我相信
你正在寻找QAction::activate()
:
void QAction::activate(ActionEvent event)
ActionEvent
是QAction::Trigger
或QAction::Hover
之一。你可能想要QAction::Trigger
.
如果菜单项列表是动态填充的,则可能需要对需要使用findChild((或findChildren((查找的任何项调用setObjectName((。添加这个:
m_pNewTabAction->setObjectName("NewTabAction");
(该字符串不需要 tr((,因为它只是内部的。然后在测试中,使用 findChild(( 然后调用 QAction::trigger((:
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();
从代码运行 QAction,部分我的代码...
void MainWindow::on_pushButtonExit_clicked()
{
quitAction = new QAction(this);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
quitAction->trigger();
}
相关文章:
- 触发时无法将 QAction 链接到函数 (Qt 5)
- join() 失败,如果在线程内部调用 io_context.run()
- 为什么我会收到"Run-Time Check Failure #2 - Stack around the variable 'pr' was corrupted"错误?
- C++ POCO - 如何在不使用 run() 方法的情况下启动线程池上的线程?
- 将 QAction 的 QString 文本作为参数传递
- TensorFlow c++ API 在 session->run() 上崩溃
- 如何在QT Creator中手动运行CMake和Run
- 使用 QtConcurrent::run() 修改成员变量?
- 如何在QtConcurrent::run中启动QTimer或为什么QVector<QTimer*>不起作用
- std::condition_variable 在 QThread::run() 中的用法
- "Make run"在终端中打印"bin/prog"
- 调用 dll 函数时"Run-Time Check Failure #0 - The value of ESP"
- Bash run 命令在子壳的后台运行
- Bazel-run 不会加载在 Bazel Build 时加载的所有 TensorFlow 包
- 如何将字符串数组传递到 QtConcurrent::run 中?
- 提升::亚洲.确保仅在调用async_receive(..)后调用io_service.run()?
- "Feature X requires run-time support"的含义是什么?
- 如何传递以 "size determined at run time" 作为引用的动态分配数组?
- 如何将Qmenu中的QACTION转换为Qwidget
- C++/QT:"run"一个 QAction