PDA

View Full Version : Implementing MenuBar AutoHide function



viking
29th March 2007, 05:22
Expert advise needed!:confused:

Does QT4 provide any such feature? Or must one detect mouse positions and manually calculate if the menubar should be hidden (read: empty all items from menubar)?

Cheers,
Viking.

aamer4yu
29th March 2007, 06:15
U can use QWidget::hide () to hide the menubar... as menu bar is derived from QWidget :)

wysota
29th March 2007, 06:29
(read: empty all items from menubar)?

What sense would it make to remove all items from the menu bar? It wouldn't hide the menu itself, would it? And if you hide the menu bar... how do you want to bring it back (I don't say it's not possible)?

viking
29th March 2007, 06:30
10q aamer4yu,

That indeed is a better idea. Dint strike me.
But anyway, the mouse position will have to be manually monitored.

-vkng

viking
29th March 2007, 06:32
What sense would it make to remove all items from the menu bar? It wouldn't hide the menu itself, would it? And if you hide the menu bar... how do you want to bring it back (I don't say it's not possible)?
An empty menu bar is not displayed. Well, that was my thinking.
But aamer4yu has suggested a better solution.

Bringing the menubar back should be easy. Just repopulate the menubar with the previously saved menu items.

-vkng

aamer4yu
29th March 2007, 06:46
What wysota meant was removing items wont hide the menu bar..... dont question his knowledge about Qt.... I dont stand anywhere near him....

also to hide/ show the menu bar u can use some keyboard shortcuts, or provide the same from toolbar...
and if u want that some actions in the menu shouldnt work, just disable the actions....

viking
29th March 2007, 06:50
What wysota meant was removing items wont hide the menu bar..... dont question his knowledge about Qt.... I dont stand anywhere near him....

also to hide/ show the menu bar u can use some keyboard shortcuts, or provide the same from toolbar...
and if u want that some actions in the menu shouldnt work, just disable the actions....
No pun intended guys :)

about the actions, actually i indeed need an autohide menubar. so i guess i'll just have to track the mouse movements...

jpn
29th March 2007, 07:12
By the way, just as a side note, possible shortcuts assigned to menu actions won't trigger while the menu bar is hidden.

wysota
29th March 2007, 07:18
An empty menu bar is not displayed if you don't populate it, because it's not created. When you do created, things change. Anyway, if you hide the menu bar, how will you know when you want to get it back?

viking
29th March 2007, 07:25
An empty menu bar is not displayed if you don't populate it, because it's not created. When you do created, things change. Anyway, if you hide the menu bar, how will you know when you want to get it back?
1. I see. Well, that rules out my idea. I will have to hide it.
2. If I hide the menubar, I will have to calculate the coords in the mouseMove Event and if they are near the menubar location on the mainwindow, i show the menubar. would it work?

-vk

viking
29th March 2007, 07:26
By the way, just as a side note, possible shortcuts assigned to menu actions won't trigger while the menu bar is hidden.
thanks jpn. i shall keep that in mind. however, my primary focus is to implement an autohide menubar. but your suggestion is good.

vk

wysota
29th March 2007, 08:03
2. If I hide the menubar, I will have to calculate the coords in the mouseMove Event and if they are near the menubar location on the mainwindow, i show the menubar. would it work?

If you enable mouse tracking then yes. But you'll have to do it in an event filter which you have to apply on your central widget and any of its subwidgets, because if a subwidget handles a mouse move event itself, it won't propagate it to the parent. Of course you'll have to calculate the coordinates in the main window coordinate system. Lots of work :)

aamer4yu
29th March 2007, 08:50
See the attachment...
hope this helps :)

