3

Create a new widget, show it, and hide it behind another application.

Is there a way to find out if the widget is visible to the user? For example, if you have two applications running and visible to the user (obviously only one of them has focus and is active) but both are visible

The reason I am interested in this is because my widget has a treewidget which calls setText() (which is very expensive) so I want to optimize the performance by updating only when the treewidget is visible to the user.

But in general, I'd like to know if there's a way of find "if the widget is visible to the user or not."

Thank you for any suggestions and alternatives.

Chenna V
  • 10,185
  • 11
  • 77
  • 104
  • I haven't got a proper answer but I believe isVisible can still be true even if the widget is obscured to the user, as long as the widget is still accessible by the parent. – Nicholas Smith Sep 01 '11 at 08:16
  • ya, but I want to know if the widget is obscured by another app. – Chenna V Sep 01 '11 at 13:12
  • There in lies the rub, you probably need to watch the QEvents being fired by the application and override the standard behaviour, if it's a one window app you could probably use applicationActivate and Deactivate, but you'll probably have to do a lot of leg work. – Nicholas Smith Sep 01 '11 at 14:51

3 Answers3

2

I think this is an expected behaviour as it is up to the window manager (if I'm not mistaken) to draw the windows according to their position, z-index,... So the Qt lib has nothing to do with that.

If you control when that other application is shown, you could hide the treewidget manually and show it again when you close the other application?

Otherwise, you should try to improve your widget and if you want help therefore, you should give us some code :)

ixM
  • 1,244
  • 14
  • 29
  • I can't avoid the treewidget and the setText operation is definitely time consuming, so I don't see much room for improvement. The only optimization I am left with is the disable updating the treewidget when it is not visible to the 'user'. Apart from the problem of treewidget, I want to know if the widget is actually visible to the 'user'. – Chenna V Aug 31 '11 at 16:21
1

You propably want something that has to do with focus.

http://doc.qt.io/archives/qt-4.7/qwidget.html#focus-prop

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
kechap
  • 2,077
  • 6
  • 28
  • 50
  • Nope, it has same behavior as ActiveWindow. If you have two applications open and both are visible to the user then only one of them has 'focus' true. I tried 'QApplication::focusWidget()' , 'QWidget::hasfocus' doesn't solve the problem – Chenna V Sep 01 '11 at 13:47
  • If this does not work then you have to subclass QApplication. QApplication has [focusChanged](http://doc.qt.nokia.com/latest/qapplication.html#focusChanged) signal and this means that you have better control. You simply check if the signal is emitted in a custom slot and enable or disable specific functionality. – kechap Sep 01 '11 at 17:52
  • Even if the focus is changed the whole application could still be visible or could be behind another application. I don't think QApplication would know that – Chenna V Sep 01 '11 at 17:55
  • I think that you can't get to a clean solution. Either you have to rethink this functionality or think something else that would trigger the functionality that you want to implement. – kechap Sep 01 '11 at 18:04
  • Yep, Looks like it. I'll see if I can find a way around. Thanks messkech – Chenna V Sep 01 '11 at 18:05
1

Have you tried QWidget::isActiveWindow()

KaZ
  • 1,165
  • 7
  • 10
  • 1
    it is only to know if the window has focus or not. i.e. if you have your widget and some other application side by side and you select the other application, even though the widget is visible to the user it is not active. – Chenna V Sep 01 '11 at 13:36