【问题标题】:PyQt5 update MainWindow after paintEventPyQt5在paintEvent之后更新MainWindow
【发布时间】:2016-11-29 23:39:50
【问题描述】:

我对 QT 很陌生。 我正在尝试在单击按钮后绘制一些矩形。 paintEvent 方法似乎有效,但在单击按钮后没有任何反应 我想我需要以某种方式更新 mainWindow 。 代码:

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);,矩形就起来了。

【问题讨论】:

    标签: python qt pyqt


    【解决方案1】:

    您可以在pridaj_slide 函数中调用self.repaint() 来更新显示(它将再次调用paintEvent):

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 2018-04-11
      • 2021-11-04
      • 1970-01-01
      相关资源
      最近更新 更多