Floating sub QMainWindow (QMainWindow as child wid

2019-07-19 06:38发布

I use a QMainWindow as child of my main QMainWindow. By that I get an other area which I can use for dockable widgets (QDockWidget).

According to the following posts this is OK, it also works perfectly for me.

  1. https://qt-project.org/forums/viewthread/17519
  2. http://www.qtcentre.org/threads/12569-QMainWindow-as-a-child-of-QMainWindow

To make the QMainWindow behaving as a normal widget, I unset the window flag, this trick is mentioned in one of the posts above.

Now I also want to be able to float this child QMainWindow with all its docked widgets. In other words, I want to revert the step "making it a normal widget". Unfortunately, this does does not work. It is gone from the main window, but not visible at all.

Any way to resolve it?

// this is the child QMainWindow
if (this->m_infoAreaFloating)
{
    // this should give me a floating window besides the main window
    this->setWindowFlags(Qt::Desktop);
    this->show();
}
else
{
    // make this compliant as QWidget
    this->setWindowFlags(this->windowFlags() & ~Qt::Window);
}

Related: a , b

1条回答
Deceive 欺骗
2楼-- · 2019-07-19 07:43

The Qt::Desktop flag is not something you are supposed to set by yourself.

You need to set the Qt::Window flag:

setWindowFlags(m_infoAreaFloating ? Qt::Window : Qt::Widget);
show();

There's no point to this->windowFlags() & ~Qt::Window: you've cleared all other window flags when setting the lone Qt::Window flag. You're in full control of the flags, there's no need to preserve some "other" flags: there are none.

查看更多
登录 后发表回答