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.
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.
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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…