4

I am trying to make a Qt application which has an auto hiding menu bar. How can i do that?

Charles Sprayberry
  • 7,741
  • 3
  • 41
  • 50
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
  • 1
    Can you give more detail about the effect you want to achieve? – ypnos Apr 19 '11 at 16:09
  • I want menu bar to appear only when mouse cursor is between 0-50 pixels of program area vertically, if not then hide itself automatically. Like windows task bar auto hide function. – Olcay Ertaş Apr 19 '11 at 18:28

1 Answers1

3

That's an interesting task ! Ok, lets see... I'd suggest you putting a code that keeps track of mouse cursor movement in QMainWindow::centralWidget(). You need to call QWidget::setMouseTracking(true) first to be able to keep track of your mouse movement (they are turned off by default). The code can look like this:

QMainWindow *mainWindow = new QMainWindow;
MyWidget * myWidget = new MyWidget(mainWindow);
myWidget->setMouseTracking(true);
mainWindow->setCentralWidget(myWidget);

And then in your widget QWidget::mouseMove() event you need to detect whether you are in the correct area. The code can look like this:

void MyWidget::mouseMoveEvent(QMouseEvent * event) {
    bool menuVisible = inCorrectArea(event->pos());
    mainWindow->menuBar()->setVisible(menuVisible);
    ...
}

There are several ways to get access to "mainWindow" in your MyWidget. One of them is to store a pointer in MyWidget private variable when you pass MainWindow in its MyWidget constructor. You can also issue a signal from your MyWidget and handle it in MainWindow.

Hope this helps.

Barbaris
  • 1,256
  • 7
  • 7