Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt::WA_TranslucentBackground not work for QWidget
Forum Updated to NodeBB v4.3 + New Features

Qt::WA_TranslucentBackground not work for QWidget

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Toocold

    Enviroment: Qt5.15.2 and windows10
    Problem: I put a QWidget which has set a QGridLayout on top of a QWebView by QStackedWidget, and set attribute of QWidget to Qt::WA_TranslucentBackground, I can see the QWebView under the QLabel but I can not operate it.
    ps: Even without set QLayout, QWidget can not propagate the mouse event.

    QtGuiApplication1::QtGuiApplication1(QWidget *parent)
            : QMainWindow(parent)
    {    
        QWebEngineView* pWebWidget = new QWebEngineView();
            pWebWidget->load(QUrl("https://forum.qt.io/category/4/qt-development"));
        
            QWidget* pWidget = new QWidget();
            QGridLayout* pGridLayout = new QGridLayout();
            QPushButton* pButton = new QPushButton("Label");
            pGridLayout->addWidget(pButton, 0, 0, Qt::AlignLeft);
            pWidget->setLayout(pGridLayout);
            pWidget->setWindowFlags(Qt::FramelessWindowHint);
            pWidget->setAttribute(Qt::WA_TranslucentBackground);
        
            QStackedLayout* pStackedLayout = new QStackedLayout();
            pStackedLayout->addWidget(pWebWidget);
            pStackedLayout->addWidget(pWidget);
            pStackedLayout->setStackingMode(QStackedLayout::StackAll);
        
            QWidget* pCentralWidget = new QWidget();
            pCentralWidget->setLayout(pStackedLayout);
            this->setCentralWidget(pCentralWidget);
    }
    

    Picture:
    ba77aca6-bcc5-4073-b07d-ed783466a369-image.png

    T Offline
    T Offline
    Toocold
    wrote on last edited by
    #2
    This post is deleted!
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #3

      Hi,

      Are you looking for the Qt::WindowTransparentForInput Window flag ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        Are you looking for the Qt::WindowTransparentForInput Window flag ?

        T Offline
        T Offline
        Toocold
        wrote on last edited by Toocold
        #4

        @SGaist Hello. I want this pushbutton can receive mouse event, and the rest transparent area can propagate event to the bottom QWebView. This flag seems not what I want.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          How are you applying it ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          T SGaistS 2 Replies Last reply
          0
          • SGaistS SGaist

            How are you applying it ?

            T Offline
            T Offline
            Toocold
            wrote on last edited by
            #6

            @SGaist Hey.
            I want a QLyout which has many buttons on the top of the QWebEngineview.
            Both of them can auto resize themselves, and the rest transparency area of the QLayout can propagate event like mouse click or wheel event.
            But seems like QLayout must set to a Qwidget so that it can be resized.
            I can implement this idea by eventfilter so that i can pass through the event, but any other ways?
            Thank you.

            1 Reply Last reply
            0
            • SGaistS SGaist

              How are you applying it ?

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #7

              Hence my question:

              @SGaist said in Qt::WA_TranslucentBackground not work for QWidget:

              How are you applying it ?

              I haven't used it in a long time so what happens if you apply the flag only on the widget that you make transparent and that will likely contain your buttons.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              T 1 Reply Last reply
              0
              • SGaistS SGaist

                Hence my question:

                @SGaist said in Qt::WA_TranslucentBackground not work for QWidget:

                How are you applying it ?

                I haven't used it in a long time so what happens if you apply the flag only on the widget that you make transparent and that will likely contain your buttons.

                T Offline
                T Offline
                Toocold
                wrote on last edited by
                #8

                @SGaist said in Qt::WA_TranslucentBackground not work for QWidget:

                How are you applying it

                Sorry for my off-topic reply.
                If you mean the flag of Qt::WindowTransparentForInput, I apply it by

                    pWidget->setWindowFlags(Qt::WindowTransparentForInput | Qt::FramelessWindowHint);
                    pWidget->setAttribute(Qt::WA_TranslucentBackground);
                

                Nothing changed just like my original code, buttons can work but you can't manipulate the web.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Just to check, what happens if you do not use QStackedWidget but manually position pWidget on top of your web view ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  T 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Just to check, what happens if you do not use QStackedWidget but manually position pWidget on top of your web view ?

                    T Offline
                    T Offline
                    Toocold
                    wrote on last edited by
                    #10

                    @SGaist ok, I have tried this before.

                        m_pWebWidget = new QWebEngineView(this);
                        m_pWebWidget->load(QUrl("https://forum.qt.io/category/4/qt-development"));
                        m_pWebWidget->setFixedSize(200, 200);
                        m_pWebWidget->show();
                    
                        m_pWidget = new QWidget(this);
                        m_pWidget->setFixedSize(200, 200);
                        m_pWidget->setWindowFlags(Qt::WindowTransparentForInput | Qt::FramelessWindowHint);
                        m_pWidget->setAttribute(Qt::WA_TranslucentBackground);
                        m_pWidget->show();
                    

                    The web view is not operable either.

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      Bonnie
                      wrote on last edited by Bonnie
                      #11

                      Qt::WA_TranslucentBackground just change the painting of background, doesn't ignore the mouse events.
                      Qt::WA_TransparentForMouseEvents is the attribute for that, but it will also make its child widget (the button) not clickable.
                      (PS. I think it's not necessary to set Qt::FramelessWindowHint to a non-window widget.)
                      Currently the easier way I can think of is, just create a button as the web view's child widget and control its geometry manually without any layout.
                      By this way you don't need to set any flag / attribute to have a clickable button and a operable web view.

                          QWebEngineView* pWebWidget = new QWebEngineView();
                          pWebWidget->load(QUrl("https://forum.qt.io/category/4/qt-development"));
                      
                          QPushButton* pButton = new QPushButton("Label", pWebWidget);
                          pButton->move(0, 100);
                      
                          this->setCentralWidget(pWebWidget);
                      

                      If you need to adjust the geometry of the button when window size changes, do it in resizeEvent.

                      T 1 Reply Last reply
                      1
                      • B Bonnie

                        Qt::WA_TranslucentBackground just change the painting of background, doesn't ignore the mouse events.
                        Qt::WA_TransparentForMouseEvents is the attribute for that, but it will also make its child widget (the button) not clickable.
                        (PS. I think it's not necessary to set Qt::FramelessWindowHint to a non-window widget.)
                        Currently the easier way I can think of is, just create a button as the web view's child widget and control its geometry manually without any layout.
                        By this way you don't need to set any flag / attribute to have a clickable button and a operable web view.

                            QWebEngineView* pWebWidget = new QWebEngineView();
                            pWebWidget->load(QUrl("https://forum.qt.io/category/4/qt-development"));
                        
                            QPushButton* pButton = new QPushButton("Label", pWebWidget);
                            pButton->move(0, 100);
                        
                            this->setCentralWidget(pWebWidget);
                        

                        If you need to adjust the geometry of the button when window size changes, do it in resizeEvent.

                        T Offline
                        T Offline
                        Toocold
                        wrote on last edited by
                        #12

                        @Bonnie Yeah, I think Qt may not have exact flag/attribute I need.
                        And what you say it's a useful way to solve my quesiton if I only need to manage a few objects.
                        Without QLayout it will become a strenuous work in further.
                        I think below code is a feasible way to solve similar question:

                        bool QtGuiApplication1::eventFilter(QObject *watched, QEvent *event)
                        {
                            if (watched == m_pWidget && (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::Wheel))
                            {
                                if (m_pWebWidget->focusProxy())
                                {
                                    QCoreApplication::sendEvent(m_pWebWidget->focusProxy(), event);
                                }
                            }
                            return false;
                        }
                        

                        Thank you for your advice and i will use it.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved