Qt setWindowFlags (Qt :: Window) function code parses non-modal windows and top

If you need a window to support modal and non-modal windows, you generally need to change the window to QDailog,

A key point to achieve this function is to set the setStyleSheet (Qt :: Window), the source code is as follows:

        Widget = 0x00000000,

        Window = 0x00000001,

        Dialog = 0x00000002 | Window,

 

If you need a window to support modal and non-modal windows, you generally need to change the window to QDailog,

A key point to achieve this function is to set the setStyleSheet (Qt :: Window), the source code is as follows:

        Widget = 0x00000000,

        Window = 0x00000001,

        Dialog = 0x00000002 | Window,

 

Qt::Dialog

0x00000002 | Window

Indicates that the widget is a window that should be decorated as a dialog (i.e., typically no maximize or minimize buttons in the title bar). This is the default type for QDialog. If you want to use it as a modal dialog, it should be launched from another window, or have a parent and used with the QWidget::windowModality property. If you make it modal, the dialog will prevent other top-level windows in the application from getting any input. We refer to a top-level window that has a parent as a secondary window.

 

 

Can QDialog be used to realize the function of modal and non-modal dialog boxes, is there any other way to achieve similar functions, can QWidget be used to achieve similar effects, can QWidget be used to achieve the function of window top and non-modal dialog boxes , If the requirement does not require a modal dialog box, only the top of the window and non-modal window are needed, you can use QDialog to achieve similar functions, or you can use QWidget. QWidget itself does not have such a function by default. Can be achieved, you can set QWidget :: setWindowsFlags (this-> windowsFlags () | Qt :: Window ), you can have such a function.

After setting the Qt :: Window flag, the window has the function of always sticking to the top and is a function of a non-modal dialog box. So if the demand is like this, you can use either QDialog or QWindow. You can set Qt :: Window. You do n’t need to set the window to stay on top. Similar windowFlags can also implement non-modal dialogs and window on top. Function. Of course, if the requirement is a modal dialog box, it is recommended to use the ready-made packaged QDialog.

If you need to implement the modal dialog box function, but the window effect is indeed non-modal, you can use QWidget, QWidget :: setWindowsFlags (this-> windowsFlags () | Qt :: Window ) , you can achieve the window effect of the modal dialog box , Plus use

     show();
     QEventLoop evtLoop;
     m_pEvtLoop = &evtLoop;
     evtLoop.exec();

Then the custom OK button of the non-modal window that pops up can be done as follows

void ColorDialog::okBtnClickedSlot()

{

    m_buttonRole = Yes;

    if (m_pEvtLoop != NULL)

    {

        m_pEvtLoop->exit();

    }

    this->hide();

}

 

void ColorDialog::cancelBtnClickedSlot()

{

    m_buttonRole = No;

    if (m_pEvtLoop != NULL)

    {

        m_pEvtLoop->exit();

    }

    this->hide();

}

 

   

 

 

确实可以使用QDialog实现模态和非模态对话框的功能,还有其他方法实现类似的功能吗,可以使用QWidget实现类似的效果吗,可以使用QWidget实现窗口置顶且为非模态对话框的功能,如果需求不要求是模态对话框,只需要窗口置顶和非模态窗口,可以使用QDialog实现类似的功能,也可以使用QWidget实现,QWidget本身默认是没有这样的功能的需要多做一些设置就可以实现了,可以设置QWidget::setWindowsFlags(this->windowsFlags() | Qt::Window),就可以有这样的功能。

设置了Qt::Window标志之后,窗口就具有一直置顶的功能,且为非模态对话框的功能。所以如果需求是这样的话,既可以使用QDialog实现,也可以使用QWindow实现,设置Qt::Window就可以了,不需要再设置窗口一直置顶等类似的windowFlags也可以实现非模态对话框且窗口置顶的功能。当然如果需求要求是模态对话框,建议还是用现成封装好的QDialog就可以了。

如果需要实现模态对话框功能,但是窗口效果确实非模态的,可以使用QWidget,QWidget::setWindowsFlags(this->windowsFlags() | Qt::Window),就可以实现模态对话框的窗口效果,再加上使用

     show();
     QEventLoop evtLoop;
     m_pEvtLoop = &evtLoop;
     evtLoop.exec();

然后再弹出的非模态窗口的自定义确定取消按钮做如下操作就可以了

void ColorDialog::okBtnClickedSlot()

{

    m_buttonRole = Yes;

    if (m_pEvtLoop != NULL)

    {

        m_pEvtLoop->exit();

    }

    this->hide();

}

 

void ColorDialog::cancelBtnClickedSlot()

{

    m_buttonRole = No;

    if (m_pEvtLoop != NULL)

    {

        m_pEvtLoop->exit();

    }

    this->hide();

}

发布了85 篇原创文章 · 获赞 18 · 访问量 12万+

Guess you like

Origin blog.csdn.net/a1317338022/article/details/105514311