2015-07-21 115 views
-1

我需要通過按Alt鍵(如在FireFox中)來隱藏/顯示主菜單功能。我知道如何隱藏並顯示它,但在顯示後我無法突出顯示菜單。我試過menuBar()->actions()[0].hover(),activateWindow(),setActiveAction(),activate(QAction::Hover),但沒有什麼幫助。我該怎麼做?也許我應該使用WinApi函數而不是Qt?Qt如何突出顯示菜單?

bool MainWindow::event(QEvent *event){ 
    if (event->type() == QEvent::KeyPress) 
    { 
     QKeyEvent *ke = static_cast<QKeyEvent*>(event); 
     if (ke->key() == Qt::Key_Alt) 
     { 
      keyReleaseEvent(ke); 
      return true; 
     } 
    } 
    return QMainWindow::event(event); 
} 

手柄Alt鍵

void MainWindow::keyReleaseEvent (QKeyEvent* event) 
{ 
    if (event->key() == Qt::Key_Alt){ 
     if (menuBar()->isHidden()){ 
      menuBar()->show(); 
      menuBar()->setFocus(Qt::MenuBarFocusEvent); //here I trying to highlight the menu 
     } 
     else{ 
      menuBar()->hide(); 
     { 
    } 
} 

回答

0

嘗試調試一下,看看發生了什麼。

你可以這樣做:

if (menuBar()->hasFocus()) 
    qDebug() << " I have the focus"; 
else 
    qDebug() << " I don't have the focus"; 

,看看它是否有焦點,但工作不良或沒有焦點,我認爲這是問題...某處它失去它

一個小技巧,可以幫助您使用:

QTimer::singleShot(0, menuBar(), SLOT(setFocus())); 

MainWindow::keyReleaseEvent

而最後一件事......請注意,設置焦點需要一個focusPolicy,也許this可以幫助你。

+0

我已經試過這個。它有重點,但沒有突出顯示 – Dimanesson

+0

我編輯過它,忘記了focuspolicy,你有沒有設置它? – Megasa3

+0

試過'QTimer :: singleShot(0,menuBar(),SLOT(setFocus()));',它沒有幫助 – Dimanesson