qt - How to add custom QWidget to the QMenubar as action which can be toggled?
问题描述
I have an Widget class as a QtPlugin. In my main application i want to load this widget and add to menubar as toggledaction, so that later if i close the widget i can open it via the Menu bar. How can this be achieved.
As this is not a docketwidget i cannot use the following:
ui->menuPlugins->addAction(dockedWidget->toggleViewAction());
I have tried something like this:
void MainWindow::addToPluginsMenu(QWidget *const widget) {
ui->menuPlugins->setEnabled(true);
QWidgetAction *ac = new QWidgetAction(this);
ui->menuPlugins->addAction(ac);
}
An action is added but it is empty and cannot be toggled. Could anyone provide me a solution?
Thank you
解决方案
为了添加切换操作并在切换操作后保持它们可见。
使用setDefaultWidget函数
例如:
// Create menu
QMenu * poMainMenu = new QMenu(this);
// toggled widget
QCheckBox * poCbTest = new QCheckBox("toggle",this);
// set the toggle widget as menu action
QWidgetAction *ac = new QWidgetAction(this);
ac->setDefaultWidget(poCbTest);
// add the action to the menu
poMainMenu->addAction(ac);
推荐阅读
- sql - 经纬度半径范围内的oracle搜索
- google-drive-api - 访问 Google Drive 上公共文件的元数据时出现 404 错误
- javascript - textarea 模式属性中的正则表达式以允许组合字母数字和特殊字符
- ruby-on-rails - ActiveAdmin 自定义列排序
- amazon-web-services - 如何在不创建资产的情况下从 CDK 引导程序获取 s3BucketName?
- git - 查找哪些 git 分支已经合并到分支 x,但还没有在 master 中?
- android - 具有 2 种可能布局的 ViewBinding
- python - 使用 ftplib 并行化 FTP 请求 (MLSD)
- javascript - 显示带有从 react.js 中的表格元素带来的信息的弹出窗口
- r - 如何在ggplot2中绘制超额收益?