{"id":11707,"date":"2021-02-02T09:10:09","date_gmt":"2021-02-02T09:10:09","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=11707"},"modified":"2021-02-02T09:50:23","modified_gmt":"2021-02-02T09:50:23","slug":"qt-popup-overlay-window","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=11707","title":{"rendered":"Qt Popup\/Overlay Window"},"content":{"rendered":"<p><a href=\"https:\/\/doc.qt.io\/qt-5\/qapplication.html#activeModalWidget\"><code>QApplication::activeModalWidget()<\/code><\/a><br \/>\nA modal widget is a special top-level widget which is a subclass of QDialog that specifies the modal parameter of the constructor as true. A modal widget must be closed before the user can continue with other parts of the program.<\/p>\n<p><a href=\"https:\/\/doc.qt.io\/qt-5\/qapplication.html#activePopupWidget\"><code>QApplication::activePopupWidget()<\/code><\/a><br \/>\nA popup widget is a special top-level widget that sets the <code>Qt::WType_Popup<\/code> widget flag, e.g. the <code>QMenu <\/code>widget. When the application opens a popup widget, all events are sent to the popup. Normal widgets and modal widgets cannot be accessed before the popup widget is closed.<\/p>\n<hr>\n<p><!-- ------------------------------------------------------------------------------------------------------------ --><\/p>\n<h4>Questions<\/h4>\n<p><a href=\"https:\/\/forum.qt.io\/topic\/85056\/custom-popup-widget\">Custom popup widget<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/56955960\/how-would-i-go-about-making-a-overlay-widget\">How would I go about making a overlay widget<\/a><\/p>\n<h4>Code<\/h4>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid QCompleter::setPopup(QAbstractItemView *popup)\r\n{\r\n    Q_D(QCompleter);\r\n    Q_ASSERT(popup != nullptr);\r\n    if (d-&gt;popup) {\r\n        QObject::disconnect(d-&gt;popup-&gt;selectionModel(), nullptr, this, nullptr);\r\n        QObject::disconnect(d-&gt;popup, nullptr, this, nullptr);\r\n    }\r\n    if (d-&gt;popup != popup)\r\n        delete d-&gt;popup;\r\n    if (popup-&gt;model() != d-&gt;proxy)\r\n        popup-&gt;setModel(d-&gt;proxy);\r\n     popup-&gt;hide();\r\n\r\n    Qt::FocusPolicy origPolicy = Qt::NoFocus;\r\n    if (d-&gt;widget)\r\n        origPolicy = d-&gt;widget-&gt;focusPolicy();\r\n\r\n    \/\/ Mark the widget window as a popup, so that if the last non-popup window is closed by the\r\n    \/\/ user, the application should not be prevented from exiting. It needs to be set explicitly via\r\n    \/\/ setWindowFlag(), because passing the flag via setParent(parent, windowFlags) does not call\r\n    \/\/ QWidgetPrivate::adjustQuitOnCloseAttribute(), and causes an application not to exit if the\r\n    \/\/ popup ends up being the last window.\r\n    popup-&gt;setParent(nullptr);\r\n    popup-&gt;setWindowFlag(Qt::Popup);\r\n    popup-&gt;setFocusPolicy(Qt::NoFocus);\r\n    if (d-&gt;widget)\r\n        d-&gt;widget-&gt;setFocusPolicy(origPolicy);\r\n\r\n    popup-&gt;setFocusProxy(d-&gt;widget);\r\n    popup-&gt;installEventFilter(this);\r\n    popup-&gt;setItemDelegate(new QCompleterItemDelegate(popup));\r\n#if QT_CONFIG(listview)\r\n    if (QListView *listView = qobject_cast&lt;QListView *&gt;(popup)) {\r\n        listView-&gt;setModelColumn(d-&gt;column);\r\n    }\r\n#endif\r\n\r\n    QObject::connect(popup, SIGNAL(clicked(QModelIndex)),\r\n                     this, SLOT(_q_complete(QModelIndex)));\r\n    QObject::connect(this, SIGNAL(activated(QModelIndex)),\r\n                     popup, SLOT(hide()));\r\n\r\n    QObject::connect(popup-&gt;selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),\r\n                     this, SLOT(_q_completionSelected(QItemSelection)));\r\n    d-&gt;popup = popup;\r\n}\r\n\r\nvoid QCompleterPrivate::showPopup(const QRect&amp; rect)\r\n{\r\n    const QRect screen = QWidgetPrivate::availableScreenGeometry(widget);\r\n    Qt::LayoutDirection dir = widget-&gt;layoutDirection();\r\n    QPoint pos;\r\n    int rh, w;\r\n    int h = (popup-&gt;sizeHintForRow(0) * qMin(maxVisibleItems, popup-&gt;model()-&gt;rowCount()) + 3) + 3;\r\n    QScrollBar *hsb = popup-&gt;horizontalScrollBar();\r\n    if (hsb &amp;&amp; hsb-&gt;isVisible())\r\n        h += popup-&gt;horizontalScrollBar()-&gt;sizeHint().height();\r\n\r\n    if (rect.isValid()) {\r\n        rh = rect.height();\r\n        w = rect.width();\r\n        pos = widget-&gt;mapToGlobal(dir == Qt::RightToLeft ? rect.bottomRight() : rect.bottomLeft());\r\n    } else {\r\n        rh = widget-&gt;height();\r\n        pos = widget-&gt;mapToGlobal(QPoint(0, widget-&gt;height() - 2));\r\n        w = widget-&gt;width();\r\n    }\r\n\r\n    if (w &gt; screen.width())\r\n        w = screen.width();\r\n    if ((pos.x() + w) &gt; (screen.x() + screen.width()))\r\n        pos.setX(screen.x() + screen.width() - w);\r\n    if (pos.x() &lt; screen.x())\r\n        pos.setX(screen.x());\r\n\r\n    int top = pos.y() - rh - screen.top() + 2;\r\n    int bottom = screen.bottom() - pos.y();\r\n    h = qMax(h, popup-&gt;minimumHeight());\r\n    if (h &gt; bottom) {\r\n        h = qMin(qMax(top, bottom), h);\r\n\r\n        if (top &gt; bottom)\r\n            pos.setY(pos.y() - h - rh + 2);\r\n    }\r\n\r\n    popup-&gt;setGeometry(pos.x(), pos.y(), w, h);\r\n\r\n    if (!popup-&gt;isVisible())\r\n        popup-&gt;show();\r\n}\r\n<\/pre>\n<hr>\n<p><!-- ------------------------------------------------------------------------------------------------------------ --><\/p>\n<h1>Transparent\/Alpha Background<\/h1>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/10450902\/qdialog-with-transparent-background-color\">QDialog with transparent background color<\/a><br \/>\n<a href=\"https:\/\/www.kdab.com\/how-not-to-lose-the-alpha-channel\/\">How not to lose the alpha channel<\/a><\/p>\n<hr>\n<p><!-- ------------------------------------------------------------------------------------------------------------ --><\/p>\n<h1>QML<\/h1>\n<h4>Documentation<\/h4>\n<p><a href=\"https:\/\/doc.qt.io\/qt-5\/qtquickcontrols2-popups.html\">Qt 5.15 &#8211; Qt Quick Controls &#8211; Popup Controls<\/a><\/p>\n<h4>Questions<\/h4>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/41526665\/popup-item-loses-opaquness\/41534424\">Popup item loses opaquness<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/59120600\/how-to-set-popup-background-color-to-transparent\">how to set popup background color to transparent<\/a><br \/>\n<a href=\"https:\/\/forum.qt.io\/topic\/109355\/change-popup-background-to-transparent\">Change popup background to transparent<\/a><br \/>\n<a href=\"https:\/\/forum.qt.io\/topic\/84163\/overlaying-qwidgets\/9\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>QApplication::activeModalWidget() A modal widget is a special top-level widget which is a subclass of QDialog that specifies the modal parameter of the constructor as true. A modal widget must be closed before the user can continue with other parts of the program. QApplication::activePopupWidget() A popup widget is a special top-level widget that sets the Qt::WType_Popup [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11707","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/11707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11707"}],"version-history":[{"count":4,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/11707\/revisions"}],"predecessor-version":[{"id":11715,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/11707\/revisions\/11715"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}