viking
29th March 2007, 09:49
If you enable mouse tracking then yes. But you'll have to do it in an event filter which you have to apply on your central widget and any of its subwidgets, because if a subwidget handles a mouse move event itself, it won't propagate it to the parent. Of course you'll have to calculate the coordinates in the main window coordinate system. Lots of work :)
Yes! Thanks for your inputs. btw, what component could replace the trapezoidal menubar that appears on the top when you connect to Remote Desktop in Windows. Basically that is what I am trying to achieve. Limitation of a menu bar is that it must only be applied to a gui component that is of type QMainWindow. But what if I wanted to add an Autohide Bar on the top of the screen like the one we see when connecting to a remote computer using mstsc in Windows?

viking
29th March 2007, 09:59
See the attachment...
hope this helps :)


See the attachment...
hope this helps :)

Thanks a ton aamer4yu for this. I really appreciate it! I tried out the app. I feel it needs some debuggings coz in the function



void CMainWindow::initial()
{
menuBarRect = menuBar()->geometry();
menuBar()->hide();

}

the moment the menubar hides, the Y2 in the geometry becomes -1. hence, goin forward,


if(menuBarRect.contains(mousePos) )
{
menuBar()->show();
isMenuOn = true;
}
else
{
menuBar()->hide();
isMenuOn = false;
}

control never enters the if part, it always goes into the else. i'll look into it. of course your inputs would be valuable.

as i replied to wysota, i actually want to implement the trapezoidal menubar that appears on the top when you connect to Remote Desktop in Windows. the one with a fancy PIN button that toggles autohide... still searching for a way to do that... any help wud be appreciated! :)

viking
29th March 2007, 10:19
See the attachment...
hope this helps :)

I changed the function, not in the best possible way, but its working now...



void CMainWindow::mouseMoveEvent ( QMouseEvent * event )
{
QPoint mousePos;
mousePos = event->pos();

if( (menuBarRect.bottom() + 20) > mousePos.y() )
{
menuBar()->show();
isMenuOn = true;
}
else
{
menuBar()->hide();
isMenuOn = false;
}

QMainWindow::mouseMoveEvent(event);
}

aamer4yu
29th March 2007, 10:22
Seems u havent understood my program or I have understood what u wanted.
I implemented auto hide like in taskbar in windows.

What I do is, initially using the single shot timer I capture the geometry of the menubar. This is a private member, so its value is retained. As soon as I set the geometry cordinated in menuBarRect, I hide the menubar. So u wont see a menubar initially.
When u move the mouse in the upper part of the window, where the menu is supposed to appear, I capture it, and show the menubar.

Did u try running the code ?? or u just walked thru manually ?? also isMenuOn variable is not needed. I was just trying with that variable, and forgot to delete it.

Also I havent seen your "trapezoidal menubar that appears on the top when you connect to Remote Desktop in Windows." so cant help u on that right now...

if u have problem in running my code, ask me.... :)

aamer4yu
29th March 2007, 10:25
what error were u getting before editing the code ??
it was working fine for me...

viking
29th March 2007, 10:26
Seems u havent understood my program or I have understood what u wanted.
I implemented auto hide like in taskbar in windows.

What I do is, initially using the single shot timer I capture the geometry of the menubar. This is a private member, so its value is retained. As soon as I set the geometry cordinated in menuBarRect, I hide the menubar. So u wont see a menubar initially.
When u move the mouse in the upper part of the window, where the menu is supposed to appear, I capture it, and show the menubar.

Did u try running the code ?? or u just walked thru manually ?? also isMenuOn variable is not needed. I was just trying with that variable, and forgot to delete it.

Also I havent seen your "trapezoidal menubar that appears on the top when you connect to Remote Desktop in Windows." so cant help u on that right now...

if u have problem in running my code, ask me.... :)
@ I did! And I ran it in debug mode as well. Thats when i found out that the control never enters to the part where it is supposed to show the menubar.

@ I'll try to send you a screenshot of that "trapezoidal menubar that appears on the top when you connect to Remote Desktop in Windows." But hey, thanks for all the time man! Appreciate it a lot !!! :)

