gpt4 book ai didi

python - Qt 按钮仅在有焦点时响应返回 - 需要自定义事件处理程序?

转载 作者:太空宇宙 更新时间:2023-11-04 10:56:33 25 4
gpt4 key购买 nike

我正在 Python 中使用 PyQt4 创建 Qt 应用程序。我已选择 Dialog 作为我的顶级应用程序小部件。我已阅读并理解按钮的默认和自动默认属性。我想要以下行为:按钮在单击或获得焦点(通过 Tab)并且用户按下 Enter 时响应。我还希望他们在获得焦点时改变颜色。我不希望他们在没有焦点时响应 Enter。这是因为我有一个 TextEdit 框,我希望将 Enter 逐字解释为 CR。如果我在按钮上启用 AutoDefault,将选择一个作为默认值(或者我可以指定它),如果我按 enter,该按钮将被激活。它始终突出显示,除非另一个按钮具有焦点。如果我禁用 AutoDefault,则两个按钮都不会突出显示,即使它们具有焦点,它们也不会响应输入,但只有空间,当它们具有焦点时。这让用户看不到焦点在哪里。

我的问题是:我是否需要编写自定义事件处理程序来启用所需的行为?一个相应的问题是:我最好不要使用 Dialog 作为我的顶层。

我正在使用 Qt Designer 和 pyqtuic。以下是两个按钮之一的代码摘录(另一个具有相同的操作):

    self.QuitButton = QtGui.QPushButton(Dialog)
self.QuitButton.setFocusPolicy(QtCore.Qt.StrongFocus)
self.QuitButton.setAutoDefault(False)

如有要求,可提供完整代码。

谢谢。


编辑 2/18 上午 7:00。完整代码如下。两个模块中的第一个是由 pyuic 从设计器 .ui 文件创建的模块是“email.py”:

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'email.ui'
#
# Created: Thu Feb 16 20:12:55 2012
# by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s

class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(643, 480)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(Dialog.sizePolicy().hasHeightForWidth())
Dialog.setSizePolicy(sizePolicy)
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(184, 255, 189))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Light, brush)
brush = QtGui.QBrush(QtGui.QColor(120, 255, 130))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Midlight, brush)
brush = QtGui.QBrush(QtGui.QColor(28, 127, 36))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Dark, brush)
brush = QtGui.QBrush(QtGui.QColor(37, 170, 48))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Mid, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.BrightText, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ButtonText, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Shadow, brush)
brush = QtGui.QBrush(QtGui.QColor(155, 255, 163))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.AlternateBase, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 220))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ToolTipBase, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ToolTipText, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(184, 255, 189))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Light, brush)
brush = QtGui.QBrush(QtGui.QColor(120, 255, 130))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Midlight, brush)
brush = QtGui.QBrush(QtGui.QColor(28, 127, 36))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Dark, brush)
brush = QtGui.QBrush(QtGui.QColor(37, 170, 48))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Mid, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.BrightText, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ButtonText, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Shadow, brush)
brush = QtGui.QBrush(QtGui.QColor(155, 255, 163))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.AlternateBase, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 220))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ToolTipBase, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ToolTipText, brush)
brush = QtGui.QBrush(QtGui.QColor(28, 127, 36))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
brush = QtGui.QBrush(QtGui.QColor(184, 255, 189))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Light, brush)
brush = QtGui.QBrush(QtGui.QColor(120, 255, 130))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Midlight, brush)
brush = QtGui.QBrush(QtGui.QColor(28, 127, 36))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Dark, brush)
brush = QtGui.QBrush(QtGui.QColor(37, 170, 48))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Mid, brush)
brush = QtGui.QBrush(QtGui.QColor(28, 127, 36))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.BrightText, brush)
brush = QtGui.QBrush(QtGui.QColor(28, 127, 36))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush)
brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Shadow, brush)
brush = QtGui.QBrush(QtGui.QColor(56, 255, 72))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.AlternateBase, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 220))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ToolTipBase, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ToolTipText, brush)
Dialog.setPalette(palette)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Times New Roman"))
font.setPointSize(16)
Dialog.setFont(font)
self.body = QtGui.QTextEdit(Dialog)
self.body.setGeometry(QtCore.QRect(20, 90, 601, 341))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.body.sizePolicy().hasHeightForWidth())
self.body.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Times New Roman"))
font.setPointSize(18)
self.body.setFont(font)
self.body.setFocusPolicy(QtCore.Qt.StrongFocus)
self.body.setObjectName(_fromUtf8("body"))
self.bodylabel = QtGui.QLabel(Dialog)
self.bodylabel.setGeometry(QtCore.QRect(20, 60, 141, 31))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Times New Roman"))
self.bodylabel.setFont(font)
self.bodylabel.setObjectName(_fromUtf8("bodylabel"))
self.subjectlabel = QtGui.QLabel(Dialog)
self.subjectlabel.setGeometry(QtCore.QRect(20, 16, 71, 31))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Times New Roman"))
self.subjectlabel.setFont(font)
self.subjectlabel.setObjectName(_fromUtf8("subjectlabel"))
self.SendButton = QtGui.QPushButton(Dialog)
self.SendButton.setGeometry(QtCore.QRect(336, 440, 121, 31))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(0, 255, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Light, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 255, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Light, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 255, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Light, brush)
self.SendButton.setPalette(palette)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Times New Roman"))
font.setPointSize(14)
self.SendButton.setFont(font)
self.SendButton.setFocusPolicy(QtCore.Qt.StrongFocus)
self.SendButton.setAutoDefault(False)
self.SendButton.setObjectName(_fromUtf8("SendButton"))
self.QuitButton = QtGui.QPushButton(Dialog)
self.QuitButton.setGeometry(QtCore.QRect(470, 440, 121, 31))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Times New Roman"))
font.setPointSize(14)
self.QuitButton.setFont(font)
self.QuitButton.setFocusPolicy(QtCore.Qt.StrongFocus)
self.QuitButton.setAutoDefault(False)
self.QuitButton.setObjectName(_fromUtf8("QuitButton"))
self.subject = QtGui.QLineEdit(Dialog)
self.subject.setGeometry(QtCore.QRect(90, 16, 531, 31))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.subject.sizePolicy().hasHeightForWidth())
self.subject.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Times New Roman"))
font.setPointSize(16)
self.subject.setFont(font)
self.subject.setFocusPolicy(QtCore.Qt.StrongFocus)
self.subject.setObjectName(_fromUtf8("subject"))

