如何确定哪个小部件发出信号 [英] How to determine which widget emitted the signal

查看:52
本文介绍了如何确定哪个小部件发出信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让 QlineEdit 字段可双击,以便用户双击 QlineEdit 字段(例如,qle01")调用函数.该函数应该能够通过名称"识别向函数发送信号的 QLineEdit 对象.

I want to make QlineEdit fields double-clickable, so that a user's double-clicking on a QlineEdit field (e.g., 'qle01') calls a function. The function should be able to identify by 'name' the QLineEdit object which sent the signal to the function.

我不知道在下面的示例代码中用name"来描述qle01"和qle02"是否合适.也许更好的术语是把手".

I do not know if 'name' is the right word to describe 'qle01' and 'qle02' in my example code below. Maybe a better term would be a 'handle'.

在我下面的脚本中,如果双击 qle01,我的目标是打印第 9 行,QLineEdit 字段的名称是‘qle01’."如果您能帮助我弄清楚如何使第 9 行打印QLineEdit 字段的名称为‘qle01’."

In my script below, if qle01 was double-clicked, my goal is to have line 9 print, "The QLineEdit field's name is 'qle01'." I would appreciate help in figuring out how to make line 9 print "The QLineEdit field's name is 'qle01'."

在信用到期的地方给予信用,除了我在第 9 行的伪代码之外,下面的其余代码来自 mouseDoubleClickEvent with QLineEdit

Givng credit where credit is due, except for my pseudo code on line 9, the rest of the coding below is drawn from Example No. 1 in a StackOverflow posting at mouseDoubleClickEvent with QLineEdit

import sys

from PyQt4 import QtGui

class LineEdit(QtGui.QLineEdit):
    def mouseDoubleClickEvent(self, event):
        print("pos: ", event.pos())
        # The next line is pseudo-code, because I don't know how to properly code it
        print("The QLineEdit field's name is '" + ['qle01' or 'qle02'] + "'") # i.e., depending on which of the 
                                                                       # QLineEdit fields was double-clicked  

class Widget(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        QtGui.QWidget.__init__(self, *args, **kwargs)
        qle01 = LineEdit()
        qle02 = LineEdit()
        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(qle01)
        lay.addWidget(qle02)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

我有一些关于如何将句柄或名称传递给槽/函数的想法,但我没有得到任何有用的东西.

I had a couple of ideas about how to get the handle or name passed to the slot/function, but I have not gotten anywhere useful.

  1. 一个想法是将 QLineEdit 字段的信号发送到该字段的 QWidget.effectiveWinId(窗口系统标识符)的功能槽,但我不知道如何访问 QWidget.effectiveWinId.

  1. One idea was to have the QLineEdit field's signal send to the fuction-slot the field's QWidget.effectiveWinId (window system identifer), but I could not figure out how to access the QWidget.effectiveWinId.

另一个想法是使用我在许多帖子和教程中看到的 sender() 函数(例如 如何确定是谁发出了信号?).我尝试使用 sender() 函数如下:

Another idea was to use the sender() function that I have seen mentioned in many postings and tutorials (e.g. How to determine who emitted the signal?). I tried to use the sender() function as follows:

class ObjectName(object):
@QtCore.pyqtSlot()
def __getattribute__(self, name):
    print "getting `{}`".format(str(name))
    print('str(self.sender()) = ' + str(self.sender()))      

但最后一行产生了这个输出:str(self.sender()) = None.

But the last line produced this output: str(self.sender()) = None.

我在位于 的 PyQt4 参考指南下找不到对 sender() 函数的任何引用https://www.riverbankcomputing.com/static/Docs/PyQt4/.所以我不了解 sender() 函数,我显然不知道如何使用它.

I cannot find any reference to the sender() function under the PyQt4 Reference Guide located at https://www.riverbankcomputing.com/static/Docs/PyQt4/. So I do not understand the sender() function, and I obviously cannot figure out how to use it.

所以,最重要的是,我希望您能帮助我弄清楚如何让第 9 行打印QLineEdit 字段的名称是‘qle01’."

So, bottom line, I would appreciate help in figuring out how to make line 9 print "The QLineEdit field's name is 'qle01'."

推荐答案

变量名不标识对象,例如以下代码:

The name of a variable does not identify the object, for example in the following code:

qle01 = LineEdit()
foo_name = qle01

标识 QLineEdit 的变量的名称是什么?好吧 qle01 和 foo_name 因为它们都是内存空间的别名.

What is the name of the variable that identifies the QLineEdit? Well qle01 and foo_name since both are alias to a memory space.

可以做的就是标识指向同一个对象的所有变量都具有相同id的对象.

What can be done is to identify the object that all the variables that point to the same object will have the same id.

另一方面,最好实现一个信号来通知是否在 QLineEdit 中进行了双击,因为这将允许我们通过 QObject 的 sender() 方法获取对象.

On the other hand it is better to implement a signal to notify if doubleclick was made in the QLineEdit since that will allow us to obtain the object through the sender() method of the QObject.

import sys

from PyQt4 import QtCore, QtGui


class LineEdit(QtGui.QLineEdit):
    doubleClicked = QtCore.pyqtSignal()

    def mouseDoubleClickEvent(self, event):
        self.doubleClicked.emit()
        super(LineEdit, self).mouseDoubleClickEvent(event)


class Widget(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        super(Widget, self).__init__(*args, **kwargs)
        self.qle01 = LineEdit(doubleClicked=self.on_doubleClicked)
        self.qle02 = LineEdit(doubleClicked=self.on_doubleClicked)
        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(self.qle01)
        lay.addWidget(self.qle02)

    @QtCore.pyqtSlot()
    def on_doubleClicked(self):
        if self.sender() is self.qle01:
            print("The QLineEdit field's name is 'qle01'.")
        elif self.sender() is self.qle02:
            print("The QLineEdit field's name is 'qle02'.")


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

这篇关于如何确定哪个小部件发出信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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