最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

python 3.x - How to add labels to 3D Axis in pyqtgraph - Stack Overflow

matteradmin8PV0评论

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()
Post a comment

comment list (0)

  1. No comments so far