简体   繁体   English

Qt 中多个 windows 的应用

[英]Application with multiple windows in Qt

What is a good way to create application with multiple windows in Qt?在 Qt 中创建具有多个 windows 的应用程序的好方法是什么? Something like GIMP.像 GIMP 之类的东西。 I have already read this answer and it is not what I need.我已经阅读了这个答案,这不是我需要的。

I have two windows, one with controls and one with OpenGL inside.我有两个 windows,一个带有控件,一个带有 OpenGL。 Right now I have something like this:现在我有这样的事情:

int main()
{
    // some code

    MainWindow mw;
    mw.show();

    GLWindow gw;
    gw.show();

    // ...
}

I don't like this for two reasons.我不喜欢这个有两个原因。 First reason is when I start my application, window with OpenGL will be on top (because it is last to call show() ) but MainWindow will be buried somewhere under all opened windows.第一个原因是当我启动我的应用程序时,带有 OpenGL 的 window 将位于顶部(因为它是最后一个调用show() )但MainWindow将被埋在所有打开的 Z0F4137ED1502B5045D6083AA258B54C 下的某个地方What I need is both windows in front of everything (like GIMP), preferably with focus on MainWindow (I guess I can bring them to front, so that is minor issue).我需要的是 windows 在所有东西(如 GIMP)的前面,最好关注MainWindow (我想我可以把它们放在前面,所以这是小问题)。 Second reason I don't like this is that my application will be closed completely only when I close both windows.我不喜欢这个的第二个原因是,只有当我关闭 windows 时,我的应用程序才会完全关闭。

So I was thinking of having a reference to GLWindow inside MainWindow , and creating it from MainWindow .所以我想在MainWindow中引用GLWindow ,并MainWindow创建它。

Would that be a good way to create application with several windows?这是用几个 windows 创建应用程序的好方法吗?

EDIT: GLWindow inherits from QOpenGLWindow.编辑:GLWindow 继承自 QOpenGLWindow。

I'm not sure I completely understand the situation but, typically, you can make the 'secondary' widgets child dialogs of the QMainWindow .我不确定我是否完全理解这种情况,但通常情况下,您可以制作QMainWindow的“辅助”小部件子对话框 Given your example code that would be something like...鉴于您的示例代码类似于...

int main ()
{
    // some code

    MainWindow mw;
    mw.show();

    /*
     * Secondary windows should be created with the QMainWindow mw
     * as their parent.
     */
    GLWindow gw(&mw);

    /*
     * Set the Qt::Dlaog window flag to `encourage' the QMainWindow
     * and its children to behave as a group.
     */
    gw.setWindowFlags(gw.windowFlags() | Qt::Dialog);
    gw.show();

    // ...
}

Now all widgets behave as a group (possibly subject to the window manager being used) and closing the QMainWindow will, by default, close the application.现在所有小部件都表现为一个组(可能取决于正在使用的 window 管理器),默认情况下,关闭QMainWindow将关闭应用程序。

You are doing right, but with the following simple tricks, you can resolve both issues that cause you do not like your right method:您做对了,但是通过以下简单的技巧,您可以解决这两个导致您不喜欢正确方法的问题:

  1. To activate both windows, just do as follows:要同时激活 windows,只需执行以下操作:
    MainWindow mw;
    mw.show();
    mw.activateWindow();

    GLWindow gw;
    gw.show();
    gw.activateWindow();
  1. To resolve the quit problem, you have to override the closeEvent in both windows.要解决退出问题,您必须覆盖 windows 中的closeEvent To do that, add the following code into the header file of your both windows:为此,请将以下代码添加到 windows 的header文件中:
protected:
    void closeEvent(QCloseEvent *event) override;

and in the implementation of the closeEvent, just write qApp->quit();而在closeEvent的实现中,只需要写qApp->quit(); . . This way once you close either window, your application will terminate completely.这样,一旦您关闭 window,您的应用程序将完全终止。

MainWindow.cpp主窗口.cpp

void MainWindow::closeEvent(QCloseEvent *event)
{
    qApp->quit();
}

and

GLWindow.cpp GLWindow.cpp

void GLWindow::closeEvent(QCloseEvent *event)
{
    qApp->quit();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM