2014-01-25 17 views
0

마우스 오른쪽 버튼으로 아이콘을 변경하는 GUI 응용 프로그램을 만들려고합니다! 이 내 간단한 코드입니다 : 이미지 여기버튼 아이콘 변경 클릭

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

과 :

Link of the GUI Application

내가 시간 동안 노력 했어하지만 난 아직도 그 일을 할 수 아니에요, 어떤 아이디어? 가능한 경우 단추, 레이블 또는 whatelse 대신 Image Widget을 사용하고 싶습니다.

Thank you!

답변

0

QObject::sender을 사용하면 클릭 한 버튼을 얻을 수 있습니다.

def button_click(self): 
    test_pixmap = QPixmap(16, 16) 
    test_pixmap.fill(Qt.red) 
    self.sender().setIcon(QIcon(test_pixmap)) 
+0

음, 작동합니다. 버튼을 사용하는 대신 위젯 이미지를 클릭 할 수있게 할 수있는 방법이 있습니까? –

+0

위젯 이미지 란 무엇입니까? 이벤트 필터를 설치하여 위젯 (예 : 픽스맵을 표시 할 수있는 'QLabel')의 클릭 이벤트를 얻을 수 있습니다. 나는. 'self.label.installEventFilter (self)'를 호출하고 메인 윈도우 클래스의'eventFilter' 메소드를 구현하십시오. –