PyQt Child Widget不接收paintEvent

时间:2011-08-29 09:44:56

标签: qt pyqt paintevent

我在qt designer中创建了一个小部件,并使用pyuic将ui文件转换为名为Ui_wid_canvas的python类。这应该用作特殊画布:

# file mgcanvas.py
from PyQt4 import QtCore, QtGui

class Ui_wid_canvas(object):
    def setupUi(self, wid_canvas):
        wid_canvas.setObjectName("wid_canvas")
        wid_canvas.resize(400, 300)
        self.horizontalLayout = QtGui.QHBoxLayout(wid_canvas)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pushButton = QtGui.QPushButton(wid_canvas)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)

        self.retranslateUi(wid_canvas)
        QtCore.QMetaObject.connectSlotsByName(wid_canvas)

    def retranslateUi(self, wid_canvas):
        wid_canvas.setWindowTitle(QtGui.QApplication.translate("wid_canvas", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("wid_canvas", "PushButton", None, QtGui.QApplication.UnicodeUTF8))

从Ui_wid_canvas我派生了一个MyCanvas类来实现paintEvent函数和一些实用函数,比如moo()。在痛苦的事件中,所有它应该做的是绘制两个rects。如果我使用下面的类作为我的应用程序,一切都像魅力一样。

# file mycanvas.py
from PyQt4 import QtCore, QtGui
import mgcanvas


class MyCanvas(mgcanvas.Ui_wid_canvas, QtGui.QWidget):
    def __init__(self):
        super(mgcanvas.Ui_wid_canvas, self).__init__()
        self.setupUi(self)

    def paintEvent(self, qpaintevent):
        print "PaintEvent canvas"
        painter = QtGui.QPainter(self)
        painter.setBrush(QtGui.QColor(255,0,0,80))
        painter.setPen(QtGui.QColor(00,00,00,255))

        painter.drawRect(10,10,100,100)
        r = QtCore.QRectF(110,110,100,100)
        painter.drawRect(r)
        painter.drawText(r,"Hello", QtGui.QTextOption(QtCore.Qt.AlignCenter))



    def moo(self):
        print "This is canvas mooing"

现在,当我创建一个应用程序测试实例化MyCanvas(见下文)时,将调用paintEvent for Test,但永远不会调用MyCanvcas的痛苦事件,不会绘制rects,也不会在控制台上输出“Paintevent Canvas”。如果我在Test.paintevent()中调用self.widget.update()self.widget.redraw(),则不会捕获paintevent。如果我手动调用self.widget.paintevent(),则调用该函数,但画家未激活。另一方面,显示了按钮,我从中可以看出正确包含了小部件,但是子小部件不会调用paint事件。

    # file test.py; executed with `python test.py`
    from PyQt4 import QtCore, QtGui
    import mycanvas

    class Test(object):
        def setupUi(self, Gui):
            self.counter = 0
            Gui.setObjectName("TestObject")
            Gui.resize(500,500)
            self.layout = QtGui.QVBoxLayout()
            self.widget = mycanvas.MyCanvas()
            self.widget.setupUi(self)
            self.widget.setObjectName("wid_canvas")
            self.layout.addWidget(self.widget)

            self.retranslateUi(Gui)
            QtCore.QMetaObject.connectSlotsByName(Gui)

        def retranslateUi(self, Gui):
            Gui.setWindowTitle(QtGui.QApplication.translate("TestObject", "Title", None, QtGui.QApplication.UnicodeUTF8))


        def paintEvent(self, qpaintevent):
            print "---> Enter"
            self.counter += 1
            print "counter", self.counter
            self.widget.repaint()
            self.widget.moo()
            print "<-- Leave"

    class MyTest(Test, QtGui.QWidget):
        def __init__(self):
            super(Test, self).__init__()
            self.setupUi(self)

    if __name__ == '__main__':
        import sys
        app = QtGui.QApplication(sys.argv)
        ui = MyTest()
        ui.show()

        sys.exit(app.exec_())

设置Qt.WA_PaintOutsidePaintEvent不是一个选项,因为它在Mac和Windows上不起作用,但我想保持平台无关。

请原谅我发布了这么多代码,但我想这会让事情变得更容易。我试图将它保持在最低限度。有人能告诉我如何将Widget MyCanvas绘制在自身上,并将此绘制小部件包含在另一个小部件MyTest中,它将作为应用程序使用吗?

1 个答案:

答案 0 :(得分:1)

在您的类Test中,您没有将布局附加到参数Gui,方法是将其作为参数传递给QVBoxLayout,并且为self.widget.setupUi调用了MyCanvas,尽管它是已由MyCanvas构造函数调用。

class Test(object):
    def setupUi(self, Gui):
        self.counter = 0
        Gui.setObjectName("TestObject")
        Gui.resize(500,500)
        self.layout = QtGui.QVBoxLayout(Gui)
        self.widget = mycanvas.MyCanvas()
        self.widget.setObjectName("wid_canvas")
        self.layout.addWidget(self.widget)

        self.retranslateUi(Gui)
        QtCore.QMetaObject.connectSlotsByName(Gui)