Results 1 to 4 of 4

Thread: Get the Window Visible State when it is partially visible

  1. #1
    Join Date
    Oct 2013
    Location
    Everett WA USA
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Get the Window Visible State when it is partially visible

    Dear All,

    I am working on an application where in which I need to determine the state of the window based on the visibility state.
    I have an enum declared as follows:
    enum APP_WIN_STATE
    {
    APP_WIN_INACTIVE, // Window in not associated with a display
    APP_WIN_OBSCURED, // Window is completed obscured on a display
    APP_WIN_PARTIAL, // Window is partially obscured on a display
    APP_WIN_VISIBLE, // Window is completely visible on a display
    };

    I need to set the window state based on their visibility states.

    Qt Code:
    1. #include <QApplication>
    2. #include <QPalette>
    3. #include <QWidget>
    4. #include <QDebug>
    5. #include <QString>
    6.  
    7. void printWidgetProperties(QWidget *pWidget, QString message)
    8. {
    9. qDebug() << message;
    10.  
    11. QRegion visRegn = pWidget->visibleRegion();
    12. if (visRegn.isEmpty())
    13. {
    14. qDebug() << "is *not* visible";
    15. }
    16. else
    17. {
    18. if(visRegn.boundingRect() == pWidget->rect()) {
    19. qDebug() << "is fully visible";
    20. } else {
    21. qDebug() << "is partially visible";
    22. }
    23. }
    24. }
    25.  
    26. QWidget* createWidget(int top, int left, int width, int height, QColor color, QWidget *pParent = NULL)
    27. {
    28. QWidget *pWidget = new QWidget(pParent);
    29. pWidget->setFixedSize(width, height);
    30. pWidget->setGeometry(top, left, width, height);
    31.  
    32. QPalette pal;
    33. pal.setColor(QPalette::Background, color);
    34. pWidget->setAutoFillBackground(true);
    35. pWidget->setPalette(pal);
    36.  
    37. return pWidget;
    38. }
    39.  
    40. int main(int argc, char *argv[ ])
    41. {
    42. QApplication app(argc, argv);
    43.  
    44. QPalette pal;
    45. QWidget *pMainWin = createWidget(0, 0, 480, 272, Qt::white);
    46.  
    47. // QWidget * pWinG = createWidget(10, 10, 420, 260, Qt::green, pMainWin);
    48. QWidget * pWinG = createWidget(0, 0, 480, 272, Qt::green, pMainWin);
    49.  
    50. // QWidget * pWinB = createWidget(190, 86, 100, 100, Qt::blue, pMainWin);
    51. QWidget * pWinB = createWidget(0, 0, 480, 20, Qt::blue, pMainWin);
    52.  
    53. // QWidget * pWinR = createWidget(380, 86, 70, 50, Qt::red, pMainWin);
    54. QWidget * pWinR = createWidget(0, 20, 480, 252, Qt::red, pMainWin);
    55.  
    56. QWidget * pWinY = createWidget(380, 86, 70, 50, Qt::yellow, pMainWin);
    57.  
    58. pMainWin->show();
    59.  
    60. printWidgetProperties(pWinG, "Widget Green");
    61. printWidgetProperties(pWinR, "Widget Red");
    62. printWidgetProperties(pWinB, "Widget Blue");
    63. printWidgetProperties(pWinY, "Widget Yellow");
    64.  
    65. return app.exec();
    66. }
    To copy to clipboard, switch view to plain text mode 

    In the above code I am trying to determine each widget state based on the visibility region. My expectations are as follows from the code:
    a. Widget Green - Completely Obscured
    b. Widget Blue - Cimpletely Visible
    c. Widget Red - Partially Visiblle
    d. Widget Yellow - Completely Visible

    But the above code gives me an output that
    "Widget Green"
    is *not* visible
    "Widget Red"
    is fully visible
    "Widget Blue"
    is fully visible
    "Widget Yellow"
    is fully visible

    Query: How do we determine that widget is partially visible (in my case yellow widget is enclosing some part of the Red Widget and hence Red Widget is partially visible)?
    Is there a way to say that a Widget (Red Widget in my example) is partially Visible in Qt?
    Kindly do the needful

    Regards
    SRaju

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Get the Window Visible State when it is partially visible

    Try this:
    Qt Code:
    1. if(visRegn.boundingRect() == pWidget->rect() && visRegn.rects().count() == 1) {
    2. qDebug() << "is fully visible";
    3. }
    To copy to clipboard, switch view to plain text mode 
    It assumes that if visible region is just one single rect equal to whole bounding rect, then widget is fully visible.

  3. #3
    Join Date
    Oct 2013
    Location
    Everett WA USA
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Get the Window Visible State when it is partially visible

    Thank you very much , it worked well. If there are any overlapping rectangles then it is partially Visible otherwise it is completely visible.

    Regards
    SRaju

  4. #4
    Join Date
    Apr 2010
    Location
    Minsk
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get the Window Visible State when it is partially visible

    Doesn't work for me. visRegn.boundingRect() and pWidget->rect() are always equal and visRegn.rects().count() always return 4 when the window is visible (setVisible(true))
    I tried to implement next..

    I have a window and a tray icon. There are 3 possible actions on click on the tray.
    1) raise the window to front if it's behind of some others windows
    2) show and raise it if it's currently in the hidden state.
    3) hide it if it's currently fully visible.

    I guess there is no other Qt way to do this. So may be it's better to implement DE specific tray plugins for my application. For example KDE has a concept of "associated widget" in their KStatusNotifierItem. According to comments in the code it seems it does exactly what I want.

Similar Threads

  1. Always visible qml
    By Gerhman in forum Qt Quick
    Replies: 0
    Last Post: 18th August 2012, 07:15
  2. Replies: 2
    Last Post: 2nd March 2012, 07:55
  3. QT help is not visible
    By ganeshrgaikwad in forum Installation and Deployment
    Replies: 0
    Last Post: 12th January 2010, 11:30
  4. Cursor not visible
    By jpujolf in forum Qt Programming
    Replies: 8
    Last Post: 11th May 2007, 14:05
  5. semi-transparent window with a visible line on it
    By nagpalma in forum Qt Programming
    Replies: 10
    Last Post: 27th April 2007, 13:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.