Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在QtGui.QPainterPath.addRect()中绘制颜色?_Python_Pyqt_Pyqt5_Qgraphicsview_Qgraphicsscene - Fatal编程技术网

Python 如何在QtGui.QPainterPath.addRect()中绘制颜色?

Python 如何在QtGui.QPainterPath.addRect()中绘制颜色?,python,pyqt,pyqt5,qgraphicsview,qgraphicsscene,Python,Pyqt,Pyqt5,Qgraphicsview,Qgraphicsscene,我试图在PyQt5中绘制rect。但我总是有点不对劲。我指的是“QPainterPath文档”,有一个例子: path = QPainterPath() path.addRect(20, 20, 60, 60) path.moveTo(0, 0) path.cubicTo(99, 0, 50, 50, 99, 99) path.cubicTo(0, 99, 50, 50, 0, 0) QPainter painter(self) painter.fillRect(0, 0, 100,

我试图在PyQt5中绘制rect。但我总是有点不对劲。我指的是“QPainterPath文档”,有一个例子:

path = QPainterPath()
path.addRect(20, 20, 60, 60)

path.moveTo(0, 0)
path.cubicTo(99, 0,  50, 50,  99, 99)
path.cubicTo(0, 99,  50, 50,  0, 0)

QPainter painter(self)
painter.fillRect(0, 0, 100, 100, Qt.white)
painter.setPen(QPen(QColor(79, 106, 25), 1, Qt.SolidLine,
                    Qt.FlatCap, Qt.MiterJoin))
painter.setBrush(QColor(122, 163, 39))

painter.drawPath(path)
我自己尝试过,但我无法理解什么是“QPainter painter(self)”以及它在那里的工作方式,我找不到QPainter命令。 下面是我的代码示例:

from PyQt5 import QtGui, QtCore, QtWidgets
import sys
class testUi(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(testUi, self).__init__(parent)
        self.window = 'vl_test'
        self.title = 'Test Remastered'
        self.size = (1000, 650)

        self.create( )

    def create(self):
        self.setWindowTitle(self.title)
        self.resize(QtCore.QSize(*self.size))
        self.testik = test(self)

        self.mainLayout = QtWidgets.QVBoxLayout( )
        self.mainLayout.addWidget(self.testik)
        self.setLayout(self.mainLayout)


class test(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(test, self).__init__(parent)
        self._scene = QtWidgets.QGraphicsScene( )
        self.setScene(self._scene)
        self.drawSomething( )

    def drawSomething(self):
        self.path = QtGui.QPainterPath( )
        self.path.moveTo(0, 0)
        self.path.addRect(20, 20, 60, 60)

        self._scene.addPath(self.path)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    v = testUi()
    v.show()
    sys.exit(app.exec_())
在我的代码中,我没有在“addRect”之后写任何东西。因此,任何人都可以清楚地知道如何使用“QPainter”。谢谢您的时间

附言 附加问题:在QGraphicScene中绘制基本体并与之交互的最快方法是什么?因为我可以看到很多绘制曲线/多边形并对其进行操作的方法,但对于工作流来说,什么是最有效的方法呢?对不起,我的英语是addPath()方法返回一个QGraphicsPathItem,该项继承自QAbstractGraphicsShapeItem,该项允许您分别使用setBrush()和setPen()设置填充和边框颜色

def draw something(self):
self.path=QtGui.QPainterPath()
self.path.moveTo(0,0)
self.path.addRect(20,20,60,60)
item=self.\u scene.addPath(self.path)
#或
#item=qtwidts.QGraphicsPathItem(路径)
#self.\u场景添加项(项目)
item.setPen(
QtGui.QPen(
QtGui.QColor(79106,25),
1.
QtCore.Qt.SolidLine,
QtCore.Qt.FlatCap,
QtCore.Qt.MiterJoin,
)
)
项目.退刀(QtGui.QColor(122、163、39))

在QGraphicScene中,绘制基本体并与之交互的最快方法是什么?因为我可以知道,绘制曲线/多边形并操纵它们的方法有很多,但对于工作流来说,什么方法最有效

Qt中的整个绘制系统最终使用QPainter,尽管我前面展示的代码没有看到明确的用法,但它正在使用,因为QGraphicsItem有一个使用QPainter、笔刷和笔的绘制

此外,QPainter经过优化,因此不必担心Qt端的优化

QGraphicsView和QGraphicscene是一个基于QGraphicsSitem的高级框架,因此一般来说,您应该能够使用预定义项构建任何内容,但如果您无法创建自己的QGraphicsSitem(实现方法paint()和boundingRect())

如果要绘制不想与之交互的永久对象(移动、单击等),则可以在绘制项目之前或之后分别使用QGraphicscene的drawForeGround或drawForeGround方法

欲了解更多信息,请阅读: