How does one subclass QValue3DAxisFormatter with PySide for use in Q3DScatter?

Question:

When I subclass QValue3DAxisFormatter in my application, the application simply hangs and then exits without an exception or explanation. I have tried removing every and all methods in my subclass (even though they are supposed to be implemented, according to the docs) just to test what the problem is, but it always simply exits without explanation. I have looked everywhere for examples of how to achieve this custom formatter in Python, but the only example I can find in any language is this example: https://code.qt.io/cgit/qt/qtdatavis3d.git/tree/examples/datavisualization/qmlaxisformatter?h=5.15, which is the code from the explanation found at https://doc.qt.io/qt-5/qtdatavisualization-qmlaxisformatter-example.html#custom-axis-formatter
. I don’t really understand how to translate that to Python code (I’m also not looking to make a calendar-based axis, nor am I using QML), though I’ve tried the basic setup as follows:

class AxisFormatter(QtDataVisualization.QtDataVisualization.QValue3DAxisFormatter):
    def __init__(self):
        super().__init__()
        print("init")
    def createNewInstance(self):
        print("creating new instance")
        return AxisFormatter()
    def populateCopy(self, copy: QtDataVisualization.QtDataVisualization.QValue3DAxisFormatter):
        print("populating copy")
        super().populateCopy(copy)
    def recalculate(self) -> None:
        print("recalculating")
    def stringForValue(self, value: float, format: str) -> str:
        print('stringForValue')
        return str(value)

(The only print statement that will print here is the "init" one, then after ~10 seconds of hanging, the application exits.) Ideally I would like to simply map the axis’ (integer) value to an array that I provide as an argument to AxisFormatter, but I can’t even get this simple boilerplate working.

Asked By: elunomas

||

Answers:

Apparently there is some bug that requires a persistent reference to the formatter, so the problem is not in the code above, but rather in the assignment of the formatter to the axis. I did have:

my_chart.axisX().setFormatter(AxisFormatter())

But it must be created as follows:

self.my_new_formatter = AxisFormatter()
my_chart.axisX().setFormatter(self.my_new_formatter)

Thanks to Friedemann Kleint, a Qt JIRA admin, for this answer.
(I believe this issue may be addressed in a future Qt release.)

Answered By: elunomas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.