D
D
Dmitry Filyushin2016-08-19 05:48:28
Python
Dmitry Filyushin, 2016-08-19 05:48:28

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

I'm trying to get the data of the pressed QLabel in the event. Before that, there was a subclassing
option that also does not work. The output in the debugger is None. Tried copying position via copy.copy(position).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2016-08-19
@Avernial

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 question

Ask a Question

731 491 924 answers to any question