【发布时间】:2017-05-29 15:44:52
【问题描述】:
我得到了这个代码:
from PyQt4 import QtGui, QtCore
class MyFrame(QtGui.QGraphicsView):
def __init__( self, parent = None ):
super(MyFrame, self).__init__(parent)
scene = QtGui.QGraphicsScene()
self.setScene(scene)
self.resize( 400, 240 )
# http://pyqt.sourceforge.net/Docs/PyQt4/qpen.html
pencil = QtGui.QPen( QtCore.Qt.black, 2)
pencil.setStyle( QtCore.Qt.SolidLine )
# pencil.setStyle( QtCore.Qt.UpArrow )
scene.addLine( QtCore.QLineF( 0, 0, 100, 100 ), pencil )
if ( __name__ == '__main__' ):
app = QtGui.QApplication([])
f = MyFrame()
f.show()
app.exec_()
哪个绘制了这个窗口:
当我用图像编辑器在最后一张图像上绘制这些图像时,如何在行的一端添加一个箭头:
我用这个伪代码找到了这个 C++ http://www.codeproject.com/Articles/3274/Drawing-Arrows 的教程:
// ARROWSTRUCT
//
// Defines the attributes of an arrow.
typedef struct tARROWSTRUCT {
int nWidth; // width (in pixels) of the full base of the arrowhead
float fTheta; // angle (in radians) at the arrow tip between the two
// sides of the arrowhead
bool bFill; // flag indicating whether or not the arrowhead should be
// filled
} ARROWSTRUCT;
// ArrowTo()
//
// Draws an arrow, using the current pen and brush, from the current position
// to the passed point using the attributes defined in the ARROWSTRUCT.
void ArrowTo(HDC hDC, int x, int y, ARROWSTRUCT *pArrow);
void ArrowTo(HDC hDC, const POINT *lpTo, ARROWSTRUCT *pArrow);
只需用所需属性填充 ARROWSTRUCT,确保当前 DC 位置正确(MoveTo() 等),然后调用两个 ArrowTo() 函数之一。尺寸参数(nWidth 和 fTheta)定义如下:
技术
这可以追溯到高中代数和三角学。 ArrowTo() 函数首先构建整行的向量。然后它根据您传递的 nWidth 和 fTheta 属性计算箭头两侧的点。棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒棒哒!
这是一些伪伪代码:
lineVector = toPoint - fromPoint
lineLength = length of lineVector
// calculate point at base of arrowhead
tPointOnLine = nWidth / (2 * (tanf(fTheta) / 2) * lineLength);
pointOnLine = toPoint + -tPointOnLine * lineVector
// calculate left and right points of arrowhead
normalVector = (-lineVector.y, lineVector.x)
tNormal = nWidth / (2 * lineLength)
leftPoint = pointOnLine + tNormal * normalVector
rightPoint = pointOnLine + -tNormal * normalVector
此外,我还可以找到另一个问题Drawing a polygon in PyQt,但它是针对 qt5 的。因此在pyqt4中用多边形绘制箭头是否更好?
【问题讨论】:
-
如果您已经有 2 个解决方案,您的问题是什么?