Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Make a maskWidget to grandparent widget



  • I want to make a mask widget,which is transparent to its parent widget,and mask its grandparent widget.But it seems like that the mask widget isn't able to resize larger than its parent,could you please expand what's wrong?
    20210722204029.png

    //MaskWidget.cpp
    MaskWidget::MaskWidget(QWidget *parent) : QWidget(parent)
    {
        m_pVisibleLabel = new QLabel(this);
        //mask,not transparent
        setStyleSheet("QWidget{background-color:rgba(0,0,255,60%);}");
        //transparent,so that I am able to leave the parent widget visible
        m_pVisibleLabel->setStyleSheet("QLabel{background-color:transparent;}");
    }
    
    void MaskWidget::SyncUI()
    {
        QWidget *pParent = parentWidget();
        if(pParent != nullptr)
        {
            QWidget *pGrandParent = pParent->parentWidget();
            if(pGrandParent != nullptr)
            {
                //mask the grandparent widget,like a mask,but not work
                this->resize(pGrandParent->width(),pGrandParent->height());
                //leave the parent widget visible
                m_pVisibleLabel->resize(pParent->width(),pParent->height());
                QPoint point = mapToParent(QPoint(0,0));
                m_pVisibleLabel->move(point);
            }
        }
    }
    
    //ChildWidget.cpp
    //show the mask whenever the child widget is shown
    void ChildWidget::showEvent(QShowEvent *event)
    {
        m_pMask->show();
        m_pMask->SyncUI();
        m_pMask->raise();
    }
    


  • @Joe-Johnson said in Make a maskWidget to grandparent widget:

    isn't able to resize larger than its parent,could you please expand what's wrong?

    True. Since the position is displayed in parent's coordinate system, it can't exceed its parent's boundaries.

    What if you "un-parent" it and make it a direct child of its current grandparent?



  • @Joe-Johnson said in Make a maskWidget to grandparent widget:

    isn't able to resize larger than its parent,could you please expand what's wrong?

    True. Since the position is displayed in parent's coordinate system, it can't exceed its parent's boundaries.

    What if you "un-parent" it and make it a direct child of its current grandparent?



  • This post is deleted!


  • @Pl45m4 oops,It works well,thanks.Here is result a01c7252-c384-4aee-8f7b-63071df25dff-image.png


Log in to reply