viking
29th March 2007, 10:27
what error were u getting before editing the code ??
it was working fine for me...
It was? Thats strange. It did not work for me. I'll try again with your code. And try to find out.

aamer4yu
29th March 2007, 10:32
@ I did! And I ran it in debug mode as well. Thats when i found out that the control never enters to the part where it is supposed to show the menubar.

It does enter.... move the mouse close to the title bar....
what platform are u using by the way ? I tried on windows with Qt 4.2.0 and its working fine for me. The menu shows up when u move the mouse just below the title bar area, where it shud appear

viking
29th March 2007, 10:36
It does enter.... move the mouse close to the title bar....
what platform are u using by the way ? I tried on windows with Qt 4.2.0 and its working fine for me. The menu shows up when u move the mouse just below the title bar area, where it shud appear
I tried! It did not work for me. Windows here too. But Qt 4.1.5. Could that be the reason? Doesnt seem so....

aamer4yu
29th March 2007, 10:42
it could be :D

wysota
29th March 2007, 10:53
Yes! Thanks for your inputs. btw, what component could replace the trapezoidal menubar that appears on the top when you connect to Remote Desktop in Windows.

A custom widget.

viking
29th March 2007, 11:30
A custom widget.
Thanks wysota. Will try to implement that using a custom widget...

giverson
29th March 2007, 19:20
keep in mind that MouseMoveEvents only get passed when they move over the widget's editable boxes unless you have a mouse button pressed down.

Because of this, your scrollbars and stuff like that don't process mousemove or mousepress events, AFAIK. I would assume it's the same way with the titlebar.

Of course, I've never enabled mousemovement on a central widget so I don't know what kind of effect that will have on subchildren. Good luck getting this working.

viking
30th March 2007, 12:56
keep in mind that MouseMoveEvents only get passed when they move over the widget's editable boxes unless you have a mouse button pressed down.

Because of this, your scrollbars and stuff like that don't process mousemove or mousepress events, AFAIK. I would assume it's the same way with the titlebar.

Of course, I've never enabled mousemovement on a central widget so I don't know what kind of effect that will have on subchildren. Good luck getting this working.
thanks giverson!
you made the post JIT :)

viking
2nd April 2007, 04:58
Guys,

Thanks a lot for all the help! I finally implemented this.

1. Created a custom widget.
2. setGeometry( )
3. Displayed the widget

4. it _is_ true that the mouseMoveEvent did work ONLY on the parent form, and did not work when the mouse was over a child form (editable box, or anything else for that matter. in my case it was another widget). And truly, setting the mouse tracking to true for the parent widget did NOT work. but, when i also set the MouseTracking to true for the child widget, i found the PARENT widget getting singals of mouse movements on the child. To re-clarify, I set the setMouseTracking(true) in both the child and the parent widget, and implemented the mouseMoveEvent in the PARENT ONLY. and yet, on a mouseMove on the child, the function in the parent is always called.

Is this behavior normal? That's the question!

-vk

aamer4yu
2nd April 2007, 05:15
i dont know :D...
just tried to track in the code... what i was able to get is setAttribute is the function u have to dig for...
Happy digging in the code,,, or wait till someone answers ;)

viking
2nd April 2007, 05:19
i dont know :D...
just tried to track in the code... what i was able to get is setAttribute is the function u have to dig for...
Happy digging in the code,,, or wait till someone answers ;)
let me look into setAttribute( ) function. :)
the behavior was really intriguing. and i tried replicating it again, and i was able to do so.
somehow setting the mousetracking of a child widget sends signals to the mouseMoveEvent function of the parent.

the experts should really explain this for the benefit of novices :)

-vk

jpn
2nd April 2007, 09:10
Mouse event propagation (http://labs.trolltech.com/blogs/2006/05/27/mouse-event-propagation/)

fatecasino
1st April 2011, 20:17
This post is really helpful!
I applied it and it worked fine in Linux version.
However it does not work in windows!
the menu bar never appears, does this have to do with the coordinates of the windows screen?!