3

I've been searching the internet how to draw or render text on QQuickItem but to no avail. I opt not to use QQuickPaintedItem which uses QPainter paint() function. Aside from that there is also a known issue of QQuickPaintedItem on iOS retina display devices where the display is blurred and edges were not sharp.

Please advise any possible work-around on this.

matteoL
  • 41
  • 4

2 Answers2

2

Since QtDeclarative has been deprecated already, I opt not to use QQuickPaintedItem which uses QPainter paint() function

That statement doesn't make a lot of sense. QtDeclarative is QtQuick1, QQuickPaintedItem is part of the QtQuick2 module and has nothing to do with QtDeclarative. Furthermore, even though it uses QPainter it is still hardware accelerated using OpenGL.

Overloading a custom QQuickItem to draw text in it manually, without assistance from QPainter or any other similar class will be a very complex task.

A QQuickItem is basically the class behind QML's Item element. QML also has a Text element. QML has been designed for rapid UI development, it is entirely pointless to draw the text manually. You don't need any C++ for this, only QML:

Item {
    Text {
        text: "something"
    }
}

Take a look at the Text element and its properties, you can specify font, color and whatnot. You can directly use the element as a source for graphics effects too.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • 1
    Sorry, for the confusion on the above statement. I know already about Text QML Type. However, I wanted to render text into each custom geometry nodes which formed a single QSGNode. Is it possible to render a text after creating the geometry node? The same with QPainter behavior where you can draw a rectangle then draw text on it after. TIA. – matteoL Apr 14 '15 at 15:55
  • You cannot render text into a geometry node. Whatever is it that you are trying to achieve, I think you are going the wrong way about it. You can build geometry for text manually, and use it in a geometry node, but I'd say that's a very bad idea. – dtech Apr 14 '15 at 16:03
  • Thanks @ddriver. Can you give advise and tips what should I do to draw this [sample](http://oi60.tinypic.com/2a61oxx.jpg) with text inside each polygon. – matteoL Apr 15 '15 at 07:48
  • @user1870074 - you could `QQuickItem` to implement the actual "arrowy thingie", it should be easy enough to do and just put a Text element inside of it. This way it will be more useful and easier to work with. QML is modular, one item renders one thing, there is no QML element which renders more than one feature at a time. Unless you use `QQuickPaintedItem` in which case you can use `QPainter` to draw multiple features. – dtech Apr 15 '15 at 11:54
  • What if you tried first using Text (in QML) and JS and saw that it is just too slow? – Nils Nov 23 '18 at 08:50
1

You can use QPainter to draw on a QImage like a canvas. Then you can turn this into a QSGTexture that can be assigned to a QSGSimpleTextureNode.

Here is an excerpt of code I recently wrote to do this:

QColor color("steelblue");
QRect rect(0,0,aw, ah);
for(auto ch: m_charList){
    QImage canvas(rect.width(), rect.height(), QImage::Format_RGBA8888);
    canvas.fill(QColor("transparent"));
    QPainter painter(&canvas);

    QFont font = painter.font();
    //font.setPixelSize(48);
    font.setPixelSize(rect.width());
    font.setBold(true);
    painter.setFont(font);
    painter.setPen(color);

    QRect bounding = QRect(0, 0, rect.width(), rect.height());
    painter.drawText(0, 0, rect.width(), rect.height(), Qt::AlignCenter, ch, &bounding);

    QSGTexture *texture = this->window()->createTextureFromImage(canvas);
    m_textureList[ch] = texture;
}

The repo with the full working code in context is here.

There are MANY reasons to want to render like this. You can do rotations in 3D space to QSGNodes for one.

Demolishun
  • 1,592
  • 12
  • 15