Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

python - 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 (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.

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'.

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'."

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. 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.

  2. 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()))      
    

But the last line produced this output: 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/. So I do not understand the sender() function, and I obviously cannot figure out how to use it.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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? Well qle01 and foo_name since both are alias to a memory space.

What can be done is to identify the object that all the variables that point to the same object will have the same 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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...