How to place a QPainter element in QML Rectangle? - qt

There is a diagram which I have drawn with QPainter.
How do I diaplay it in QML's Rectangle?
That diagram will get updated on the runtime.

The question is a bit vague.
Do you have the diagram in a QImage or a QPixmap? Then you can use image providers to expose the diagram to QML, and draw it into a Image element.
Do you have the code that uses a QPainter to draw it, and want to make a QML element that uses such code? In that case, subclass QDeclarativeItem (in Qt Quick 1) or QQuickPaintedItem (Qt Quick 2), override their paint() methods and do the drawing there. You can then register the new type in the QML engine, so you can use a Diagram type in your QML files.

Related

QML equivalent of QPainterPathStroker

I am trying to create an outline of a path in QML. I read that QPainterPathStroker is able to do this, but it is not in QML. What is the QML equivalent of QPainterPathStroker?
Canvas QML type is the closest one. You can see Canvas examples here.

How can I add multiple QPixMap item to a widget?

I have video analysis c++ code and porting to the QT Creator for gui requirements.
We are detecting certain shapes and items from the video stream and adjust their size ie 100x100 and want to display on the screen. We can display through Label but only one QpixMap item.
What is the best way to show detected images automatically on a widget such as last 20 one that autosized?
Is there any a small code sample to start with it to show us the direction?
Thanks.
You could subclass QWidget and reimplement paintEvent to paint whatever you need using QPainter.
See Basic Drawing Example for details:
http://doc.qt.io/qt-5/qtwidgets-painting-basicdrawing-example.html

QGraphicsItemGroup to OpenGL texture

I'm working on an application that is using map tiles in a QGraphicsItemGroup to render a map and I want to make the switch to OpenGL so I can add 3D shapes on the map.
In order to do so as smoothly as possible I would need to get a texture from this QGraphicsItemGroup.
In case anyone has this issue I managed to get something I could work with. Rather than using the QGraphicsItemGroup I took the whole scene and made an image from it that I use as a texture.
Code extract : (map is my QGraphicsScene)
QImage img(ui->map->width(), ui->map->height(),QImage::Format_ARGB32_Premultiplied);
QPainter p(&img);
ui->map->fitInView(0,0,scene.width(),scene.height(),Qt::IgnoreAspectRatio);
ui->map->viewport()->render(&p);
p.end();
img.save("tmp.png");

How can I paint on a specific pixel in BlackBerry 10, Qt QML Cpp

I simply want to paint a 1 pixel dot on a specific point on the screen. Is there anyway to do that? In QML or cpp?
Rectangle QML item should works good for you: http://qt-project.org/doc/qt-4.8/qml-rectangle.html
If you need to draw something more specific and more flexible, then you can customize QDeclarativeItem in C++, unset QGraphicsItem::ItemHasNoContents flag in your custom class and implement paint method. More details: http://qt-project.org/doc/qt-4.8/qdeclarativeitem.html

Scalable painting of a Qt application

I'm writing a simulation of an embedded device's screen (which contains custom widgets on top of a main QWidget), and while the native size of the screen is 800x600, I want to be able to scale it up and down by dragging the window's corner. Without diddling with grid layouts and stretchers (which won't scale the fonts up/down), how do I accomplish this sort-of zoom? I think part of the solution might be to create a QTransform and somehow inject that into the QWidget for the entire application, or its QPaintDevice or QPaintEngine. I'd like to do this without putting QTransform in each custom widget, just the "main window" QWidget.
This is possible if you are using QGraphicsView as your main display widget. QGraphicsScene now supports widgets as content, so you can literally just scale them.
I believe the alternative is to reimplement the paint() for each widget, and manually set the transform/scale before the painting of child widgets.
Bit of a guess here as I've not tried it... but you could try putting the top-level widget into a QGraphicsView then get the QGraphicsView to do the scaling.
You could then enable OpenGL on the QGraphicsView and have it scaled in hardware so it's nice and fast.

Resources