PDA

View Full Version : Clickable area for QPushButton with menu



SKolaMunn
2nd March 2012, 14:55
I have a custom widget created in Qt Designer with a QPushButton. In code, I added a menu to the button with setMenu. For some reason, I can only "trigger" the button by clicking the right edge of it, if I click anywhere else (and its a fairly wide button), it does not register. If I click on the very edge, the menu pops up (it seems like I have just a 2-pixel wide area to click). I added a mousePressEvent in the parent widget to see what was happening and when I check for the child widget at the point the mouse was clicked, it does not give me that widget, I can't tell what widget it is (object name = "" so I assume its not a widget I explicitly added in Designer interfering ??) but it seems like there's possibly a widget above it that's interfering? Any suggestions? Even a hack at this point (to explicitly set the clickable area of the button?) as I really need a solution! I'm using layouts so I don't see how I could have an overlapping widget, it seems I must be getting the "background widget" underneath the button when I check the widget at that point. I've read about bugs on Mac OSX related to style sheets but I'm using Windows 7 and I am not using a style sheet on this widget.

high_flyer
2nd March 2012, 15:15
1. What is the logic of adding a menu to a button??
This is probably a very bad design - if you explain what you want to achieve (not what how, but what) we might suggest a better solution for it.
Read about QAction - it is probably what you need.
2. Show your code.
3. Please format your text in paragraphs, its hard to read it in such a big "chunk".

SKolaMunn
2nd March 2012, 15:19
Of course after I finally posted my question that's been troubling me for days, I think I've found the cause of the problem and, at least, a workaround. Maybe someone can suggest a better solution, but in the meantime, this might be relevant and useful to others.

My button is at the very top left corner of my widget. After looking closer at the widget returned in mousePressEvent childAt(evt->pos()), I see that the widget being returned is actually an instance of a class that I am using that inherits QWidget in order to make use of winEvent but is not actually explicitly added to my GUI/widget/layout/etc. The class was written by a collegue and I did not even realize it was inheriting QWidget until now. The object is used and apparently its "widget" is automatically placed at the top left position on top of everything else and is invisible. Therefore its taking precedence over my button. I moved my button over to the right and I can click it anywhere I want. So, I need to either move this other widget "under" my button or I guess to an empty location on my GUI or have it ignore mouse events but allow widgets under it to receive mouse events. Any suggestions appreciated.


high_flyer -

A button with a menu is a very useful tool!

I'd show code but I made the widget in Qt Designer and my class that uses the ui has 5000 lines of code and clearly wasn't doing anything to the button other then using "QPushButton::setMenu" so I don't know what piece of code would be relevant.

Anyways, I found the cause of my problem (above).


Solution -

I guess once the problem was found a solution was fairly obvious, I just set the size of the interfering widget to (0,0) (using setFixedSize).

Hope this is helpful to someone else!

high_flyer
6th March 2012, 10:37
high_flyer -

A button with a menu is a very useful tool!

I'd show code but I made the widget in Qt Designer and my class that uses the ui has 5000 lines of code and clearly wasn't doing anything to the button other then using "QPushButton::setMenu" so I don't know what piece of code would be relevant.
I admit I haven't seen it before - and indeed it seems Qt does have a QPushButton with a menu.
I have learned a new thing today too :)
Still, personally I don't understand what is the use of it - and in what way it is different then having a context menu called on the button - seems to do the same thing, just with a different user interaction (look) - and only under some styles.