我正在尝试用一些文本构建一个弧。我能够创建弧,我可以将文本与曲线一起放置。但到目前为止,我找不到一种垂直于曲线旋转文本的方法。
以下是我正在尝试的代码
from __future__ import division
import os
import sys
from PyQt4 import QtGui,QtCore
import math
class PathPaintTest(QtGui.QFrame):
def __init__(self, *args):
super (PathPaintTest, self).__init__(*args)
self.setMaximumSize(250, 110)
self.setMinimumSize(250, 110)
self.setFrameShape(QtGui.QFrame.WinPanel)
self.setFrameShadow(QtGui.QFrame.Sunken)
def paintEvent(self, event):
hw = QtCore.QString("Hello World")
drawWidth = self.width() / 100
painter = QtGui.QPainter(self)
pen = painter.pen()
pen.setWidth(drawWidth)
pen.setColor(QtGui.QColor(QtCore.Qt.red))
painter.setPen(pen)
painter.translate(5,0)
cc1 = QtCore.QPointF(5, -15)
cc2 = QtCore.QPointF(220, -15)
path1 = QtGui.QPainterPath(QtCore.QPointF(5, 140))
path1.cubicTo(cc1, cc2, QtCore.QPointF(240, 140))
painter.drawPath(path1)
pen.setColor(QtGui.QColor(QtCore.Qt.yellow))
painter.setPen(pen)
font = painter.font()
font.setPixelSize(drawWidth * 5)
painter.setFont(font)
percentIncrease = 1 / (hw.size() + 1)
perecent = 0
for i in range(hw.size()):
perecent+=percentIncrease
point = QtCore.QPointF(path1.pointAtPercent(perecent))
painter.drawText(point,QtCore.QString(hw[i]))
QtGui.QFrame.paintEvent(self,event)
class TextTest(QtGui.QWidget):
def __init__(self):
super(TextTest, self).__init__()
self.initUI()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.close()
def initUI(self):
self.mypb = PathPaintTest()
hbox = QtGui.QHBoxLayout()
hbox.addWidget(self.mypb)
vbox = QtGui.QVBoxLayout()
vbox.addLayout(hbox)
self.setLayout(vbox)
self.setGeometry(1900, 500, 450, 180)
self.setWindowTitle('Text Test')
def run():
app = QtGui.QApplication(sys.argv)
ex = TextTest()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
run()
但我正在努力实现接近这篇文章http://zrusin.blogspot.com/2006/11/text-on-path.html的内容。喜欢文本想要根据角度旋转。知道如何使用QPainterPath和QPainter或任何其他方法吗?
我正在寻找像这样的输出
很抱歉因为声誉较低而放置图片链接我无法将图片添加到我的帖子中。
编辑:
这是一些更新版本..其python版本的qt-project.org/faq/answer/how_do_i_make_text_follow_the_line_curve_and_angle_of_the_qpainterpath
from __future__ import division
import os
import sys
from PyQt4 import QtGui,QtCore
import math
class PathPaintTest(QtGui.QFrame):
def __init__(self, *args):
super (PathPaintTest, self).__init__(*args)
self.setMaximumSize(250, 110)
self.setMinimumSize(250, 110)
self.setFrameShape(QtGui.QFrame.WinPanel)
self.setFrameShadow(QtGui.QFrame.Sunken)
def paintEvent(self, event):
hw = QtCore.QString("Hello World")
drawWidth = self.width() / 100
painter = QtGui.QPainter(self)
pen = painter.pen()
pen.setWidth(drawWidth)
pen.setColor(QtGui.QColor(QtCore.Qt.red))
painter.setPen(pen)
painter.translate(5,0)
c1 = QtCore.QPointF(5, -15)
c2 = QtCore.QPointF(220, -15)
path = QtGui.QPainterPath(QtCore.QPointF(5, 140))
path.cubicTo(c1, c2, QtCore.QPointF(240, 140))
painter.drawPath(path)
pen.setColor(QtGui.QColor(QtCore.Qt.green))
painter.setPen(pen)
font = painter.font()
font.setPixelSize(drawWidth * 10)
painter.setFont(font)
perecentIncrease = 1 / (hw.size() + 1)
perecent = 0
for i in range(hw.size()):
perecent+=perecentIncrease
point = QtCore.QPointF(path.pointAtPercent(perecent))
angle = path.angleAtPercent(perecent)
rad = math.radians(angle)
sina = math.sin(rad)
cosa = math.cos(rad)
deltaPenX = cosa * pen.width()
deltaPenY = sina * pen.width()
newX = (cosa * point.x()) - (sina * point.y())
newY = (cosa * point.y()) + (sina * point.x())
deltaX = newX - point.x()
deltaY = newY - point.y()
tran = QtGui.QTransform(cosa,sina,-sina,cosa,-deltaX + deltaPenX,-deltaY - deltaPenY)
painter.setWorldTransform(tran)
painter.drawText(point,QtCore.QString(hw[i]))
QtGui.QFrame.paintEvent(self,event)
class TextTest(QtGui.QWidget):
def __init__(self):
super(TextTest, self).__init__()
self.initUI()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.close()
def initUI(self):
self.mypb = PathPaintTest()
hbox = QtGui.QHBoxLayout()
hbox.addWidget(self.mypb)
vbox = QtGui.QVBoxLayout()
vbox.addLayout(hbox)
self.setLayout(vbox)
self.setGeometry(300, 200, 500, 250)
self.setWindowTitle('Text Test')
def run():
app = QtGui.QApplication(sys.argv)
ex = TextTest()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
run()
但我仍然无法获得轮换。
更新:
我现在正在使用它是更新的部分,也许它也会帮助其他人。
painter.save()
painter.translate(point)
painter.rotate(-angle)
painter.drawText(QtCore.QPoint(0, -pen.width()),QtCore.QString(hw[i]))
painter.restore();
答案 0 :(得分:1)
painter.save()
painter.translate(point)
painter.rotate(-angle)
painter.drawText(QtCore.QPoint(0, -pen.width()),QtCore.QString(hw[i]))
painter.restore();
由于评论而回答我自己的问题并对迟到的回复感到抱歉:)