我需要按Alt键来隐藏/显示主菜单功能(就像在FireFox中一样)。我知道如何隐藏和显示它,但是在显示之后我无法突出显示菜单。我已经尝试了menuBar()->actions()[0].hover()
,activateWindow()
,setActiveAction()
,activate(QAction::Hover)
,但没有任何帮助。我该怎么做?也许我应该使用WinApi函数而不是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 :(得分:0)
重复How to focus menuBar() with Qt(我刚才回答)
因为在另一个问题上没有接受答案,所以我不能将此标记作为重复标记。包括以下答案:
我想做同样的事情。我的解决方案,完整的例子,作为一个要点:
https://gist.github.com/xim/ee56564f425151ea2fa70f730d644873
因为它包含很多其他垃圾,一个最小的例子:
class AutoHidingMenuBar : public QMenuBar {
Q_OBJECT
public:
AutoHidingMenuBar() : QMenuBar() {
setMaximumHeight(0);
connect(qApp, &QApplication::focusChanged, this, &AutoHidingMenuBar::focusChanged);
}
private slots:
void focusChanged(QWidget *from, QWidget *to) {
bool inFocus = hasFocus() || isAncestorOf(focus) || hasFocusedChild();
if (inFocus && maximumHeight() == 0) {
auto action = activeAction();
setMaximumHeight(100);
if (action) {
// XXX This is a bit of a hack. We could do
// QCoreApplication::processEvents();
// setActiveAction(action);
// with almost the same effect, but then we *open* the first menu on single alt press...
auto evt = new QMouseEvent(QEvent::MouseMove, actionGeometry(action).center(), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
QCoreApplication::postEvent(this, evt);
}
} else if (!inFocus && maximumHeight() != 0)) {
setMaximumHeight(0);
}
}
private:
bool hasFocusedChild();
};
答案 1 :(得分:-1)
尝试调试它,看看发生了什么。
您可以执行以下操作:
import arrow
date_str = "2015-07-17 06:01:51.066141+00:00"
unix_time = arrow.get(date_str).timestamp
看看它是否有焦点但是效果不好或者它没有焦点我认为这是问题......某处它正在失去它
可以帮助您使用的一个小技巧:
import datetime
date_str = "2015-07-17 06:01:51.066141+00:00"
date_str_no_colon = date_str[:-3]+date_str[-2:] # remove last colon
dt_obj = datetime.datetime.strptime(date_str_no_colon, "%Y-%m-%d %H:%M:%S.%f%z")
unix_time = dt_obj.timestamp()
在if (menuBar()->hasFocus())
qDebug() << " I have the focus";
else
qDebug() << " I don't have the focus";
最后一件事......请注意,设置焦点需要一个focusPolicy,也许this可以帮助你。