添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode . Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

Hello,

I am making a simple program in Python with PyQT 5 for GUI, which is supposed to draw a square onto the screen.

However, I am quite confused as to how I am supposed to set up my QPainter.

I have the following code:

from PyQt5 import QtWidgets, QtGui, QtCore
import sys
def paintEvent(self, x, y, color, event = None):
    global qp
    qp = QtGui.QPainter(self);
    qp.begin(self)
    qp.setPen(QtGui.QColor(int(color, 16)))
    qp.setBrush(QtGui.QColor(int(color, 16)))
    qp.drawRect(x, y, 25, 25)
    qp.end()
def window(title, width, height):
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QWidget()
    w.setFixedSize(width, height)
    w.setWindowTitle(title)
    paintEvent(w, 0, 0, 'ffffff')
    w.show()
    sys.exit(app.exec_())
window('test', 120, 120)

But this gives me:

QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setPen: Painter not active
QPainter::setBrush: Painter not active
QPainter::drawRects: Painter not active
QPainter::end: Painter not active, aborted

Can someone point me in the right direction?

Thanks,

P.S First post. new to Qt - don't be harsh ;)