PDA

View Full Version : How do I paint custom controls & then propagate events?



slobberchopz
24th December 2013, 07:41
For example, one widget I am trying to create is a mix of QToolButton and QLineEdit.

When leaveEvent() occurs, it appears like a QToolButton with the icon/text on the left & the menu-arrow on the right (QToolButton.MenuButtonPopup). When enterEvent() occurs, the left half of the button (icon/text) becomes a QLineEdit, while the right half is still a drop down menu.

I have been trawling through whatever forums google search brings up, and while I loosely understand how to paint it, how do I connect the events properly? If I paint the QLineEdit, how do I get that part of the custom widget to act and respond like a QLineEdit? (Python preferred, C++ is ok, also an example including the paint event would be even better ;)

[QComboBox will not work, and definitely the other widgets I'd like to create don't have any approximate equivalent. Also, I know I can build a widget with other simpler widgets, and then mess with the stylesheets to achieve something similar looking... but I know there is an answer to the above, because Qt does it, and it would have better cross-system styling then writing custom stylesheets and trying to figure out what to do to make it exactly the same..]

wysota
24th December 2013, 09:27
Drawing is done via QStyle API. I suggest you derive your widget from QLineEdit to avoid having to reimplement all its functionality, push the content to the left (by setting content margins) and use QStyle API to draw the drop down indicator. Handle events for the drop down and show a menu when it should be activated.

anda_skoa
24th December 2013, 14:09
As an approximation you could also try a composite widget, using a QLabel and a QLineEdit in a QStackedWidget on the left half and a button on the right half.

You then implement enter and leave event such that you trigger which of the two left side widgets is the one visible in the stacked widget.

Cheers,
_

slobberchopz
24th December 2013, 20:52
Thanks. Working with these suggestions.