Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 28
Registered: ‎06-25-2009
My Device: 9930
My Carrier: China Telecom

Re: how to obtain the width of a Qstring belonging to a TextArea?

To be honest, many things are difficult in cascades now, when will the next beta come?

 

We need draw in the form, it is reallly urgent.

Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: how to obtain the width of a Qstring belonging to a TextArea?

Drawing in the form is a different topic than getting the width of a QString in a TextArea.

 

See:

http://supportforums.blackberry.com/t5/Cascades-Development-Knowledge/Using-QImage-and-QPainter-to-P...

http://supportforums.blackberry.com/t5/Cascades-Development-Knowledge/Using-OpenGL-in-a-Cascades-For...

https://www.blackberry.com/jira/browse/BBTEN-1

 

Also, look at http://code.google.com/p/skia/

 

If you have further questions that are not on the topic of finding the width of QString in a TextArea, please start a new thread.


Stuart

Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: how to obtain the width of a Qstring belonging to a TextArea?

[ Edited ]

I tried getting a QFont and using QFontMetrics, but as I expected I didn't succeed.  The key line in the Qt documentation is: "Note that a QApplication instance must exist before a QFont can be used."

 

I found this but haven't finished playing with it:

https://developer.blackberry.com/native/beta/documentation/good_citizen_initialize_2006633_11.html, specifically:

 

Load the font type to use for our color menu and then determine the height of the menu for use later when we're determining which menu button is pressed. The function bbutil_load_font() generates the font symbols at the appropriate size for the screen of the BlackBerry 10 device through the use of dpi (dots per inch). We also need to calculate the point size for the font, which is specific to the device, using the font size (15 point) and the dpi of a BlackBerry PlayBook (170) as a reference value.

int dpi = bbutil_calculate_dpi(screen_cxt);

if (dpi == EXIT_FAILURE) {
        fprintf(stderr, "Unable to calculate dpi\n");
        return EXIT_FAILURE;
}

int point_size = (int)(15.0f / ((float)dpi / 170.0f ));

font = bbutil_load_font("/usr/fonts/font_repository/monotype/arial.ttf", point_size, dpi);
  
if (!font) {
    return EXIT_FAILURE;
}

float text_width, text_height;
bbutil_measure_text(font, "Color Menu", &text_width, &text_height);
menu_height = text_height + 10.0f + button_size_y * 4;

 

(See also: http://devblog.blackberry.com/2012/07/blackberry-10-font-changes/)

 

Let me know if this helps!

 

Stuart

Developer
Developer
Posts: 174
Registered: ‎03-05-2009
My Device: Dev Alpha
My Carrier: -

Re: how to obtain the width of a Qstring belonging to a TextArea?

Hi Stuart,

 

i am having the same need i.e to calculate actual screen size (dpi/pt) of a string in a Label (or any other ui control)...

 

ran through the above code but:

"/usr/fonts/font_repository/monotype/arial.ttf"

how do i know what font is being used by the ui control (Label) ??

 

 

Highlighted
Developer
Posts: 63
Registered: ‎09-10-2009
My Device: Z10, Bold 9700
My Carrier: O2 Germany

Re: how to obtain the width of a Qstring belonging to a TextArea?

I've ran into the same problem, too, needing to calculate both the font height used by the system as well as the width in pixels a string will use in the UI. How to find out which font is used by the system for a specific UI control?

Or has anyone found a different solution to this problem?
----

My Twitter: @hendrik_dev
BlackBerry Development Advisor
Posts: 15,264
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: how to obtain the width of a Qstring belonging to a TextArea?

If you know the font being used, you can load it with free type and do so with a function available from the free type library.    bb_util.c in the OpenGL samples and OpenGLView.cpp in GoodCitizenCascades includes functions to load a font, check on the size of text with the font, and render it in OpenGL.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 16,786
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: how to obtain the width of a Qstring belonging to a TextArea?

and how do i know the font being used? on my DevAlpha i did not find a font selection, only a size selector in the options. Unless you change the font within your application we only have one font to consider, correct? (and which would that be? arial?) or is the font changeable somewhere else in the options?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
BlackBerry Development Advisor
Posts: 15,264
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: how to obtain the width of a Qstring belonging to a TextArea?

You can't really.  What are you going to use it for?  Cascades isn't really designed for manual layouts.  We recommend using the flow layouts over any absolute positioning so that your application can scale to different resolutions.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Developer
Posts: 174
Registered: ‎03-05-2009
My Device: Dev Alpha
My Carrier: -

Re: how to obtain the width of a Qstring belonging to a TextArea?

@MSohm

what if you need to do a custom button where the text displayed on the button changes based on the language? 

cascades button simply fades out the text Smiley Sad 

 

for example

in EN, the width of the button can be 100px

in FR, w=180px

in ES, w=200px

 

now you will probably tell me to hardcode these sizes, but then we will have to hardcode for each language. And also, what if that text is simply dynamic?