久久久久久久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 ()?環形?)
主站蜘蛛池模板: 自拍偷拍中文字幕 | 四虎影院在线播放 | 日韩在线不卡视频 | 欧美日韩国产三级 | 欧美一二 | 国产中文字幕在线播放 | 黄色在线播放 | 日韩一级黄 | 免费性网站 | 日韩中文av | 日日爱视频 | 成人永久免费视频 | av一区二区三区 | 99精品久久久久久中文字幕 | 四虎四虎 | 国产黄色在线观看 | 九九在线 | 国产一级片在线播放 | 精品国产一区二 | 一级黄色av| 中国农村毛片免费播放 | 五月激情久久 | 午夜视频免费在线观看 | 久久av免费 | 亚洲色综合| 日本中文字幕在线播放 | 简单av网| 中文字幕在线视频播放 | 国产香蕉视频在线观看 | 一区二区三区在线观看视频 | 婷婷色网| 天天干天天色 | 国产成人亚洲精品自产在线 | 欧美三级韩国三级日本三斤在线观看 | 日韩av免费在线播放 | 五月婷婷网 | 日产毛片 | 玖玖在线播放 | 在线日韩欧美 | 黄色大片免费在线观看 | 午夜综合网 |