admin管理员组文章数量:1023838
I would like to add labels to a 3D axis drawn in GLViewWidget from pyqtgraph.opengl. I followed the example but did not manage to make it work properly. When I subclass the GLViewWidget object and overwrite the paintGL() method, it says that there are not any self.qglColor and self.renderText attributes inherited from the parent ('MyGLView' object has no attribute 'qglColor'; 'MyGLView' object has no attribute 'renderText').
Here is the code:
from pyqtgraph.opengl import GLViewWidget
from PyQt6.QtGui import QFont, QColor, QFontMetrics
from PyQt6.QtWidgets import QApplication
class MyGLView(GLViewWidget):
def __init__(self, parent=None):
super().__init__(parent)
def paintGL(self, *args, **kwargs):
GLViewWidget.paintGL(self, *args, **kwargs)
font = QFont()
font.setFamily("Tahoma")
font.setPixelSize(21)
font.setBold(True)
title_str = 'Screen Coordinates'
metrics = QFontMetrics(font)
m = metrics.boundingRect(title_str)
width = m.width()
height = m.height()
scrn_sz_width = self.size().width()
self.qglColor(QColor(255, 255, 0, 255))
self.renderText((scrn_sz_width-width)/2, height+5, title_str, font)
app = QApplication([])
fig1 = MyGLView()
fig1.show()
Any suggestion whether I am just using wrong version (0.13.3) of pyqtgraph? If that is the case can I just make it work without downgrading/upgrading?
I would like to add labels to a 3D axis drawn in GLViewWidget from pyqtgraph.opengl. I followed the example but did not manage to make it work properly. When I subclass the GLViewWidget object and overwrite the paintGL() method, it says that there are not any self.qglColor and self.renderText attributes inherited from the parent ('MyGLView' object has no attribute 'qglColor'; 'MyGLView' object has no attribute 'renderText').
Here is the code:
from pyqtgraph.opengl import GLViewWidget
from PyQt6.QtGui import QFont, QColor, QFontMetrics
from PyQt6.QtWidgets import QApplication
class MyGLView(GLViewWidget):
def __init__(self, parent=None):
super().__init__(parent)
def paintGL(self, *args, **kwargs):
GLViewWidget.paintGL(self, *args, **kwargs)
font = QFont()
font.setFamily("Tahoma")
font.setPixelSize(21)
font.setBold(True)
title_str = 'Screen Coordinates'
metrics = QFontMetrics(font)
m = metrics.boundingRect(title_str)
width = m.width()
height = m.height()
scrn_sz_width = self.size().width()
self.qglColor(QColor(255, 255, 0, 255))
self.renderText((scrn_sz_width-width)/2, height+5, title_str, font)
app = QApplication([])
fig1 = MyGLView()
fig1.show()
Any suggestion whether I am just using wrong version (0.13.3) of pyqtgraph? If that is the case can I just make it work without downgrading/upgrading?
Share Improve this question edited Nov 18, 2024 at 17:52 Rabbid76 212k30 gold badges160 silver badges200 bronze badges asked Nov 18, 2024 at 17:52 New2codingNew2coding 71713 silver badges26 bronze badges1 Answer
Reset to default 1You have to use QPainter to paint in paintGL
method.
Here is an example you can use:
from pyqtgraph.opengl import GLViewWidget
from PyQt6.QtGui import QFont, QColor, QFontMetrics, QPainter, QPen
from PyQt6.QtWidgets import QApplication
class MyGLView(GLViewWidget):
def __init__(self, parent=None):
super().__init__(parent)
def paintGL(self, *args, **kwargs):
GLViewWidget.paintGL(self, *args, **kwargs)
painter = QPainter(self)
font = QFont()
font.setFamily("Tahoma")
font.setPixelSize(21)
font.setBold(True)
painter.setPen(QPen(QColor(255, 255, 0, 255)))
painter.setFont(font)
title_str = 'Screen Coordinates'
metrics = QFontMetrics(font)
m = metrics.boundingRect(title_str)
width = m.width()
height = m.height()
scrn_sz_width = self.size().width()
painter.drawText(int((scrn_sz_width - width) / 2), height + 5, width, height, 0, title_str)
painter.end()
app = QApplication([])
fig1 = MyGLView()
fig1.show()
app.exec()
I would like to add labels to a 3D axis drawn in GLViewWidget from pyqtgraph.opengl. I followed the example but did not manage to make it work properly. When I subclass the GLViewWidget object and overwrite the paintGL() method, it says that there are not any self.qglColor and self.renderText attributes inherited from the parent ('MyGLView' object has no attribute 'qglColor'; 'MyGLView' object has no attribute 'renderText').
Here is the code:
from pyqtgraph.opengl import GLViewWidget
from PyQt6.QtGui import QFont, QColor, QFontMetrics
from PyQt6.QtWidgets import QApplication
class MyGLView(GLViewWidget):
def __init__(self, parent=None):
super().__init__(parent)
def paintGL(self, *args, **kwargs):
GLViewWidget.paintGL(self, *args, **kwargs)
font = QFont()
font.setFamily("Tahoma")
font.setPixelSize(21)
font.setBold(True)
title_str = 'Screen Coordinates'
metrics = QFontMetrics(font)
m = metrics.boundingRect(title_str)
width = m.width()
height = m.height()
scrn_sz_width = self.size().width()
self.qglColor(QColor(255, 255, 0, 255))
self.renderText((scrn_sz_width-width)/2, height+5, title_str, font)
app = QApplication([])
fig1 = MyGLView()
fig1.show()
Any suggestion whether I am just using wrong version (0.13.3) of pyqtgraph? If that is the case can I just make it work without downgrading/upgrading?
I would like to add labels to a 3D axis drawn in GLViewWidget from pyqtgraph.opengl. I followed the example but did not manage to make it work properly. When I subclass the GLViewWidget object and overwrite the paintGL() method, it says that there are not any self.qglColor and self.renderText attributes inherited from the parent ('MyGLView' object has no attribute 'qglColor'; 'MyGLView' object has no attribute 'renderText').
Here is the code:
from pyqtgraph.opengl import GLViewWidget
from PyQt6.QtGui import QFont, QColor, QFontMetrics
from PyQt6.QtWidgets import QApplication
class MyGLView(GLViewWidget):
def __init__(self, parent=None):
super().__init__(parent)
def paintGL(self, *args, **kwargs):
GLViewWidget.paintGL(self, *args, **kwargs)
font = QFont()
font.setFamily("Tahoma")
font.setPixelSize(21)
font.setBold(True)
title_str = 'Screen Coordinates'
metrics = QFontMetrics(font)
m = metrics.boundingRect(title_str)
width = m.width()
height = m.height()
scrn_sz_width = self.size().width()
self.qglColor(QColor(255, 255, 0, 255))
self.renderText((scrn_sz_width-width)/2, height+5, title_str, font)
app = QApplication([])
fig1 = MyGLView()
fig1.show()
Any suggestion whether I am just using wrong version (0.13.3) of pyqtgraph? If that is the case can I just make it work without downgrading/upgrading?
Share Improve this question edited Nov 18, 2024 at 17:52 Rabbid76 212k30 gold badges160 silver badges200 bronze badges asked Nov 18, 2024 at 17:52 New2codingNew2coding 71713 silver badges26 bronze badges1 Answer
Reset to default 1You have to use QPainter to paint in paintGL
method.
Here is an example you can use:
from pyqtgraph.opengl import GLViewWidget
from PyQt6.QtGui import QFont, QColor, QFontMetrics, QPainter, QPen
from PyQt6.QtWidgets import QApplication
class MyGLView(GLViewWidget):
def __init__(self, parent=None):
super().__init__(parent)
def paintGL(self, *args, **kwargs):
GLViewWidget.paintGL(self, *args, **kwargs)
painter = QPainter(self)
font = QFont()
font.setFamily("Tahoma")
font.setPixelSize(21)
font.setBold(True)
painter.setPen(QPen(QColor(255, 255, 0, 255)))
painter.setFont(font)
title_str = 'Screen Coordinates'
metrics = QFontMetrics(font)
m = metrics.boundingRect(title_str)
width = m.width()
height = m.height()
scrn_sz_width = self.size().width()
painter.drawText(int((scrn_sz_width - width) / 2), height + 5, width, height, 0, title_str)
painter.end()
app = QApplication([])
fig1 = MyGLView()
fig1.show()
app.exec()
本文标签: python 3xHow to add labels to 3D Axis in pyqtgraphStack Overflow
版权声明:本文标题:python 3.x - How to add labels to 3D Axis in pyqtgraph - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745603104a2158572.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论