pyqt:单击主窗口时的焦点窗口小部件

我有一个小部件,当我点击一个按钮时打开,但当我点击其他地方(例如另一个程序),然后单击回主窗口小部件不会显示出来,我的意思是它在那里,但它是当它失去焦点时,我点击该程序的后面。 是否有一种方法可以在失去焦点后单击主窗口时使窗口小部件“聚焦”。

编辑:

我正在创建这样的菜单:

def FindDialog(self):
  self.w_2 = includes.CtrlF.MainWindow(self) # display find text window
  self.w_2.show()

这里是窗口代码:

# -*- 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)

你有没有试过使用这个:

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

在你的__init__

链接地址: http://www.djcxy.com/p/77409.html

上一篇: pyqt: Focus widget when mainwindow is clicked

下一篇: How to display a window on a secondary display in PyQT?