繁星淼淼
这取决于您要使用的油漆级别:有2个选项:使用 QPainter:这种效果可以通过绘制 2 个置换矩形来实现,其中背景矩形的颜色比前面的颜色深:from PyQt5 import QtCore, QtGui, QtWidgetsdef 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_())使用 QGraphicsDropShadowEffect:在这种情况下, QWidget 和 QGraphicsItem 支持这种效果:from PyQt5 import QtCore, QtGui, QtWidgetsif __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_())
一只斗牛犬
据我所知,没有内置的 PyQt 3D 绘制小部件/功能,因为您只能绘制 2D 多边形。但是我们可以创建一个自定义类来模拟 3D 绘画。从您的 Java 链接参考:Java 支持 3D 矩形,但三维效果不是很明显。由于海拔较低,影响可以忽略不计。Java 设计人员通过沿矩形边框绘制较浅和较深的线条来提供 3D 效果。我们可以模拟Java的3D绘制功能的效果:void fill3DRect(int x, int y, int width, int height, boolean raised)此方法使用上述指定的参数绘制一个实心 3D 矩形。最后一个布尔参数true表示高于绘图表面的高度,false表示蚀刻到表面。为了在 Python 中获得 3D 效果,我们基本上可以通过具有两种颜色的阴影然后使某些侧面变暗和照亮来做同样的事情。from PyQt5 import QtCore, QtGui, QtWidgetsimport sysclass 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_())