pyqt: Focus widget when mainwindow is clicked
I have a widget that I open when I click a button, but when I click somewhere else (eg another program) and then click back to the main window the widget doesn't show up with it, I mean it is there but it is behind the program I clicked when it lost focus. Is there a way to make the widget "focus" when the main windows is clicked after it lost focus.
Edit:
I am creating the menu like this:
def FindDialog(self):
self.w_2 = includes.CtrlF.MainWindow(self) # display find text window
self.w_2.show()
And here is the window code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'out/CtrlF.ui'
#
# Created: Sat Aug 24 17:15:24 2013
# by: PyQt4 UI code generator 4.10.2
#
# WARNING! All changes made in this file will be lost!
# TODO:
# Get check boxes working
# on start up get selected text and set that
# loop around on end (back and forwards)
#
from PyQt4.Qt import *
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form, main):
Form.setObjectName(_fromUtf8("Form"))
Form.setFixedSize(370, 128)
Form.setWindowFlags(Qt.SubWindow | Qt.WindowStaysOnTopHint)
#Form.setFocusPolicy(Qt.StrongFocus)
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(10, 10, 46, 13))
self.label.setOpenExternalLinks(False)
self.label.setObjectName(_fromUtf8("label"))
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(10, 30, 241, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.checkBox = QtGui.QCheckBox(Form)
self.checkBox.setGeometry(QtCore.QRect(10, 60, 81, 17))
self.checkBox.setObjectName(_fromUtf8("checkBox"))
self.checkBox_2 = QtGui.QCheckBox(Form)
self.checkBox_2.setGeometry(QtCore.QRect(10, 80, 131, 17))
self.checkBox_2.setObjectName(_fromUtf8("checkBox_2"))
self.checkBox_3 = QtGui.QCheckBox(Form)
self.checkBox_3.setGeometry(QtCore.QRect(10, 100, 91, 17))
self.checkBox_3.setObjectName(_fromUtf8("checkBox_3"))
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(280, 10, 81, 21))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(lambda: main.FindText(self.lineEdit.text(), False, self.checkBox.isChecked(), self.checkBox_2.isChecked(), self.checkBox_3.isChecked()))
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(280, 40, 81, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_2.clicked.connect(lambda: main.FindText(self.lineEdit.text(), True, self.checkBox.isChecked(), self.checkBox_2.isChecked(), self.checkBox_3.isChecked()))
self.pushButton_3 = QtGui.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(280, 70, 81, 23))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.pushButton_3.clicked.connect(Form.close)
self.label_2 = QtGui.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(160, 100, 111, 16))
self.label_2.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.label_2.setStyleSheet(_fromUtf8("font: 8pt "MS Shell Dlg 2";n"
"color: rgb(0, 85, 255);n"
"text-decoration: underline;"))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Find Text", None))
self.label.setText(_translate("Form", "Find:", None))
self.pushButton.setText(_translate("Form", "Find Next", None))
self.pushButton_3.setText(_translate("Form", "Close", None))
self.pushButton_2.setText(_translate("Form", "Find Previous", None))
self.checkBox.setText(_translate("Form", "Match case", None))
self.checkBox_2.setText(_translate("Form", "Match whole word only", None))
self.checkBox_3.setText(_translate("Form", "Regex search", None))
self.label_2.setText(_translate("Form", "Goto Replace (Ctrl+H)", None))
class MainWindow(QWidget):
def __init__(self, main):
QWidget.__init__(self)
self.ui = Ui_Form()
self.ui.setupUi(self, main)
Have you tried to use this:
self.setWindowFlags(PyQt4.QtCore.Qt.WindowStaysOnTopHint)
in your __init__
?
上一篇: pyqt:较低的码头小部件
下一篇: pyqt:单击主窗口时的焦点窗口小部件