Skip to content Skip to sidebar Skip to footer

Pyqt Window Focus

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 di

Solution 1:

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.

Solution 2:

classwindow_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)

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

Solution 3:

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

classwindow_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)

    deffocusOutEvent(self,event):
        self.showNormal()

That's it! Have fun coding

Post a Comment for "Pyqt Window Focus"