检测在QLineEdit或QPushButton上的输入 [英] Detecting enter on a QLineEdit or QPushButton

查看:69
本文介绍了检测在QLineEdit或QPushButton上的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为游戏构建了一个应用程序,易于上手.在这种游戏中,系统会随机选择一个数字,而玩家(玩家)会尝试找出该数字.一切都快完成了.该应用程序包含一个 QLineEdit ,一个标签和三个按钮.一旦应用程序告知玩家想要的号码范围,他/她将输入 bet_number 并单击播放"按钮.然后,根据这个数字,他/她会收到一条消息,告知想要的数字与 bet_number 的距离有多近.

I've built an app for a game, simple to start. It's a game in which the system randomly chooses a number and a gamer (player) tries to find out the number. Everything is almost done. The app consists of a QLineEdit, a label and three buttons. Once the app tells the player the range of the wanted number, he/she types a bet_number and clicks on the play button. And according to this number he/she gets a message about how close or far the wanted number is away from the bet_number.

但是我发现单击按钮有点恶心.相反,我想使用 Enter键进行播放.因此,要实现这一目标,可以归结为以下两个问题:

But I find it a little disgusting to click a button. Instead I want to use Enter key to play. So to achieve this, it comes down to specifically two questions:

  1. 如何使用Enter键进行播放(我想知道何时 QLineEdit 检测到按下Enter键)?这样,我将正确编码以指向play方法.

  1. How could one change to using Enter to play (I mean I need know when QLineEdit detects enter key is pressed)? In this way I'll code properly to point the play method.

如果焦点位于播放按钮,则如何使用该按钮上的Enter键?(使Button接受Enter键)

If the play button's got the focus, how do you use enter key on this button? (make Button accept Enter key)

推荐答案

对于 QLineEdit 连接到>> returnPressed 信号.

For the QLineEdit connect to the returnPressed signal.

或者,如果您使用 setAutoDefault QPushButton a> s您发出 clicked 在聚焦的 上按下 Enter 时发出信号QPushButton :

Alternatively, if you use the setAutoDefault method on your QPushButtons you emit the clicked signal when Enter is pressed on a focused QPushButton:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.pushButtonOK = QtGui.QPushButton(self)
        self.pushButtonOK.setText("OK")
        self.pushButtonOK.clicked.connect(self.on_pushButtonOK_clicked)
        self.pushButtonOK.setAutoDefault(True)

        self.lineEditNumber = QtGui.QLineEdit(self)
        self.lineEditNumber.returnPressed.connect(self.pushButtonOK.click)

        self.layoutHorizontal = QtGui.QHBoxLayout(self)
        self.layoutHorizontal.addWidget(self.pushButtonOK)
        self.layoutHorizontal.addWidget(self.lineEditNumber)

    @QtCore.pyqtSlot()
    def on_pushButtonOK_clicked(self):
        inputNumber = self.lineEditNumber.text()
        if inputNumber.isdigit():
            info = "You selected `{0}`"

        else:
            info = "Please select a number, `{0}` isn't valid!"

        print info.format(inputNumber)

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()

    sys.exit(app.exec_())

这篇关于检测在QLineEdit或QPushButton上的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