[SOLVED] How to define your own subclass of QChart(C++) as a QML ItemType
-
I looked over many documentations, and still can't find a solution.
So the question is: How is it possible to make an own class which is derived from QChartView or QChart and use it in QML?
The question is not only about QChart or QChartView, it is about all the Qt classes (like QTableView, QListView etc.)I think, it would be nice to see a good example on how to use derived Qt C++ classes as Items, it would even be a big help for all of us.
The reason why we need it is, because we could use our C++ classes and also use the conveniences of QML if it would be possible.I tried quite many different solution and read about it a lot, but my QChartView doesn't work with QML...
My closest try is what I linked, I hope people here, have a solution for this problem."Link for the sourcecode. I made it short.":http://speedy.sh/JnEqS/teszt.zip
-
Hi, and welcome to the Qt Dev Net!
If you want to define your own QML Item, then you need to subclass "QQuickItem":http://doc.qt.io/qt-5/qquickitem.html.
Here are some tutorials:
-
Thank you for the fast response, and for welcoming me to the Qt Dev Net!
I looked through the examples and the documentations, but the examples are using primitive drawing commands to draw, and not already implemented QClasses like QChartView or QListView or even QTreeView.My quess is that the QChartView and all the other QClasses are using the QPainter to draw, which is used to draw on a QWidget, but the QWidget is not presented/reachable in the QML system.
So we need a good workaround which provides both the QPainter and the QWidget.So to solve the problem probably we need the QQuickPaintedItem what can provide a QPainter for drawing, however it still can't provide the QWidget on which to draw. (I amnot sure if we are closer to a good solution but...)
With the QPainter we can draw primitiveswhy can'T we paint more complex classes like QChartView or any QClasses.
The QChart's paint(...) function needs a QPainter and also a QStyleOptionGraphicsItem and a QWidget to draw on.
So here the question is can we reach a QWidget* for the QQuickPaintedItem's painting area, or maybe better to ask why do we need the QWidget if drawing primitives are working only with a QPainter.If drawing QClasses on a QQuickPaintedItem were possible, then we could even import the QTreeView (C++ GUI element) to a QML Item type without a problem.
But I also heard TreeView does not exist in QML, so probably somehow it is not this easy.
After all these thought I don't know if the solution I am trying to do is possible in Qt.tl,dr; Short question is that can we make the QChartView to draw on/in/by QQuickItem in QML?
-
Hi,
Just to be sure I'm understanding your correctly: are you looking for the ChartView QML element ? -
I need a ChartView for which I can reimplement the void event(QEvent*) function in C++ (or every other event).
So in short I just want to expand ChartView's functionalities with functions written in C++. And probably just looking for a clear solution, I feel there is a chance that this is reachable.
And to be honest, I just don't want to use javascript, if it is possible to be solved in C++.
-
Then you should take a look at DeclarativeChart and friends
-
So in this case the solution is to go back to QtQuick 1.0, and use the Declarative classes, because it provides QWidget.
I think if this is the only way then I mark this thread as solved.
But I still want to know what do I leave out if I go back to QtQuick 1.0, and why doesn't QtQuick 2.3 support such features? Wanna know some pro/cons for the two solutions.
If I should guess why can't 2.3 support what 1.0 supported is because QWidget doesn't really use OpenGL and GPU features, but this is only a guess.
-
I'm glad to hear you've found a solution.
[quote author="hghgi1" date="1424089426"]But I still want to know what do I leave out if I go back to QtQuick 1.0, and why doesn't QtQuick 2.3 support such features? Wanna know some pro/cons for the two solutions.
If I should guess why can't 2.3 support what 1.0 supported is because QWidget doesn't really use OpenGL and GPU features, but this is only a guess.[/quote]Your guess is correct.
Qt Quick 1 is simply a wrapper around the "Graphics View Framework":http://doc.qt.io/qt-5/graphicsview.html, which is based on QWidgets and renders graphics using the CPU.
Qt Quick 2 is a complete rewrite using new technology, which is based on OpenGL and renders graphics using the GPU. Its internals do not share anything in common with QWidgets. It has much better performance than Qt Quick 1 due to hardware-accelerated graphics, as well as a custom JavaScript engine that is optimized for transferring data between C++ and QML. (You might not need to worry about the graphics performance though)