Answer the question
In order to leave comments, you need to log in
How to transfer additional the data in kontrol and then it to consider in the event handler?
Python 2.7, PyQt4
I create controls in a loop:
water = QPixmap ("images//water.png")
for position in positions:
label = QLabel()
label.mousePressEvent = self.labelClicked
label.setPixmap(water)
label.setToolTip(str(position[0]) + str(position[1]))
label.answer = position
def labelClicked(self, event):
sender = self.sender()
a = sender.answer[0]
self.setWindowTitle(str(a))
Answer the question
In order to leave comments, you need to log in
Why didn't inheritance work for you?
Try this example using inheritance and signals:
from PyQt4 import QtGui, Qt
class MyLabel(QtGui.QLabel):
clicked = Qt.pyqtSignal(str)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def mousePressEvent(self, *args, **kwargs):
self.clicked.emit(self.text())
return QtGui.QLabel.mousePressEvent(self, *args, **kwargs)
class Main(QtGui.QWidget):
def __init__(self):
super().__init__()
layout = QtGui.QVBoxLayout(self)
for i in range(5):
label = MyLabel(self, "Label " + str(i))
label.clicked.connect(self.set_title)
layout.addWidget(label)
self.resize(300, 300)
def set_title(self, label):
self.setWindowTitle(label)
if __name__ == '__main__':
app = Qt.QApplication([])
m = Main()
m.show()
app.exec()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question