Qt4:将 QMainWindow 实例放置在其他 QWidget/QMainWindow 中 [英] Qt4: Placing QMainWindow instance inside other QWidget/QMainWindow

查看:53
本文介绍了Qt4:将 QMainWindow 实例放置在其他 QWidget/QMainWindow 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 QMainWindow 实例放在另一个 QWidget 中(例如另一个 QMainWindow 的 centralWidget).

I'd like to place QMainWindow instance inside another QWidget (for example centralWidget of another QMainWindow).

我想知道为什么它不起作用?QMainWindow 直接继承自 QWidget.将 QWidget 放置在另一个 QWidget 中可以正常工作.

I'm wondering why it doesn't work ? QMainWindow inherits directly from QWidget. Placeing QWidget inside another QWidget works fine.

我经常将 QMainWindow 实例放在 QTabBar 中,没有任何问题.

I often place QMainWindow instances in QTabBar without any problems.

ps 你可能会问为什么我需要使用 QMainWindow ?我想使用垂直布局在 1 个表单中放置 2 个小部件.我希望两个小部件都直接在它们上面有单独的工具栏.

ps You may ask why do I need to use QMainWindow ? I want to place 2 widgets inside 1 form using vertical layout. I want both widgets to have seperate Toolbars directly over them.

也许还有其他方法可以使用 QtCreator 将工具栏放置在普通 QWidget 中?

Maybe there is some other way to place toolbars inside plain QWidgets using QtCreator ?

编辑

第一个例子(工作正常)

我基于 QWidget 创建了新的类/表单.(QtCreator 基于标准模板创建 3 个文件 *.cpp、*.h 和 *.ui).

I create new class/form based on QWidget. (QtCreator creates 3 files *.cpp, *.h and *.ui based on standard templates).

类声明如下

class NotesEditor : public QWidget
{
    Q_OBJECT

public:
    explicit NotesEditor(QWidget *parent = 0);
    ~NotesEditor();

private:
    Ui::NotesEditor *ui;
};

当我尝试使用这个小部件并将其放置在另一个小部件中时,它工作正常.我使用了 qtcreator 的提升到..."功能 - 这里没有问题.

When I try to use this widget and place it inside another widget it works fine. I used "promote to ..." feature of qtcreator - no problems here.

第二个例子(不起作用)

我基于 QMainWindow 创建了新的类/表单.(QtCreator 基于标准模板创建 3 个文件 *.cpp、*.h 和 *.ui).

I create new class/form based on QMainWindow. (QtCreator creates 3 files *.cpp, *.h and *.ui based on standard templates).

类声明如下所示:

class Notes : public QMainWindow
{
    Q_OBJECT

public:
    explicit Notes(QWidget *parent = 0);
    ~Notes();

private:
    Ui::Notes *ui;
};

现在,当我尝试将这个小部件放在另一个小部件中时,它是不可见的.和之前我使用 qtcreator 的promote to ..."功能一样.

And now when I try to place this widget in another widget its not visible. Same as before I used "promote to ..." feature of qtcreator.

两个小部件(第一个基于 QWidget,第二个基于 QMainWindow)具有基于标准 qtcreator 代码模板的相同默认结构.我在这里没有做太多更改 - 只是在表单设计器中添加了一些按钮.

Both widgets (first based on QWidget, second based on QMainWindow) have the same default structure based on standard qtcreator code templates. I didn't change much here - just added some buttons in the form designer.

在第二个示例中,我尝试在类实例上使用 setEnabled(true) 和 setVisible(true).第一个没有结果.第二个在单独的窗口中打开这个小部件.

In the second example I tried to use setEnabled(true) and setVisible(true) on the class instance. The first one gives no results. The second one opens this widget in seperate window.

我认为最大的问题是 QMainWindow 嵌套在另一个 QWidget 中的可能性.正如我之前写的,QMainWindow 实例可以毫无问题地放置在 QTabWidgets 中.

I think that the big question is what probibits QMainWindow to be nested inside another QWidget. As I wrote before QMainWindow instances can be placed inside QTabWidgets without any problems.

推荐答案

遇到同样的问题,我找到了解决方案 此处.

Having the same problem, I found the solution here.

QMainWindow 将它的窗口类型设置为 Qt::Window,这样即使它有一个父窗口,它也会在一个独立的窗口中(你可以通过在你的 QMainWindow 上调用 show() 来确认这一点,你会在它自己的窗口中看到它窗户).尝试添加行

QMainWindow sets its window type to Qt::Window so that it will be in an independent window even if it has a parent (you can confirm this by calling show() on your QMainWindow, and you will see it in its own window). Try adding the line

window->setWindowFlags(Qt::Widget);

在你构建 QMainWindow 之后.

after you construct the QMainWindow.

这篇关于Qt4:将 QMainWindow 实例放置在其他 QWidget/QMainWindow 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