2016-11-29 2 views
-1

나는 QT를 처음 사용했다. 버튼 클릭 후 일부 직사각형을 페인트하려고합니다. paintEvent 메서드가 작동하는 것 같지만 버튼 클릭 후 아무 것도 발생하지 않습니다. 어떻게 든 mainWindow를 업데이트해야한다고 생각합니다. 코드 :PyQt5 업데이트 MainWindow after paintEvent

from PyQt5 import QtCore, QtGui, uic, QtWidgets 
from PyQt5.QtWidgets import QMainWindow, QApplication 
from PyQt5.QtGui import QPainter, QColor, QBrush 

qtCreatorFile = "gui.ui" # Enter file here. 

#This is where you add the file you created earlier. It is loaded using the inbuilt function 
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile) 

class MyApp(QtWidgets.QMainWindow): 

    pridat_slide = False 

    def __init__(self): 
     super(MyApp, self).__init__() 
     self.ui = Ui_MainWindow() 
     self.ui.setupUi(self) 
     self.ui.pridaj_slide.clicked.connect(self.pridaj_slide) 


    def pridaj_slide(self): 
     self.pridat_slide = True 

    def paintEvent(self, e): 
     qp = QPainter() 
     qp.begin(self) 
     self.drawRectangles(qp) 
     qp.end() 

    def drawRectangles(self, qp): 
     if self.pridat_slide: 
      print ("test"); 

      qp.setBrush(QColor(200, 0, 0)) 
      qp.drawRect(10, 15, 90, 60) 

      qp.setBrush(QColor(255, 80, 0, 160)) 
      qp.drawRect(130, 15, 90, 60) 

      qp.setBrush(QColor(25, 0, 90, 200)) 
      qp.drawRect(250, 15, 90, 60) 
      self.update() 
      self.pridat_slide = False 

if __name__ == "__main__": 
    try: 
     app = QtWidgets.QApplication(sys.argv) 
     window = MyApp() 
     window.show() 
     sys.exit(app.exec_()) 

화면 : 어떤 제안 & 개선을위한 "test" is printed

감사합니다.

문제점 MainWindow를 배경 색이었다 해결. background-color: rgb(145,145,145);을 삭제하고 사각형이 표시됩니다.

당신이 디스플레이를 업데이트 self.repaint()를 호출 할 수 있습니다

답변

0

pridaj_slide 기능에 (그것을 다시 paintEvent를 호출합니다) :

def pridaj_slide(self): 
    self.pridat_slide = True 
    self.repaint()