Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [solved]Strange behavior of QML Elements written in Qt
Qt 6.11 is out! See what's new in the release blog

[solved]Strange behavior of QML Elements written in Qt

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xardas008
    wrote on last edited by
    #1

    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)

    1 Reply Last reply
    0
    • X Offline
      X Offline
      xardas008
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chriadam
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • X Offline
          X Offline
          xardas008
          wrote on last edited by
          #4

          @
          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.

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xardas008
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mbrasser
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved