问题描述
我一直在尝试寻找一种在 PyQtGraph 中渲染文本的方法,同时将 OpenGL 用于 3D 绘图。已提出的一种可行解决方案允许呈现简单的文本,但它将所有内容呈现在一行上,即使您确实包含“\r\n”转义字符(使用 GLViewWidget 类的 renderText() 成员)。
我遇到的另一个解决方案建议使用 QPainter 对象,如下所示。这似乎跨多行呈现文本,但它不会在实际的 GLViewWidget 上覆盖文本,而是似乎正在清除整个窗口。下面的代码应该在每次调用paintGL() 时打印计数器的值。如果您逐步执行代码,您可以看到 QPainter 对象将整个背景设置为白色。
有人成功地使用 QPainter 类绘制文本吗?
如果有人对如何跨多行呈现文本有任何其他想法,我将不胜感激。
from PyQt5 import Qt,QtGui
from PyQt5.QtGui import QColor
from pyqtgraph.opengl import GLViewWidget
import pyqtgraph.opengl as gl
from PyQt5.QtGui import QColor
from pymap3d import *
from pyqtgraph.Qt import QtCore,QtGui
from pyqtgraph.opengl.GLGraphicsItem import GLGraphicsItem
class GLView(GLViewWidget):
"""
I have implemented my own GLViewWidget
"""
def __init__(self,parent=None):
super().__init__(parent)
self.cntr = 0
def paintGL(self,*args,**kwds):
GLViewWidget.paintGL(self,**kwds)
self.qglColor(QColor(255,255))
self.renderText(0,'This is a test')
font = QtGui.QFont()
font.setFamily("Tahoma")
font.setPixelSize(11)
font.setBold(True)
picture = QtGui.QPicture()
qp = QtGui.QPainter()
qp.begin(self)
qp.setFont(font)
rectangle = QtCore.QRect(0,200,50)
qp.drawText(rectangle,f"Cntr: {self.cntr}\r\nNew line")
self.cntr += 1
qp.end()
if __name__ == '__main__':
# Create app
app = QtGui.QApplication([])
w = GLView()
w.resize(800,800)
w.show()
w.setwindowTitle('Earth 3D')
w.setCameraPosition(distance=20)
g = gl.GLGridItem()
w.addItem(g)
while w.isVisible():
app.processEvents()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)