【问题标题】:pyqt: Focus widget when mainwindow is clickedpyqt:单击主窗口时的焦点小部件
【发布时间】:2013-08-28 11:33:39
【问题描述】:

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

编辑:

我正在创建这样的菜单:

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)

【问题讨论】:

    标签: python qt user-interface pyqt


    【解决方案1】:

    你试过用这个吗:

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

    在你的__init__?

    【讨论】:

    • 我现在试过了,它可以工作,但是当我点击另一个程序时它不会落后于其他窗口。当我单击另一个程序时,我希望它在它的后面,当我再次单击该程序时,它在它的前面。
    • 您是否设置了焦点策略? self.setFocusPolicy(QtCore.Qt.StrongFocus) 或 self.setFocusPolicy(QtCore.Qt.NoFocus)
    • 我现在试过了,但还是不行。我用我正在使用的代码编辑了我的帖子。
    猜你喜欢
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 2016-08-15
    • 2014-05-10
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    相关资源
    最近更新 更多