问题描述
我正在开发一个项目,该项目需要调用带有工具栏的模式窗口,以便在加载之前对某些数据进行一些处理.我需要工具栏的原因是用户有几个不同的可能选项可以组合.
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
QMainWindow
modal?
推荐答案
如果你不需要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
QToolBar
to aQDialog
in the QMainWindow::addToolBar() sense, becauseQDialog
inherits onlyQWidget
and 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 QMainWindow
s 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 中添加一个工具栏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!