self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
Dialog.setTabOrder(self.body, self.SendButton)
Dialog.setTabOrder(self.SendButton, self.QuitButton)

def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Send Message to \"afamily\"", None, QtGui.QApplication.UnicodeUTF8))
self.bodylabel.setText(QtGui.QApplication.translate("Dialog", "Message Body", None, QtGui.QApplication.UnicodeUTF8))
self.subjectlabel.setText(QtGui.QApplication.translate("Dialog", "Subject", None, QtGui.QApplication.UnicodeUTF8))
self.SendButton.setText(QtGui.QApplication.translate("Dialog", "Send Email", None, QtGui.QApplication.UnicodeUTF8))
self.QuitButton.setText(QtGui.QApplication.translate("Dialog", "Cancel-Quit", None, QtGui.QApplication.UnicodeUTF8))

以下是应用代码,是两个模块中的第二个):

import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
from email import Ui_Dialog

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s

class DialogWindow(QtGui.QDialog):

def __init__(self):
QtGui.QDialog.__init__(self)

self.ui=Ui_Dialog() # instantiates the GUI class.
self.ui.setupUi(self)

# # callbacks for things (connects)
QtCore.QObject.connect(self.ui.SendButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.send)
QtCore.QObject.connect(self.ui.QuitButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.closenow)

#QtCore.QMetaObject.connectSlotsByName(Dialog)

# start with focus on Subject box
QtGui.QWidget.setFocus(self.ui.subject)
self.ui.body.setTabChangesFocus(True)

def send(self):
print("sending")
self.close()

def closenow(self):
print("closing")
self.close()


def main():
app = QtGui.QApplication(sys.argv)
window = DialogWindow()
window.show()
sys.exit(app.exec_())


if __name__ == '__main__':
main()

最佳答案

设置将按钮链接到 Return 键的快捷方式。

您说您正在使用 Qt Designer。进入按钮的属性,在 QAbstractButton-> 快捷方式下,点击字段中的返回键。

在代码方面,这样的事情可能会起作用:

self.QuitButton.setShortcut( tr("Return") );

关于python - Qt 按钮仅在有焦点时响应返回 - 需要自定义事件处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9329808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com