Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

python - 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 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...