[PyQt] How to create non-stealing focus QFrame?

swdev.bali at gmail.com swdev.bali at gmail.com
Sun Jul 6 15:48:45 BST 2014

Dear community,

I am trying to create a Windows application containing a QMainWindow  that also open up several QFrame window containing some buttons in it. It sort of like a free floating tool window. But the problem is, when I click button on a floating QFrame, the main window lost its focus. Giving unpleasant experience to the user.

I have found an answer actually, that should’ve solve this problem: using win32con.WS_EX_NOACTIVATE. But somehow, I still unable to properly applied this extended style. This is the SO page containing this solution : http://stackoverflow.com/questions/18662031/avoid-application-activation-and-focus-in-when-clicking-buttons-on-it-windows/18774481?noredirect=1#comment38088304_18774481

This is my Python code using PyWin32 libraries to try to use that extended style:

    def no_focus(self):
        TODO Suppose to create a non stealing focus window.
        Not working yet
        import ctypes
        import win32con
        import win32gui

        dc = win32gui.GetWindowDC(self.winId())
        user32 = ctypes.windll.user32
        existing_style = user32.GetWindowLongW(dc, win32con.GWL_EXSTYLE)
        user32.SetWindowLongW(dc, win32con.GWL_EXSTYLE, existing_style | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOOLWINDOW)

Based on that SO page, I have also try to override showEvent(), but to no result:

    def showEvent(self, *args, **kwargs):
        super(FSFloatingFrame, self).showEvent(*args)

My current partial solution, is to trap eventFilter for QEvent.WindowActivate in my tool window base class, and activateWindow() for the mainwindow. But it also stop all the buttons from functioning.

def eventFilter(self, object, QEvent):
        if QEvent.type() == QEvent.WindowActivate:
            #this half solve the problem: window won't steal focus: but it also make no action to the buttons
        return super(FSFloatingFrame, self).eventFilter(object, QEvent)

Any suggestion on this particular case?

I am quite wonder whether tool window that behave like it’s a tool window (non stealing focus) have already been provided by Qt…

Thanks in advance!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140706/7ca02edd/attachment.html>

More information about the PyQt mailing list