简体   繁体   English

如何确定哪个小部件发出信号

[英]How to determine which widget emitted the signal

I want to make QlineEdit fields double-clickable, so that a user's double-clicking on a QlineEdit field (eg, 'qle01') calls a function.我想让 QlineEdit 字段可双击,以便用户双击 QlineEdit 字段(例如,'qle01')调用函数。 The function should be able to identify by 'name' the QLineEdit object which sent the signal to the function.该函数应该能够通过“名称”识别向函数发送信号的 QLineEdit 对象。

I do not know if 'name' is the right word to describe 'qle01' and 'qle02' in my example code below.我不知道在下面的示例代码中,“名称”是否是描述“qle01”和“qle02”的正确词。 Maybe a better term would be a 'handle'.也许更好的术语是“手柄”。

In my script below, if qle01 was double-clicked, my goal is to have line 9 print, "The QLineEdit field's name is 'qle01'."在我下面的脚本中,如果双击 qle01,我的目标是打印第 9 行,“QLineEdit 字段的名称是‘qle01’。” I would appreciate help in figuring out how to make line 9 print "The QLineEdit field's name is 'qle01'."我很感激帮助弄清楚如何使第 9 行打印“QLineEdit 字段的名称是‘qle01’。”

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在信用到期的地方给予信用,除了我在第 9 行的伪代码之外,下面的其余代码来自使用 QLineEditmouseDoubleClickEvent 上发布的 StackOverflow 中的示例 1

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. 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.一种想法是将 QLineEdit 字段的信号发送到该字段的 QWidget.effectiveWinId(窗口系统标识符)的功能槽,但我不知道如何访问 QWidget.effectiveWinId。

  2. Another idea was to use the sender() function that I have seen mentioned in many postings and tutorials (eg How to determine who emitted the signal? ).另一个想法是使用我在许多帖子和教程中看到的 sender() 函数(例如, 如何确定谁发出了信号? )。 I tried to use the sender() function as follows:我尝试使用 sender() 函数如下:

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

But the last line produced this output: str(self.sender()) = None.但是最后一行产生了这个输出:str(self.sender()) = None。

I cannot find any reference to the sender() function under the PyQt4 Reference Guide located at https://www.riverbankcomputing.com/static/Docs/PyQt4/ .我在位于https://www.riverbankcomputing.com/static/Docs/PyQt4/的 PyQt4 参考指南下找不到对 sender() 函数的任何引用。 So I do not understand the sender() function, and I obviously cannot figure out how to use it.所以我不理解 sender() 函数,我显然不知道如何使用它。

So, bottom line, I would appreciate help in figuring out how to make line 9 print "The QLineEdit field's name is 'qle01'."因此,最重要的是,我希望您能帮助我弄清楚如何使第 9 行打印“QLineEdit 字段的名称为‘qle01’”。

The name of a variable does not identify the object, for example in the following code:变量的名称不标识对象,例如在以下代码中:

qle01 = LineEdit()
foo_name = qle01

What is the name of the variable that identifies the QLineEdit?标识 QLineEdit 的变量的名称是什么? Well qle01 and foo_name since both are alias to a memory space.好吧 qle01 和 foo_name 因为它们都是内存空间的别名。

What can be done is to identify the object that all the variables that point to the same object will have the same id.可以做的就是标识指向同一对象的所有变量将具有相同id的对象。

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

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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM