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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

You 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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

You 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