PDA

View Full Version : QPushbotton - WordWrap



anju123
13th June 2007, 19:37
I need the text in the QPushbutton to wrap automatically when it is more than the button size. I cannot increase the width of the button as there is no space for it. kindly suggest a way.

marcel
13th June 2007, 20:20
You will have to take the button text, the button width and the button font metrics and insert manually "\n"-s in the string.

Or you can use an image for the button and leave the text for the tooltip.

Regards

wysota
13th June 2007, 20:37
Or you can subclass the button and draw it yourself using proper style primitives.

anju123
13th June 2007, 21:40
thanks for the replies..
Actually I am working on the internatiolisation project and the pushbutton text may vary according to the language use.
The option of manually inserting the '\n' by calculating the width of the button and the text size looked to be a complicated work, So i thought may be there is something like QLable wordwrap feature that we could use for QPushbutton as well.
But the more I surfed the net I am getting a feeling that the only option is by manual insertion of new line character.
Please advise

marcel
13th June 2007, 21:59
But it is not that hard.
Assuming that the character size in pixels for the current button font is x pixels, then the maximum size of the text is textLength*x=T.

Given the width of the button, lets say W:
float Y = T/W.

If Y <= 1.0 then the text completely fits.
If Y > 1.0 then you must insert ~Y "\n"s every ~W/x characters.

T, Y and W must be real numbers. When you need to compute the "\n" indexes just approximate through an integer.

Regadrs

wysota
13th June 2007, 22:06
I wouldn't do any manual inserts. If the size of the container changes, you'd have to re-layout those \n characters again. It's much simpler to subclass and ask QPainter::drawText to wrap the text. But maybe there are better ways. If not, I suggest subclassing instead of manual changes of the text.

anju123
14th June 2007, 06:13
Thanks Marcel,
I had done a lot of calculation to get the desired result, but I will try your solution today.
Wysota, regarding the relayout problem , the problem started because the size of the container could not be changed as it is already occupying maximum space. Regarding your solution of subclassing and using drawtext function, I am still not clear as how that would lead to wordwrap of the text on the button. Can you please elaborate more.

wysota
14th June 2007, 09:11
Wysota, regarding the relayout problem , the problem started because the size of the container could not be changed as it is already occupying maximum space.
What if you want to decrease the size, the font size or application style changes?


Regarding your solution of subclassing and using drawtext function, I am still not clear as how that would lead to wordwrap of the text on the button. Can you please elaborate more.
There are drawText variants that do word wrapping, so essentially it's just a matter of asking the style to draw the button itself without the text and then draw the text using drawText, passing the rect() of the button (maybe adjusted a little to have a margin) as the rectangle the text should occupy.

Example result attached.

Hub++
18th July 2008, 14:56
Hello,

As I am still looking for a clean method to wrap the text in a QPushButton can the Guru or someone else, that have managed to deal with the problem, post the exact code to wrap the text in one QPushButton.

I am really waiting for your response as I don't have any clear ideas of what to do because of the numerous unclear answers given in all the forums about qt.

Thanks a lot by advance,
Hub++

wysota
20th July 2008, 15:07
It depends what you want to do. Adding newlines (\n) to your text should force wrapping it at that point. If you want the flow to adjust depending on the width of the button, you need to subclass QPushButton and reimplement the part responsible for drawing the text and calculating the size of the button. For drawing you need to use a QPainter::drawText() overload that takes flags as one of its parameters and pass the wrapping flag.

alitoh
24th January 2012, 18:33
Hello, sorry for bringing this up after so long, but I think this is exactly what I need and I want to make sure I am understanding this correctly.

I am derivating from QPushButton, but want to wrap text, so ths is what I did:



class NEWPushButton : public QPushButton
{
Q_OBJECT
public:
void drawText ( const QRect & rectangle, int flags, const QString & text, QRect * boundingRect = 0 )
{
QPainter::drawText ( rectanble, flags, text, boundingRect );
}
};


is this what you had in mind, Wysota? I am guessing the proper "boundingRect" would be the button's (which is what I want the text to wrap to, after all).

wysota
24th January 2012, 19:51
Making that method public doesn't make much sense and the syntax is incorrect, however the approach itself seems ok.

alitoh
24th January 2012, 20:07
You are totally right, I was too focused on the approach iteself and made silly mistakes.

Here is the (working) code I came up with using your suggestion, for future reference and archiving:



class NEWPushButton : public QPushButton
{
Q_OBJECT
private:
void drawText ( const QRect & rectangle, int flags, const QString & text, QRect * boundingRect = 0 )
{
QPainter painter;
painter.drawText ( rectanble, flags, text, boundingRect );
}
};

wysota
24th January 2012, 21:27
Should be:


QPainter painter(this);

Also be sure to reimplement sizeHint().

alitoh
24th January 2012, 21:38
Also be sure to reimplement sizeHint().

Why? I haven't compiled it yet on the real application (a big solution).

wysota
24th January 2012, 21:41
Because size of the text with wordwrapping is different than the one without word wrapping.