QWidget画家

时间:2017-02-20 13:10:35

标签: qt pyqt qwidget

我有这段代码:

import sys, random, pprint
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5 import QtGui
from PyQt5.QtCore import Qt

class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(QtCore.QRect(200, 200, 700, 700))

        self.widget = QtWidgets.QWidget(self)
        self.widget.setGeometry(QtCore.QRect(10, 10, 400, 200))
        self.widget.setObjectName("widget")

    def paintEvent(self, event):
        painter = QtGui.QPainter()
        painter.begin(self.widget)
        self.drawPoints(painter)
        painter.end()

    def drawPoints(self, painter):
        #drawing code


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = Window()
    ex.show()
    sys.exit(app.exec_())

我期待什么:由drawPoints()绘制的区域(400x200)。 QWidget继承QPaintDevice。所以这段代码应该可行。但控制台说:

  

QPainter :: begin:Paint device返回引擎== 0,输入:1

     

QPainter :: end:画家未激活,已中止

请帮助我。

1 个答案:

答案 0 :(得分:0)

看起来您正在尝试复制this example,对吗?

主要区别似乎是您为主窗口定义print( df .head(10) .to_string( formatters={"total_bill": "${:,.2f}".format, "tip": "${:,.2f}".format } ) ) ,而不是您实际尝试绘制的小部件。如果您创建了qi::int_parser<sint64, 10> dec64; const qi::rule<const char *, sint64()> nsRule = dec64 >> qi::lit("ns"); const qi::rule<const char *, sint64()> usRule = dec64 >> qi::lit("us"); const qi::rule<const char *, sint64()> msRule = dec64 >> qi::lit("ms"); const qi::rule<const char *, sint64()> sRule = dec64 >> qi::lit("s"); const qi::rule<const char *, sint64()> mRule = dec64 >> qi::lit("m"); const qi::rule<const char *, sint64()> hRule = dec64 >> qi::lit("h"); auto result = qi::parse(f, f + length, -hRule >> -mRule >> -sRule >> -msRule >> -usRule >> -nsRule, h, m, s, ms, us, ns); 的自定义子类,在那里定义了<html> <head> <script type="text/javascript"> var timeleft = 5; var mySpace = document.getElementById('space'); var myHeader = document.getElementById('header'); var downloadTimer = setInterval(function(){ timeleft--; document.getElementById("countdowntimer").textContent = timeleft; if(timeleft <= 0){ document.getElementById("space").style.display = "none"; document.getElementById("countdowntimer").style.display = "none"; document.getElementById("header").style.position = "initial"; clearInterval(downloadTimer); } },1000); </script> <style> body { margin: 0; } .header { background-color: gray; height: 10%; top: 0; width: 100%; } </style> </head> <body> <div class="header" id="space"></div> <div class="header" id="header" style="position:fixed"> <span id="countdowntimer">5</span> </div> <img src="http://scripts.semilo.com/mitchel/Weersverwachting.png" width="auto" height="auto"> </body> </html> ,然后将其插入到标准paintEvent()实例中,它将起作用,如下所示:

QWidget

至少,它不再打印该错误消息了。