Qt-qdialog,qwidget for modal and non-modal

Source: Internet
Author: User

Reprinted from: http://www.360doc.com/content/15/0825/16/20016257_494656595.shtml

In Qt, Qdialog is "window", while Qwidget is "part", the first is to understand the concept of QT window and parts. For Qdialog modal and non-modal is directly achievable, many textbooks will be mentioned, summarized here.
mode Qdialog mode one:

Qdialog Dlg (this); Dlg.exec (); Way two:

Qdialog *pdlg=new Qdialog (this); Pdlg->setmodal (TRUE); Pdlg->show (); non-modal qdialog

Qdialog *pdlg=new Qdialog (this); Pdlg->show ();
Qdialog to realize modal non-modal is very simple, but for qwidget a bit confused, Qwidget no exec (), nor setmodal () way, but think about it, qwidget as the base class Qdialog, and Qwidget as a "window" Use is also common, so you will realize whether there is a relative exec () or setmodal () more basic operation in Qwidget to achieve modal and non-modal. So, I found setwindowmodality (), this function is used to set the Qwidget runtime program blocking mode, the parameters are interpreted as follows: QT:: nonmodal does not block QT:: windowmodal blocks the parent window, all ancestor windows and their child windows Qt:: applicationmodal blocking the entire application
It appears that Setmodal() is using setwindowmodality() to set QT:: applicationmodal parameter also implements the modal.
So, to implement Qwidget modal and non-modal, just call setwindowmodality() to set the blocking type:

Qwidget *pwid = new Qwidget (this);
Pwid->setwindowmodality (qt::applicationmodal);
Pwid->setattribute (Qt::wa_showmodal, true);
Pwid->show ();
However, the run discovery does not implement a modal effect. It is important to note that when you want to use setwindowmodality() when setting Qwidget to modal, you should ensure that the Qwidget parent part is 0, modify qwidget *pwid = new Qwidget (this), and qwidget *pwid = new Qwidget (NULL);
In addition, by setwindowmodality() Setting modal Windows is not the only way to set part (or window) properties directly:

Pwid->setattribute (Qt::wa_showmodal, True)---------------there are many places to be aware of when Qdialog is created using Setwindowflags (Qt:: Framelesswindowhint); When you remove the title bar, this dialog box no longer blocks the parent window, and if you need to implement a blocking effect, you can specify Qt again::D Ialog, even with: setwindowflags (Qt:: dialog  | qt:: framelesswindowhint ); This will block the parent window. However, this affects the translucent (or transparent) display of the dialog box. Use QT::D ialog before the translucent display is normal: after use, but die:--do not know how to be good.  -------------- in summary whether modal and Qdialog and Qwidget are both modal and non-modal. EXEC (), show ()   Other functions have no direct relationship, only related to window properties, Use the following two ways: SetAttribute (Qt::wa_showmodal, true);//Property Set Setwindowmodality (Qt::applicationmodal);//Set Blocking type--------- The member function in the--------Qdialog Setmodal (true) and exec () is modal because he first sets the window properties: SetAttribute () and then show (see Source).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.