如何在pyqt5中使用QGraphicsView在白色区域(1280 * 720)上绘制灰色方块(200 * 100)?

时间:2017-09-19 10:31:37

标签: python pyqt nodes pyqt5 diagram

伙计们,我想要的是在白色区域(1280 * 720)上绘制一个灰色方块(200 * 100),但我不知道如何使用QGraphicsView,你能帮助我吗?

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import  sys


class AAA(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.resize(1280, 720)
        self.setStyleSheet('background:white')
        self.show()

        Xscene = QGraphicsScene()
        self.setScene(Xscene)

        gray_square=qDrawPlainRect(200,100)
        Xscene.addItem(gray_square)


A = QApplication(sys.argv)
aaa = AAA()
A.exec_()

1 个答案:

答案 0 :(得分:0)

你其实非常接近。请尝试以下方法。

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys


class AAA(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.resize(1280, 720)
        self.setStyleSheet('background:white')
        self.show()

        Xscene = QGraphicsScene()
        self.setScene(Xscene)

        # gray_square=qDrawPlainRect(200,100)
        gray_square = QGraphicsRectItem(0, 0, 200, 100)
        gray_square.setPen(Qt.gray)
        gray_square.setBrush(QBrush((Qt.gray)))
        Xscene.addItem(gray_square)

A = QApplication(sys.argv)
aaa = AAA()
A.exec_()