[solved]Strange behavior of QML Elements written in Qt



  • Hello guys,

    I've written my own component in Qt and exposed it to QML to use it in another component.
    So long everything works fine.

    But when I add my new Component twice on my window, the first one gets rendered as expected, but the second one has the font of the title.

    To make my Problem a little bit more clear I try to give an example:
    @Item {
    Text { // a caption
    font.bold: true
    ...
    }

    MyComponent { // written in Qt, paints a timeline with text underneath
    ...
    }
    }@

    My component looks something like this. I have a Text item as caption, and a small timeline where everything is painted in Qt (the line + text)



  • Things become even stranger. It looks like the font of timeline 2 is bound to the font of the caption of timeline 1.

    But how could that be?



  • I don't fully understand. Can you post a complete but minimal example which shows the issue?



  • @
    Rectangle {
    id: rect1
    Text {
    id: text1
    font.bold: true
    font.pointSize: 20
    }
    MyQtComponent{
    id: other1
    ... //(mostly anchors)
    }
    }

    Rectangle {
    id: rect2
    Text {
    id: text2
    font.bold: true
    font.pointSize: 20
    }
    MyQtComponent {
    id: other2 // draws some text with QPainter inside qt, not qml, this one gets the font settings of text1
    ...
    }
    }
    @

    In MyQtComponent I use QPainter to draw some text without setting a font explicit.
    The problem I have is, that as long as the font settings of text1 is set to 20 and bold, the font of other2 is also in this font setting. The font of other1 is normal. Both components are on the same window.



  • Ok problem solved. I added a
    @QFont font("Arial",10);
    painter->setFont(font);
    @
    to my Qt component and now everything gets correctly rendered.
    Seems that Qt took one of the last font settings used on the same parent qml component.



  • Hi,

    QDeclarativeView sets the QGraphicsView::DontSavePainterState optimization flag by default, which means any changes you make to the painter (such as painter->setFont()) in your paint() function need to be undone at the end of the paint() function, or they may "leak" to other items.

    Regards,
    Michael


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.