I’m creating a Qt application using PyQt4 in Python. I have chosen the Dialog as my top level application widget. I have read and understood the Default and AutoDefault properties of Push Buttons. I want the following behavior: the buttons respond when clicked or when they have focus (via Tab) and a user presses Enter. I also want them to change color when they have the focus. I don’t want them to respond to Enter when they do not have focus. This is because I have a TextEdit box for which I want Enter to interpreted literally as a CR. If I enable AutoDefault on the buttons, one is chosen as a default (or I can designate it) and that button gets activated if I press enter. It is highlighted all the time, except when another button has the focus. If I disable AutoDefault, neither of the buttons are highlighted, even when they have the focus, and they do not respond to enter, but only space, when they have the focus. This is leaving the user blind as to where the focus is.
My question is: do I need to write a custom event handler to enable the desired behavior? A corresponding question is: Am I better off not using a Dialog as my top level.
I am using Qt Designer and pyqtuic. Here is an excerpt of the code for one of the two buttons (the other one has the same operations):
self.QuitButton = QtGui.QPushButton(Dialog)
self.QuitButton.setFocusPolicy(QtCore.Qt.StrongFocus)
self.QuitButton.setAutoDefault(False)
Full code is available, if requested.
Thank you.
Edit 2/18 7:00 AM. Full code follows. The first of two modules is the one created by pyuic from a designer .ui file is “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))
Following is the application code, which is the second of two modules):
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()
Here is what I finally arrived in solving this problem. I spent quite a bit of time trying to control appearance to my wishes. It is probably a PyQt hack-job with the use of style sheets. At least I understand it! Style sheets are the only way I could find to control some elements of the appearance. First, below find the UI->PY file from the UI I created in Qt Designer.
Next you can find the top level code. It has all the hacks in it. Note this application reads contacts from gmail and send the message via gmain.
The processing of the keystrokes has tab moving between the four widgets. When the buttons have focus, pressing return on them will cause the button to be activated.