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.
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.
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
Or you can subclass the button and draw it yourself using proper style primitives.
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
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
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.
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.
What if you want to decrease the size, the font size or application style changes?
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.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.
Example result attached.
Last edited by wysota; 14th June 2007 at 09:16.
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++
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.
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:
Qt Code:
{ Q_OBJECT public: { } };To copy to clipboard, switch view to plain text mode
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).
Making that method public doesn't make much sense and the syntax is incorrect, however the approach itself seems ok.
Bookmarks