你能添加一个工具栏到QDialog吗? [英] Can you add a toolbar to 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
modal?
- 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?
推荐答案
-
>
可以直接在
只继承QDialog
中添加QToolBar
QDialogQWidget
而不是QMainWindow
,因为没有方法addToolBar()
)
It is not directly possible to add a
QToolBar
to aQDialog
in the sense thatQDialog
inherits onlyQWidget
and notQMainWindow
, as you noted (hence do not have the methodaddToolBar()
)
您不能创建 QMainWindow
modal,但您可以在 QDialog
中插入 QMainWindow
:
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吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!