Howto render QtQuick item into texture - qt

I have a custom QQuickFramebufferObject node which renders a texture on some geometry. Now I want to render a Qml item to a texture (e.g. QOpenGLTexture) and use this texture in my custom node. I know that there is the grabToImage (http://doc.qt.io/qt-5/qquickitem.html#grabToImage) method but this method is slow because it first renders everything into a QImage and data has to be transferred from/to the GPU.
In detail, I am looking for something like ShaderEffectSource element but with direct access to the texture id which can be used as a texture in the QQuickFramebufferObject. Is there already something implemented in Qt for this scenario?

Related

How to expose a C++ visual element to QML

"I want to draw my visual item object that created in c++ in qml."
current state.
I wrote a subclass of QQuickFramebufferObject in C++.
and it renders something using its renderer. so I think that is visual item.
I register the subclass through qmlRegisterType() for using it in QML.
I instanciated the class object in QML and set area to draw something.
After the engine loads qml, I find the object which made in QML and put it into a pointer in C++.
so that I can add data with the pointer and that updates in QML.
It is working well. rendering data on proper area.
problem I got.
I am using StackView element for navigation, and when I move to other view and comeback.
It seems QML instances in prev view are re-created, so my connections with C++ be invalidated.
solution I thought
I am not sure, that is right way or not. Anyway I tried to instanciate the object in C++ not in QML.
and register it to QML through setContextProperty().
and set parent, render area for that property, but it is not working the object do not render anything.
code outline is below
// in C++
mCustomWidget = new CustomWidget;
engine->rootContext()->setContextProperty("custom_widget", mCustomWidget);
// in QML
Item { // CustomContainer
id: container
Component.onCompleted: custom_widget.parent = container
onXChanged: custom_widget.x = x
onYChanged: custom_widget.y = y
onWidthChanged: custom_widget.width = width
onHeightChanged: custom_widget.height = height
}
I expected C++ side has the object so it would be not deleted whether move to other view or not.
With the pointer in QML side which is context property name, I can see the rendered results if I set its parent and render area.
but, It is something wrong so not working.
Please, let me know the wrong points or other solution. I would be appreciated it. Thank you.

How to handle Stylus/Pen input in QML

I am creating a simple drawing application and would like my app handle pen (stylus like Apple Pencil) input. The app is written in Qml/Qt Quick. I know that when using QWidgets you can handle tablet events using QTabletEvent (example), but how I can handle pen input in QML (using MouseArea does not work and I had no luck with PointHandler either). Does anyone know if it is possible to handle Pen input in Qt Quick?
Try using QML TapHandler: https://doc.qt.io/qt-5/qml-qtquick-taphandler.html
acceptedDevices: PointerDevice.Mouse | PointerDevice.Stylus
Looking at the example linked by #Albertino80 , I subclassed QQuickWindow which inherits QWindow and hence is able to receive tablet events (void tabletEvent(QTabletEvent* event). These events can then be used to emit appropriate signals that can be propapated to other QObjects. This method works ok, but it has the problem that event coordinates are not local to the item where they are consumed, instead one has to manually recalculate the position of each event by mapping the coordinates to the custom window.

Is there a way to access the image on the QWidget's backing store?

I'm doing some compositing inside the paintEvent() in a custom widget. Some of the compositing is done when some areas are already painted, and I need access to the current contents painted so far.
So, I'm looking for a way to access the image contents of the current backing store during a paintEvent. I've looked at QBackingStore, but there's nothing there that directly gives me access to the backing store bitmap. Is there some API, perhaps private, that could be used to provide that?
If not, I'll have to resort to painting on an explicit pixmap and rendering that pixmap onto the widget.
It is possible, but it is not portable. The QBackingStore is just a wrapper class around a QImage buffer on most platforms, but I suppose this is not guaranteed. I've researched this issue when writing the QuickWidget. A cast is needed:
QImage * image = dynamic_cast<QImage*>(backingStore()->paintDevice());
if (image != 0) // it's an image, do something with it
Be careful though not to cause the QImage to detach. Things such as resizing are off limits.
Check the QuickWidget out at:
https://code.google.com/p/quickwidget/

OpenGL rendering to a QML item

I have a QML file which contains a layout of QML items and now I want one of those items to be a QGLWidget. i.e. I want to render to a specific QML item.
Is anyone aware of how to do this?
The simplest way I suppose it to provide QML a new custom component implemented in C++. I couldn't find anything ready.
You could subclass the QDeclarativeItem and implement your OpenGL code in the paint function after using the QPainter::beginNative() function. After that you can "export" your new custom item to QML this way. This is quite simple and should work, but you'll have to setup the viewport of you QDeclarativeView to be a QGLWidget, something like this:
QDeclarativeView view;
// This is needed because OpenGL viewport doesn't support partial updates.
view.setViewportUpdateMode(QGraphicsView::FullViewportUpdateMode);
view.setViewport(new QGLWidget);
or you'll have to use the opengl graphics system for the entire application.
Another way is using QML/3D.
This thread will give you some other information.

Drawing QPixmap Rects Over QWidget with QPainter

I'll get serialized QPixmap Rects over the socket. I'll unserialize them and draw the rects in a QWidget Now my question is which QWidget should I use for this purpose ? is there any recontamination about how should I proceed ? at the moment I've decided to use a QPainter and draw using drawPixmap am I going in a wrong direction ?
It sounds like you might want use QGraphicsPixmapItem instances drawn on a QGraphicsScene with a QGraphicsView.
See Qt Graphics View Framework:
Graphics View provides a surface for
managing and interacting with a large
number of custom-made 2D graphical
items, and a view widget for
visualizing the items, with support
for zooming and rotation.
The framework includes an event
propagation architecture that allows
precise double-precision interaction
capabilities for the items on the
scene. Items can handle key events,
mouse press, move, release and double
click events, and they can also track
mouse movement.
Graphics View uses a BSP (Binary Space
Partitioning) tree to provide very
fast item discovery, and as a result
of this, it can visualize large scenes
in real-time, even with millions of
items.

Resources