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. Get visibility status of widget without inheritance?
QtWS25 Last Chance

Get visibility status of widget without inheritance?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.2k Views
  • 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.
  • D Offline
    D Offline
    donquibeats
    wrote on last edited by
    #1

    As per the docs, the .isVisible() method of a QWidget returns True "if all its parent widgets up to the window are visible".

    Is there a way to determine whether a widget has a visible status itself only regardless of the state of its parents?

    For example, let's say I've got a QGridLayout in a QFrame. On that layout are two widgets, widgetA and widgetB. Widget A is visible, but widget B is hidden. If I do .setVisible(False) on the whole QFrame, then widgetA.isVisible() and widgetB.isVisible() will both return False. But if I re-show the frame, widgetA will re-appear, but widget B will remain hidden, remembering its previous state. Whilst the containing frame is hidden, is there any parameter or method which will return True on widget A but False on widget B?

    Pl45m4P JonBJ 2 Replies Last reply
    0
    • D donquibeats

      As per the docs, the .isVisible() method of a QWidget returns True "if all its parent widgets up to the window are visible".

      Is there a way to determine whether a widget has a visible status itself only regardless of the state of its parents?

      For example, let's say I've got a QGridLayout in a QFrame. On that layout are two widgets, widgetA and widgetB. Widget A is visible, but widget B is hidden. If I do .setVisible(False) on the whole QFrame, then widgetA.isVisible() and widgetB.isVisible() will both return False. But if I re-show the frame, widgetA will re-appear, but widget B will remain hidden, remembering its previous state. Whilst the containing frame is hidden, is there any parameter or method which will return True on widget A but False on widget B?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @donquibeats said in Get visibility status of widget without inheritance?:

      is there any parameter or method which will return True on widget A but False on widget B?

      Add your own variable to your widget class.

      A function returning true while widget A is hidden because of the parent, would be a lie :)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • D Offline
        D Offline
        donquibeats
        wrote on last edited by
        #3

        I wondered if that might be the solution, but I was hoping it wouldn't be because in practice I've got a wide variety of different widgets in use, and subclassing everything (QLabel, QComboBox, QLineEdit, QCheckBox, QListWidget, QSpinBox and a few others) is going to take a bit of time.

        I agree that .visible() would be lying if it returned True.

        However the Qt framework obviously remembers each widget's visibility status individually, because when the parent is set to visible, it doesn't change the previously set visibility flags on each widget. So I was hoping that existing boolean might have been exposed in a method like .wouldBeVisibleIfParentWereVisible() (except better worded!)

        Pl45m4P 1 Reply Last reply
        0
        • D donquibeats

          I wondered if that might be the solution, but I was hoping it wouldn't be because in practice I've got a wide variety of different widgets in use, and subclassing everything (QLabel, QComboBox, QLineEdit, QCheckBox, QListWidget, QSpinBox and a few others) is going to take a bit of time.

          I agree that .visible() would be lying if it returned True.

          However the Qt framework obviously remembers each widget's visibility status individually, because when the parent is set to visible, it doesn't change the previously set visibility flags on each widget. So I was hoping that existing boolean might have been exposed in a method like .wouldBeVisibleIfParentWereVisible() (except better worded!)

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @donquibeats said in Get visibility status of widget without inheritance?:

          However the Qt framework obviously remembers each widget's visibility status individually, because when the parent is set to visible, it doesn't change the previously set visibility flags on each widget. So I was hoping that existing boolean might have been exposed in a method like .wouldBeVisibleIfParentWereVisible() (except better worded!)

          Calling setVisible(false) or hide() hides a widget explicitly. An explicitly hidden widget will never become visible, even if all its ancestors become visible, unless you show it.
          

          from https://doc.qt.io/qt-5/qwidget.html#visible-prop


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0
          • D donquibeats

            As per the docs, the .isVisible() method of a QWidget returns True "if all its parent widgets up to the window are visible".

            Is there a way to determine whether a widget has a visible status itself only regardless of the state of its parents?

            For example, let's say I've got a QGridLayout in a QFrame. On that layout are two widgets, widgetA and widgetB. Widget A is visible, but widget B is hidden. If I do .setVisible(False) on the whole QFrame, then widgetA.isVisible() and widgetB.isVisible() will both return False. But if I re-show the frame, widgetA will re-appear, but widget B will remain hidden, remembering its previous state. Whilst the containing frame is hidden, is there any parameter or method which will return True on widget A but False on widget B?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @donquibeats
            [Crazy name! :) ]

            I don't think Qt offers you public access to the "visibility flag" which is (presumably) stored in each widget, only to the isVisible() method which calculates visibility including parentage.

            I see bool QWidget::isVisibleTo(const QWidget *ancestor) const

            Returns true if this widget would become visible if ancestor is shown; otherwise returns false.

            The true case occurs if neither the widget itself nor any parent up to but excluding ancestor has been explicitly hidden.

            If you don't get a better answer, couldn't you use this to calculate what the widget's visibility must be by walking up ancestors?

            For example (untested), if you have a widget which is really visible but its parentWidget() is set to hidden, I would expect/hope:

            widget.isVisible();    // false
            widget.parentWidget()->isVisible();    // false
            widget.isVisibleTo(widget.parentWidget());    // true
            

            To get it right you'll have to do some sort of iteration/recursion up the ancestor tree, e.g. think about case where it is (only) widget->parentWidget()->parentWidget() which has been explicitly hidden. EDIT Actually, if you read the docs carefully I think widget.isVisibleTo(widget.parentWidget()); always returns the widget's own visibility regardless of parent or its ancestors' states. So need to walk the tree --- test with grandparent case.

            P.S.
            While I was writing this you posted with .wouldBeVisibleIfParentWereVisible() in it. That's exactly what isVisibleTo() is!

            1 Reply Last reply
            2
            • D Offline
              D Offline
              donquibeats
              wrote on last edited by
              #6

              Thanks very much for the response.

              You're absolutely right- I'd managed to overlook isVisibleTo() by misunderstanding the docs and thinking it was for a different purpose.

              widgetA.isVisibleTo(widgetA.parentWidget()) does work exactly as I wanted, returning the 'local' visibility relative to the frame that it's contained on, regardless of whether that frame or anything higher up the tree is hidden.

              I can also refer to a specific higher-up widget in some cases too- the parameter must be a widget rather than a layout though.

              THANK YOU!

              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