PDA

View Full Version : Text shifts when control appears...



MrGarbage
17th January 2008, 15:20
Hi,

After spending way too much time on this annoying little
problem I've decided to go to the experts..

In Designer I have created this small custom control.
It is a lineEdit with a left and right arrow (Pushbutton) at either
end. Normally the arrows are hidden.

When the control gets focus the arrows appear and clicking
one results in the linedit text changing.

This works fine. However if the text is sufficiently long then
when the arrows appear the text shifts slightly to the left.

I either want to keep the text from shifting, or perhaps
there is a better design where the arrows appear translucently
over the ends of the linedit without effecting the size of the
edit control?

Thank you!

Mark

wysota
17th January 2008, 16:37
Change the contents rect of the lineedit using setContentsMargins - the line edit is only rendered inside the contents rect. Then you can add those arrows inside the lineedit but outside the contents rect. Of course this will make your edit field smaller regardless of the presence of arrows. An alternative is to use stylesheets and set padding on the line edit. Then you can place the arrows inside the box. And use tool buttons instead of push buttons.

An alternative to that is to use QwwTextSpinBox (http://www.wysota.eu.org/wwwidgets/): http://www.wysota.eu.org/wwwidgets/screenshots/qwwtextspinbox.png

MrGarbage
17th January 2008, 20:33
I tried both of your suggestions but the problem persists. The StyleSheet option seemed to have the most potential, but I cannot get that to operate any better. Perhaps I am
missing something... I've attached my .ui..

Mark

wysota
17th January 2008, 21:43
You can't do what I meant in Designer. You have to do something that my (unpublished yet, see attachment for an image - it is a single widget derived from QLineEdit) line edit class does - it reduces the contentsRect from the right side to fit the button inside the widget. But you have to do that in hand written code an not Designer (unless of course you'll use one of my widgets that come with a Designer plugin).

You'll have to reduce the contentsRect and reimplement the resizeEvent to position the buttons where you need them. All that without using a layout (that's why you reimplement resizeEvent).

momesana
18th January 2008, 03:12
The stylesheet approach requires you to derive your widget from QLineEdit and use manual placement of the QToolButtons (or simple QWidgets) inside the QLineEdit. This you definetly can't do from within designer so you have to get your hands dirty with writing the code the good old way. You should read this very interresting post by Girish: http://labs.trolltech.com/blogs/2007/06/06/lineedit-with-a-clear-button/
It worked quite fine for me.