久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Qt 中 QMainWindow 上的深色透明層

Dark transparent layer over a QMainWindow in Qt(Qt 中 QMainWindow 上的深色透明層)
本文介紹了Qt 中 QMainWindow 上的深色透明層的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要在我的應用程序中實現一個加載..."窗口,但我更喜歡用上面有文本的深色透明層覆蓋整個 QMainWindow.有人知道怎么做嗎?我不確定如何在 Qt 中重疊小部件/布局.任何幫助將不勝感激.

解決方案

這個答案是在我與疊加層相關的一系列答案中:

//https://github.com/KubaO/stackoverflown/tree/master/questions/overlay-widget-19362455#include #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)#include #萬一類 OverlayWidget : 公共 QWidget{void newParent() {如果 (!parent()) 返回;parent()->installEventFilter(this);增加();}民眾:顯式 OverlayWidget(QWidget * parent = {}) : QWidget{parent} {setAttribute(Qt::WA_NoSystemBackground);setAttribute(Qt::WA_TransparentForMouseEvents);newParent();}受保護://!從父小部件捕獲調整大小和子事件bool eventFilter(QObject * obj, QEvent * ev) 覆蓋 {如果(對象==父()){if (ev->type() == QEvent::Resize)resize(static_cast(ev)->size());else if (ev->type() == QEvent::ChildAdded)增加();}返回 QWidget::eventFilter(obj, ev);}//!跟蹤父小部件更改布爾事件(QEvent* ev)覆蓋{if (ev->type() == QEvent::ParentAboutToChange) {if (parent()) parent()->removeEventFilter(this);}else if (ev->type() == QEvent::ParentChange)newParent();返回 QWidget::event(ev);}};類 LoadingOverlay : 公共 OverlayWidget{民眾:LoadingOverlay(QWidget * parent = {}) : OverlayWidget{parent} {setAttribute(Qt::WA_TranslucentBackground);}受保護:無效paintEvent(QPaintEvent *)覆蓋{QPainter p{this};p.fillRect(rect(), {100, 100, 100, 128});p.setPen({200, 200, 255});p.setFont({"arial,helvetica", 48});p.drawText(rect(), "Loading...", Qt::AlignHCenter | Qt::AlignVCenter);}};int main(int argc, char * argv[]){QApplication a{argc, argv};QMainWindow 窗口;QLabel 中央{"你好"};central.setAlignment(Qt::AlignHCenter | Qt::AlignTop);central.setMinimumSize(400, 300);LoadingOverlay 疊加{&central};QTimer::singleShot(5000, &overlay, SLOT(hide()));window.setCentralWidget(&central);window.show();返回 a.exec();}

I need to implement a "Loading..." window in my application but I do prefer to cover the whole QMainWindow with a dark transparent layer with a text above. Does anybody know how to do that? I am not sure how to overlap widgets/layouts in Qt. Any help will be appreciated.

解決方案

This answer is in a series of my overlay-related answers: first, second, third.

The most trivial solution is to simply add a child transparent widget to QMainWindow. That widget must merely track the size of its parent window. It is important to properly handle changes of widget parentage, and the z-order with siblings. Below is a correct example of how to do it.

If you want to stack overlays, subsequent overlays should be the children of OverlayWidget, in the z-order. If they were to be siblings of the OverlayWidget, their stacking order is undefined.

This solution has the benefit of providing minimal coupling to other code. It doesn't require any knowledge from the widget you apply the overlay to. You can apply the overlay to a QMainWindow or any other widget, the widget can also be in a layout.

Reimplementing QMainWindow's paint event would not be considered the best design. It makes it tied to a particular class. If you really think that a QWidget instance is too much overhead, you better had measurements to show that being the case.

It is possible, of course, to make the overlay merely a QObject and to put the painting code into an event filter. That'd be an alternative solution. It's harder to do since you have to also properly deal with the parent widget's Qt::WA_StaticContents attribute, and with the widget potentially calling its scroll() method. Dealing with a separate widget is the simplest.

// https://github.com/KubaO/stackoverflown/tree/master/questions/overlay-widget-19362455
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif

class OverlayWidget : public QWidget
{
   void newParent() {
      if (!parent()) return;
      parent()->installEventFilter(this);
      raise();
   }
public:
   explicit OverlayWidget(QWidget * parent = {}) : QWidget{parent} {
      setAttribute(Qt::WA_NoSystemBackground);
      setAttribute(Qt::WA_TransparentForMouseEvents);
      newParent();
   }
protected:
   //! Catches resize and child events from the parent widget
   bool eventFilter(QObject * obj, QEvent * ev) override {
      if (obj == parent()) {
         if (ev->type() == QEvent::Resize)
            resize(static_cast<QResizeEvent*>(ev)->size());
         else if (ev->type() == QEvent::ChildAdded)
            raise();
      }
      return QWidget::eventFilter(obj, ev);
   }
   //! Tracks parent widget changes
   bool event(QEvent* ev) override {
      if (ev->type() == QEvent::ParentAboutToChange) {
         if (parent()) parent()->removeEventFilter(this);
      }
      else if (ev->type() == QEvent::ParentChange)
         newParent();
      return QWidget::event(ev);
   }
};

class LoadingOverlay : public OverlayWidget
{
public:
   LoadingOverlay(QWidget * parent = {}) : OverlayWidget{parent} {
      setAttribute(Qt::WA_TranslucentBackground);
   }
protected:
   void paintEvent(QPaintEvent *) override {
      QPainter p{this};
      p.fillRect(rect(), {100, 100, 100, 128});
      p.setPen({200, 200, 255});
      p.setFont({"arial,helvetica", 48});
      p.drawText(rect(), "Loading...", Qt::AlignHCenter | Qt::AlignVCenter);
   }
};

int main(int argc, char * argv[])
{
   QApplication a{argc, argv};
   QMainWindow window;
   QLabel central{"Hello"};
   central.setAlignment(Qt::AlignHCenter | Qt::AlignTop);
   central.setMinimumSize(400, 300);
   LoadingOverlay overlay{&central};
   QTimer::singleShot(5000, &overlay, SLOT(hide()));
   window.setCentralWidget(&central);
   window.show();
   return a.exec();
}

這篇關于Qt 中 QMainWindow 上的深色透明層的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 欧美成人精品在线观看 | www亚洲一区 | 午夜免费网 | 欧美日韩国产综合在线 | 精品一区二区免费视频 | 另类 综合 日韩 欧美 亚洲 | 精品国产一级 | 久久99视频 | 日日操夜夜干 | 欧美日韩国产在线观看 | 久久99精品久久久水蜜桃 | 97人人超碰| 色资源站 | 亚洲一区欧美一区 | av一区在线 | 亚洲a级| 怡红院怡春院一级毛片 | 亚洲3p | 成人在线播放网址 | 国产在线观看网站 | 欧美a区 | 精品一区在线 | 黄色av网站在线免费观看 | 亚洲精品99久久久久久 | 亚洲精品一区中文字幕 | av看片网| 成人午夜电影在线观看 | 欧美精产国品一二三区 | 91精品国产乱码久久久久久久久 | 天天天久久久 | 国产一区2区 | 欧美极品在线观看 | 国产三级一区二区三区 | 免费高清av | 国产电影一区 | 亚洲精品黄色 | 国产二区在线播放 | 免费在线视频精品 | 国产一级在线 | 久久成人一区 | 国产91久久精品一区二区 |