You can use setShortcut on each button if you just want a really simply way
to trigger it from a key sequence:

####
self.label = QtGui.QLabel("<default>")
self.spaceButton = QtGui.QPushButton("Space")
self.enterButton = QtGui.QPushButton("Enter")

self.spaceButton.clicked.connect(partial(self.label.setText, "Space"))
self.enterButton.clicked.connect(partial(self.label.setText, "Enter"))

self.spaceButton.setShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Space))
self.enterButton.setShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Enter))
####

Or if you need more specific handling, such as multiple keys for shortcuts,
then you can define the keyPressEvent on your parent widget:

####
    def keyPressEvent(self, event):
        key = event.key()
        if key == QtCore.Qt.Key_Space:
            self.spaceButton.click()
        elif key in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):
            self.enterButton.click()
        super(Widget, self).keyPressEvent(event)
####


-- justin



On Tue, Nov 6, 2012 at 3:36 PM, iMath <[email protected]> wrote:

> The UI is as following .
>
> <http://img.my.csdn.net/uploads/201211/03/1351957960_1171.jpg>
>
>
>
> What I want is :
> When I press the Enter key ,the label should shows “Enter”
> When I press The blank space key ,the label should shows “space”
>
> How to implement this ?
>
> I know I should reimplement the keyPressEvent handler ,but I guss I was
> got stuck by the focus
>
> --
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings:
> http://groups.google.com/group/python_inside_maya/subscribe
>

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to