10

I am trying to give focus to a window if the user clicks on another window.

Right now i have two windows: Window A is behind, and Window B is in front. When Window B appears, it disables Window A. Now what i want is that whenever the user clicks outside of Window B, it should give focus back to Window B.

Here is the code for Window B:

class window_b(QtGui.QDialog):
    def __init__(self,parent=None):
        super(window_b, self).__init__(parent)
        window_a.setEnabled(False)
        self.ui = Ui_Form_window_b()
        self.ui.setupUi(self)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)

    def focusOutEvent(self,event):
        self.setFocus(True)
        self.activateWindow()
        self.raise_()
        self.show()

I tried setFocus and activateWindow, but it didnt give focus back to Window B.

Any suggestions?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Uahmed
  • 1,847
  • 5
  • 29
  • 45
  • Would you like this behaviour with respect to all other windows or only to Window A. If you want the latter you could make Window B a modal window http://en.wikipedia.org/wiki/Modal_window. Window B then is a child of Window A which afterwards can't be focused as long as Window B is open. – halex Sep 05 '12 at 12:37

4 Answers4

14

To get window_b to always stay on top you have to add the windowflag QtCore.Qt.WindowStaysOnTopHint. In your __init__ add the call

self.setWindowFlags(PyQt4.QtCore.Qt.WindowStaysOnTopHint)

I have to add that this only is a hint to the windowing manager and not guaranteed to succeed.

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
halex
  • 16,253
  • 5
  • 58
  • 67
8

self.raise_() followed by a self.activateWindow() should be the commands you are looking for, although there seems to be some kind of issues with that on my Debian OS, for example, if I click on a window that is maximized, the window will obtain focus, but it will also disappear, looks like some kind of bug, the sequence in the setTopLevelWindow method will circumvent that behaviour:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtGui, QtCore, QtWebKit, QtNetwork

class myWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)

        self.button = QtGui.QPushButton(self)
        self.button.setText("Show Dialog")

        self.dialog = QtGui.QDialog(self)
        self.dialog.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.dialog.installEventFilter(self)

        self.button.clicked.connect(self.dialog.show)

        self.setCentralWidget(self.button)

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.WindowDeactivate:
            self.setTopLevelWindow()
            self.dialog.close()

            return True

        return False

    def setTopLevelWindow(self):    
        if self.windowState() != QtCore.Qt.WindowMaximized:
            self.showMaximized()
            self.showNormal()

        else:
            self.showNormal()
            self.showMaximized()

        self.raise_()
        self.activateWindow()


if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myWindow')

    main = myWindow()
    main.show()

    sys.exit(app.exec_())
0
class window_b(QtGui.QDialog):
    def __init__(self,parent=None):
        super(window_b, self).__init__(parent)
        window_a.setEnabled(False)
        self.ui = Ui_Form_window_b()
        self.ui.setupUi(self)
        self.setWindowModality(QtCore.Qt.ApplicationModal)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)

    def focusOutEvent(self,event):
        self.setFocus(True)
        self.activateWindow()
        self.raise_()
        self.show()

dudulu
  • 754
  • 6
  • 17
  • 1
    where in the code in the question do we need to put the line you posted for the problem in the question to be solved? – Ophir Carmi Aug 11 '20 at 13:48
0

I found an easy trick there, just use self.showNormal() and it works, gives the focus back to the window, so it would be:

class window_b(QtGui.QDialog):
    def __init__(self,parent=None):
        super(window_b, self).__init__(parent)
        window_a.setEnabled(False)
        self.ui = Ui_Form_window_b()
        self.ui.setupUi(self)
        self.setFocusPolicy(QtCore.Qt.StrongFocus)

    def focusOutEvent(self,event):
        self.showNormal()

That's it! Have fun coding

Parsa
  • 53
  • 7