0

I'm trying to make a GUI Application that change the icon on the right clicked button! This is my simple code:

import sys
from PySide.QtCore import *
from PySide.QtGui import *

class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi()

    def setupUi(self):
        widget = QWidget()
        layout = QGridLayout()
        self.buttons = list()

        for x in range(3):
            row = list()
            for y in range(3):
                button = QPushButton(QIcon('Empty-Cell.png'), '{},{}'.format(x, y))
                button.clicked.connect(self.button_click)
                row.append(button)
                layout.addWidget(button, x, y)
            self.buttons.append(row)

        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def button_click(self):
        # Change icon HERE!

def main():
    app = QApplication(sys.argv)
    ui = Ui_MainWindow()
    ui.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

and here the image:

Link of the GUI Application

I've been trying for hours but i'm not still able to do that, any ideas? I would like also to use an Image Widget instead of a button, label, or whatelse if it is possible..

Thank you!

Carlo Beccarini
  • 97
  • 2
  • 14

1 Answers1

0

You can use QObject::sender to get the clicked button.

def button_click(self):
    test_pixmap = QPixmap(16, 16)
    test_pixmap.fill(Qt.red)
    self.sender().setIcon(QIcon(test_pixmap))
Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • Well, it works! Is there any way to make a widget image clickable, instead using a button? – Carlo Beccarini Jan 25 '14 at 17:15
  • What do you mean by widget image? You can get click events of any widget (e.g. a `QLabel` that can display pixmaps) by installing an event filter on it. I.e. call `self.label.installEventFilter(self)` and implement `eventFilter` method of your main window class. – Pavel Strakhov Jan 25 '14 at 20:06