April 26, 2012

xardas008 xardas008
Lab Rat
28 posts

[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:

  1. Item {
  2.    Text {        // a caption
  3.       font.bold: true
  4.       ...
  5.    }
  6.  
  7.    MyComponent {        // written in Qt, paints a timeline with text underneath
  8.      ...
  9.    }
  10. }

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)

5 replies

April 26, 2012

xardas008 xardas008
Lab Rat
28 posts

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?

April 27, 2012

chriadam chriadam
Ant Farmer
181 posts

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

April 27, 2012

xardas008 xardas008
Lab Rat
28 posts

  1. Rectangle {
  2.     id: rect1
  3.     Text {
  4.         id: text1
  5.         font.bold: true
  6.         font.pointSize: 20
  7.     }
  8.     MyQtComponent{
  9.         id: other1
  10.         ... //(mostly anchors)
  11.     }
  12. }
  13.  
  14. Rectangle {
  15.     id: rect2
  16.     Text {
  17.         id: text2
  18.         font.bold: true
  19.         font.pointSize: 20
  20.     }
  21.     MyQtComponent {
  22.         id: other2    // draws some text with QPainter inside qt, not qml, this one gets the font settings of text1
  23.         ...
  24.     }
  25. }

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.

April 27, 2012

xardas008 xardas008
Lab Rat
28 posts

Ok problem solved. I added a

  1. QFont font("Arial",10);
  2. 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.

April 30, 2012

mbrasser mbrasser
Ant Farmer
452 posts

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

 
  ‹‹ Copy paste from Xemacs editor to qtextedit does not work      How to get the 3D coordinates of mouse position in Quick3D? ››

You must log in to post a reply. Not a member yet? Register here!