在PyQt中绘制具有提升和降低效果的3D矩形/多边形

时间:2019-04-10 11:35:09

标签: python 3d pyqt

在Java中,您可以使用(请参见https://way2java.com/awt-graphics/4891/)绘制3-d矩形:

void fill3DRect(int x, int y, int width, int height, boolean raised)

在这里,最后一个参数“ raised”用于相对于绘图表面降低/升高3d矩形。

如何在PyQt中实现这种效果?

2 个答案:

答案 0 :(得分:1)

据我所知,没有内置的PyQt 3D绘画小部件/功能,因为您只能绘画2D多边形。但是我们可以创建一个自定义类来模拟3D绘画。从您的Java链接的reference

  

Java支持3D矩形,但是三维效果并不十分明显。随着海拔的降低,影响可忽略不计。 Java设计师通过沿矩形边框绘制较浅和较深的线条来赋予3D效果。

我们可以模仿 Java 3D绘画功能的效果:

void fill3DRect(int x, int y, int width, int height, boolean raised)
  

此方法使用上述指定参数绘制一个实体3D矩形。最后一个布尔参数 true 表示图纸表面上方的高程,而 false 则表示蚀刻到绘图表面中。

要在Python中获得3D效果,我们基本上可以通过使两种颜色的阴影变暗并照亮某些侧面来完成相同的操作。

Example pic

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Rectangle3D(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)

        # Elevated 3D rectangle color settings
        self.elevated_border_color = QtGui.QColor(111,211,111)
        self.elevated_fill_color = QtGui.QColor(0,255,0)
        self.elevated_pen_width = 2.5

        # Lowered 3D rectangle color settings
        self.lowered_border_color = QtGui.QColor(0,235,0)
        self.lowered_fill_color = QtGui.QColor(0,178,0)
        self.lowered_pen_width = 2.5

    def draw3DRectangle(self, x, y, w, h, raised=True):
        # Specify the border/fill colors depending on raised or lowered
        if raised:
            # Line color (border)
            self.pen = QtGui.QPen(self.elevated_border_color, self.elevated_pen_width)
            # Fill color
            self.fill = QtGui.QBrush(self.elevated_fill_color)
        else:
            # Line color (border)
            self.pen = QtGui.QPen(self.lowered_border_color, self.lowered_pen_width)
            # Fill color
            self.fill = QtGui.QBrush(self.lowered_fill_color)

        painter = QtGui.QPainter(self)

        # Draw border color of rectangle
        painter.setPen(self.pen)
        painter.setBrush(self.fill)  
        painter.drawRect(x, y, w, h)

        # Cover up the top and left sides with filled color using lines
        if raised:
            painter.setPen(QtGui.QPen(self.elevated_fill_color, self.elevated_pen_width))
        else:
            painter.setPen(QtGui.QPen(self.lowered_fill_color, self.lowered_pen_width))

        painter.drawLine(x, y, x + w, y) 
        painter.drawLine(x, y, x, y + h)

    def paintEvent(self, event):
        self.draw3DRectangle(50,50,300,150,True)
        self.draw3DRectangle(50,250,300,150,False)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv) 

    widget = Rectangle3D()
    widget.show()

    sys.exit(app.exec_())

答案 1 :(得分:1)

这取决于您要使用的油漆水平:

有2个选项:

  • 使用QPainter:

此效果可以通过绘制2个置换后的矩形来实现,其中背景矩形的颜色比正面的颜色深:

from PyQt5 import QtCore, QtGui, QtWidgets


def draw3DRect(painter, rect, color, raised=False, offset=QtCore.QPoint(4, 4)):
    if raised:
        painter.fillRect(rect.translated(offset), color.darker())
    painter.fillRect(rect, color)


class Widget(QtWidgets.QWidget):
    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        r = QtCore.QRect(
            self.width() / 4,
            self.height() / 4,
            self.width() / 2,
            self.height() / 2,
        )
        draw3DRect(painter, r, QtGui.QColor("green"), raised=True)

    def sizeHint(self):
        return QtCore.QSize(320, 240)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

enter image description here

  • 使用QGraphicsDropShadowEffect:

在这种情况下,QWidget和QGraphicsItem支持这种效果:

from PyQt5 import QtCore, QtGui, QtWidgets


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QWidget()
    lay = QtWidgets.QHBoxLayout(w)
    scene = QtWidgets.QGraphicsScene()
    view = QtWidgets.QGraphicsView(scene)
    rect_item = QtWidgets.QGraphicsRectItem(QtCore.QRectF(0, 0, 200, 100))
    rect_item.setBrush(QtGui.QColor("green"))
    effect_item = QtWidgets.QGraphicsDropShadowEffect(
        offset=QtCore.QPointF(3, 3), blurRadius=5
    )
    rect_item.setGraphicsEffect(effect_item)
    scene.addItem(rect_item)

    rect_widget = QtWidgets.QWidget()
    rect_widget.setFixedSize(320, 240)
    rect_widget.setStyleSheet("background-color:green;")
    effect_widget = QtWidgets.QGraphicsDropShadowEffect(
        offset=QtCore.QPointF(3, 3), blurRadius=5
    )
    rect_widget.setGraphicsEffect(effect_widget)

    lay.addWidget(view)
    lay.addWidget(rect_widget)
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

enter image description here