- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图了解 path1.subtracted(path2)
的工作原理。
这是代码:
from PyQt5.QtCore import QPointF
from PyQt5.QtCore import QRectF, Qt
from PyQt5.QtGui import QPainterPath, QPen
from PyQt5.QtGui import QPolygonF
from PyQt5.QtWidgets import QApplication, QGraphicsScene, \
QGraphicsView, QPushButton, QWidget, \
QVBoxLayout, QGraphicsItem, QGraphicsPathItem, QGraphicsRectItem
class Window(QWidget):
scene = None
def __init__(self):
QWidget.__init__(self)
self.view = View(self)
self.button = QPushButton('Clear View', self)
self.button.clicked.connect(self.handleClearView)
layout = QVBoxLayout(self)
layout.addWidget(self.view)
layout.addWidget(self.button)
def handleClearView(self):
self.view.scene.clear()
class View(QGraphicsView):
def __init__(self, parent):
self.scribing = False
self.erasing = False
QGraphicsView.__init__(self, parent)
self.scene = QGraphicsScene()
self.setScene(self.scene)
def resizeEvent(self, QResizeEvent):
self.setSceneRect(QRectF(self.viewport().rect()))
def mousePressEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.scribing = True
self.path1 = QPainterPath()
self.path2 = QPainterPath()
self.polygon1 = QPolygonF()
self.polygon1.append(QPointF(100,100))
self.polygon1.append(QPointF(100, 300))
self.polygon1.append(QPointF(300, 300))
self.polygon1.append(QPointF(300, 100))
self.polygon2 = QPolygonF()
self.polygon2.append(QPointF(300,100))
self.polygon2.append(QPointF(300, 300))
self.polygon2.append(QPointF(100, 300))
self.path1.addPolygon(self.polygon1)
self.path2.addPolygon(self.polygon2)
path3 = self.path1.subtracted(self.path2)
# self.scene.addPath(self.path1, QPen(Qt.blue))
# self.scene.addPath(self.path2, QPen(Qt.green))
self.scene.addPath(path3, QPen(Qt.red))
if event.buttons() == Qt.RightButton:
self.erasing = True
def mouseMoveEvent(self, event):
if (event.buttons() & Qt.LeftButton) and self.scribing:
if self.free_draw_item:
pass
if event.buttons() & Qt.RightButton and self.erasing:
pass
def mouseReleaseEvent(self, event):
self.scribing = False
self.erasing = False
# if self.eraser_item != None:
# self.scene.removeItem(self.eraser_item)
# if self.free_draw_item != None:
# self.free_draw_item.setSelected(True)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())
在此示例中,我使用QPolygonF
。我还尝试创建 p1=QPainterPath()
、p2=QPainterPath()
并减去以得到 p3
。但是,没有成功,得到相同的结果。
最佳答案
QpainterPath.subtracted()
不会减去路径元素,而是减去路径区域, see documentation
如果使用QpainterPath::operator-()
,效果相同:
# path3 = self.path1.subtracted(self.path2)
path3 = self.path1 – self.path2
您可以通过这样的方式识别路径的元素
c = path3.elementCount()
for i in range(c):
e = path3.elementAt(i)
print('Element-nr.: ', i, 'Type: ', e.type, 'x: ', e.x, 'y: ', e.y) # type: 0 = MoveTo, 1 = LineTo
我认为,你必须编写一个自己的方法,从path1和path2的元素创建path3。
关于python - 如何减去QPainterPaths、QPolygon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39726477/
我有 QPainterPath。我需要通过 x 找到 QPainterPath 的 y 坐标。 我在QPainterPath中找到了intersected()方法。因此,我创建了新的 QPainter
我需要“合并”两个 QPainterPath一起。问题是它们似乎总是两条不同的路径。 我需要的是,合并实际上变得与已构建单个(来自复杂的多边形或复杂的线束)相同,没有任何内部线伪影或子路径,因为它们以
我想显示一个动画箭头形状的按钮。 为了绘制它,我创建了一个类,它继承了 QGraphicsObject 并使用了 QPainterPath 类。 我在 QGraphicsScene 中绘制它并使用属性
有没有办法缩放 QPainterPath ? 例如,我的问题是我有一个 QPainterPath 400,400 的大小包含许多行,我想将其缩放到 800,800 或任何其他大小,并通过向其他坐标添加
在 Qt 5.4.2 中,我使用 QPainterPath 来呈现文本,因为文本可能会被分割成多个字符,并且每个字符都沿着一条曲线呈现。我在不同平台和打印时看到不同的结果。为了呈现文本,我尝试使用 Q
我正在处理一个图形编辑器的功能,我正在编辑圆弧,当形状是椭圆时,QPainterPath::arcTo 的行为并不像我预期的那样;当它是一个圆圈时,它按预期工作。 下面的两张图片显示了结果。在第一种情
有什么方法可以获取 QPainterPath 并将其展开,就像 Photoshop 中的“选择”>“增长...”(或“展开...”)命令一样? 我想获取从 QGraphicsItem::shape 返
我正在尝试确定命中扫描射弹的路径(基本上是一条线,但在我的示例中将其表示为 QPainterPath)与场景中的项目相交的点。我不确定是否有办法使用 QPainterPath、QLineF 等提供的函
如何获得代表沿 QPainterPath 单击的点的百分比。例如,假设我有一条线,如下图所示,用户单击 QPainterPath(由红点表示)。我想记录该点沿路径下降的百分比。在本例中,它将打印 0.
我正在实现自由格式绘图,使用鼠标按下并使用 Qpainter 移动来绘制自由路径 QPainterpath 现在我必须检测绘制的路径何时与另一条路径交叉或相交。我如何识别线何时在某个点相互交叉并向用户
我想在 Qt 中创建这样的形状: 这是一段代码(基本上是绘制一个矩形并在其上绘制一条弧线): QPainterPath groupPath; QPen pen; pen.setCosmetic(
我有一个 QPainterPath,我想裁剪一个 QPixmap 图像。此代码对我有用,但我想使用 PyQt5 内置功能像没有 numpy 的面具 # read image as RGB and ad
使用 QPen 时抚摸QPainterPath , 它在线的中心这样做。我想做的是沿着线的内部 绘制路径。我已经看过类似的问题here但它没有提供明确的解决方案或示例。 如何描边 QPainterPa
以下代码围绕 QPainterPath 绘制文本: QString text = "Short text"; QPainterPath rawPath; // fill path ... while
我正在制作的迷你游戏现在遇到了问题。问题如下:我为我的游戏创建了一个关卡编辑器,因此我必须创建自己的委托(delegate)和模型,当我尝试通过 shapeeditor(更有可能创建一个 painte
有人能告诉我如何访问 QPainterPath 下的所有像素吗? QPainterPath 的元素有一些方法,例如isLineTo(),所以我的第一个想法是创建一个具有起点和终点的线性函数。但如果路径
我正在尝试使用 QGraphicsPathItem 来表示其中有一个洞的多边形(想想 donut )。我可以让它正确绘制,并且中心是透明的。但是,启用项目选择或移动后,我可以在单击孔时与我的对象进行交
我正在 PySide 中使用 QGraphicsScene 创建自定义绘图应用程序 - 我希望能够绘制复杂的形状并让用户通过鼠标与它们交互。为此,我创建了一个名为 Node 的自定义 QGraphic
有什么方法可以编辑 QPainterPath 中单个“lineTo”元素的位置(或删除特定元素并用修改后的版本替换它们?)。我尝试使用 *.setElementPositionAt(i,x,y) 无济
我想画一个有角度的矩形。它可以工作,但是当我改变角度时,矩形的位置会在其他地方发生变化。我听不懂。有人帮我吗? 这是我的代码: QPoint point = QPoint(100,100); // h
我是一名优秀的程序员,十分优秀!