Improper visibility state when used Stylesheets - How do I get the proper visibility state?



  • Dear experts,

    I am working on a sample Qt desktop application where I am trying to figure out the visibility state of each child widget based on their visible region & number of visible rectangles on the display.
    Child widgets are dynamically created and inserted into the parent's widget stack.
    Each child widget has unique size and color.

    Expectation:
    Visibility state OBSCURED when a widget is covered by other.
    Visibility state VISIBLE when it is completely visible
    Visibility state PARTIAL_VISIBLE when part of the widget is obscured.

    Problem:
    When I use the style sheet to fill the background-color for the widgets, application always prints VISIBLE for all child widgets evenn when some widgets are completely obscured and some are partially visible.

    Below is the code sample which has this behavior:

    ChildWidget.ui : Default QWidget designed from Qt creator.

    ChildWidget.h
    @
    class ChildWidget : public QWidget
    {
    Q_OBJECT
    public:
    explicit ChildWidget(int width, int height, const char* pName, QString colorName, QWidget parent = 0);
    ~ChildWidget();
    const char
    getName() { return m_name; }
    protected:
    void paintEvent(QPaintEvent *pEvent);
    private:
    Ui::ChildWidget m_pChildWidgetUi;
    const char
    m_name;
    };
    @

    ChildWidget.cpp
    @
    #include <QPainter>
    #include <QStyleOption>
    #include "ChildWidget.h"
    #include "ui_ChildWidget.h"

    ChildWidget::ChildWidget(int width, int height, const char* pName, QString colorName, QWidget *parent) :
    QWidget(parent),
    m_pChildWidgetUi(new Ui::ChildWidget),
    m_name(pName)
    {
    m_pChildWidgetUi->setupUi(this);
    setFixedSize(QSize(width, height));
    setStyleSheet("QWidget {background-color: " + colorName + "}");
    }

    ChildWidget::~ChildWidget()
    {
    delete m_pChildWidgetUi;
    }

    void ChildWidget::paintEvent(QPaintEvent *pEvent)
    {
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    }
    @

    MainWidget.ui : QWidget with the name MainWidget with

    a. the size of 480*272

    b. background color (rgb(192, 0, 192) is set using style sheet

    c. QPushButton at the bottom right corner of the MainWidget with background color (rgb(0, 255, 255)) using style sheet

    MainWidget.h
    @
    class MainWidget : public QWidget
    {
    Q_OBJECT
    public:
    explicit MainWidget(QWidget *parent = 0);
    ~MainWidget();
    public slots:
    void printVisibilityState();
    private:
    Ui::MainWidget *m_pMainWidgetUi;
    };
    @

    MainWidget.cpp
    @
    #include <QtCore/QDebug>
    #include "MainWidget.h"
    #include "ui_MainWidget.h"
    #include "ChildWidget.h"

    MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    m_pMainWidgetUi(new Ui::MainWidget)
    {
    m_pMainWidgetUi->setupUi(this);
    connect(m_pMainWidgetUi->btnClick, SIGNAL(clicked()), this, SLOT(printVisibilityState()));
    }

    MainWidget::~MainWidget()
    {
    delete m_pMainWidgetUi;
    }

    void MainWidget::printVisibilityState()
    {
    QList<ChildWidget *> childWidgetsList = findChildren<ChildWidget *>();

    for (register int i = 0;  i  < childWidgetsList.size();  i++)
    {
        ChildWidget* pWidget = childWidgetsList.at(i);
    
        QRegion visibleRegion = pWidget->visibleRegion();
        QRect visibleRegionBoundingRect = visibleRegion.boundingRect();
        int visibleRegionRectsCount = visibleRegion.rects().count();
        QRect widgetRect = pWidget->rect();
    
        if (visibleRegion.isEmpty()) {
            qDebug() <<pWidget->getName()<< "is OBSCURED";
        }
        else {
            if ((visibleRegionBoundingRect == widgetRect) && (1 == visibleRegionRectsCount)) {
                qDebug() <<pWidget->getName()<< "is VISIBLE";
            }
            else {
                qDebug() <<pWidget->getName()<< "is PARTIALLY VISIBLE";
            }
        }
    }
    

    }
    @

    WindowManager.h
    @
    #include <QObject>
    #include <QList>
    #include "MainWidget.h"

    class WindowManager : public QObject
    {
    Q_OBJECT
    public:
    explicit WindowManager(QObject parent = 0);
    void insertWidget(int width, int height, const char
    pName, QString colorName);
    void printVisibilityState();
    void showMain();
    private:
    MainWidget* m_pMainWidget;
    };
    @

    WindowManager.cpp
    @
    #include "WindowManager.h"
    #include "ChildWidget.h"

    WindowManager::WindowManager(QObject *parent) :
    QObject(parent),
    m_pMainWidget(new MainWidget(NULL))
    {
    }

    void WindowManager::insertWidget(int width, int height, const char* pName, QString colorName)
    {
    ChildWidget *pChildWidget = new ChildWidget(width, height, pName, colorName, m_pMainWidget);
    Q_UNUSED(pChildWidget);
    }

    void WindowManager::showMain()
    {
    m_pMainWidget->show();
    }
    @

    Main.cpp
    @
    #include "WindowManager.h"
    #include "ChildWidget.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    WindowManager controller;

    controller.insertWidget(480, 15, "First", "white");
    controller.insertWidget(480, 100, "Second", "yellow");
    controller.insertWidget(200, 100, "Third", "blue");
    controller.insertWidget(480, 250, "Fourth", "pink");
    controller.insertWidget(480, 10, "Fifth", "grey");
    
    controller.showMain();
    
    return a.exec(&#41;;
    

    }
    @

    Why would the application shows "VISIBLE" for all child widgets even when some widgets are obscured and some are partially visible state?

    Am I missing something in calculation the visibility state?

    Does it shows VISIBLE always when I use the stylesheet? If yes, can you explain the reason?

    Is there any other way to get the proper visibility state of the ChildWidgets with stylesheets?

    Please clarify.

    Regards
    SRaju


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.