Can you add a toolbar to QDialog?(你可以在 QDialog 中添加一个工具栏吗?)
问题描述
我正在开发一个项目,该项目需要调用带有工具栏的模式窗口,以便在加载之前对某些数据进行一些处理.我需要工具栏的原因是用户有几个不同的可能选项可以组合.
I'm working on a project that needs to call a modal window with a toolbar to do some work on some data before it's loaded. The reason I need the toolbar is the user has a few different possible options that can be combined.
这里明显的选择是模态对话框(我现在正在使用它).问题是我想要一个工具栏.这是一个两部分的问题:
The obvious choice here is a Modal dialog (which I have working right now). The issue is I want a toolbar. This is a two part question:
- 是否可以在
QDialog 中添加工具栏?(在 Qt Designer 中也可以这样做吗?) - 如果 1. 不可能,我该如何制作
QMainWindow模态?
- Is it possible to add a toolbar to a
QDialog? (also is it possible to do this in Qt Designer?) - If 1. is not possible, how can I make a
QMainWindowmodal?
推荐答案
如果你不需要QMainWindow的工具栏的内置拖放功能,你可以简单地在任何布局中添加一个QToolBar,包括QDialog的layout().有关详细信息,请参阅下面的 DigviJay Patil 的回答,这在概念上绝对更简洁.
If you don't need the built-in drag and drop feature of QMainWindow's toolbars, you can simply add a QToolBar to any layout, including QDialog's layout(). See the DigviJay Patil's answer below for details, which is definitely cleaner conceptually.
否则,请继续阅读.
不可能直接在 QMainWindow::addToolBar() 意义上将
QToolBar添加到QDialog,因为QDialog仅继承QWidget而不是QMainWindow,正如您所指出的(因此没有方法addToolBar())
It is not directly possible to add a
QToolBarto aQDialogin the QMainWindow::addToolBar() sense, becauseQDialoginherits onlyQWidgetand notQMainWindow, as you noted (hence do not have the methodaddToolBar())
你不能创建一个 QMainWindow 模态,但是你可以在 QDialog 中插入一个 QMainWindow:p>
You can't make a QMainWindow modal, but you can insert a QMainWindow in a QDialog this way:
代码:
MyDialog::MyDialog() :
QDialog()
{
QMainWindow * mainWindow = new QMainWindow(); // or your own class
// inheriting QMainWindow
QToolBar * myToolBar = new QToolBar();
mainWindow->addToolBar(myToolBar);
QHBoxLayout * layout = new QHBoxLayout();
layout->addWidget(mainWindow);
setLayout(layout);
}
确实,QMainWindow 不一定是顶级小部件,您甚至可以插入多个 QMainWindow 作为单个小部件的子级(可能但这不是最明智的选择,因为用户可能会对单独的菜单栏、工具栏、停靠小部件等集感到困惑).
Indeed, a QMainWindow doesn't necessarily have to be a top-level widget, and you can even insert several QMainWindows as children of a single widget (may not be the wisest choice though, as the user would probably be confused with the separate sets of menu bars, toolbars, dock widgets, etc.).
这篇关于你可以在 QDialog 中添加一个工具栏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:你可以在 QDialog 中添加一个工具栏吗?
基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
