QToolBar的菜单延迟

QToolBar的菜单延迟,qt,Qt,我在QToolBar上创建了一个QAction并向其中添加了QMenu,从而获得了一个菜单。单击图标时,如何在菜单出现之前消除延迟 QToolBar *myToolBar = new QToolBar(this); QAction *myAction = new QAction(helloIcon, tr("Hello"), this); myToolBar->addAction(myAction); helloMenu = new QMenu(this); QAction *actio

我在QToolBar上创建了一个QAction并向其中添加了QMenu,从而获得了一个菜单。单击图标时,如何在菜单出现之前消除延迟

QToolBar *myToolBar = new QToolBar(this);
QAction *myAction = new QAction(helloIcon, tr("Hello"), this);
myToolBar->addAction(myAction);

helloMenu = new QMenu(this);
QAction *actionWorld = helloMenu->addAction(tr("World"));
QAction *actionUniverse = helloMenu->addAction(tr("Universe"));
myAction->setMenu(helloMenu);

以下是一种实现方法,以便在打开菜单时不会出现延迟:

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}
如果这似乎没有帮助,您也可以尝试调用
QMenu
上的
adjustSize()
,查看它是否会更早地执行任何计算


希望有帮助。

这应该会有帮助,但我不知道这是否是最好的解决方案:

#ifdef OS_WIN
#include <QWindowsStyle>

class FixPopupDelayWinStyle : public QWindowsStyle
{
public:
    int styleHint(StyleHint hint, const QStyleOption* opt = 0, const QWidget* widget = 0, QStyleHintReturn* returnData = 0 ) const
    {
        if (hint == QStyle::SH_ToolButton_PopupDelay)
            return 0;
        return QWindowsStyle::styleHint(hint, opt, widget, returnData);
    }
};
#endif


#ifdef OS_WIN
_toolBar_contains_action_with_Menu->setStyle(new FixPopupDelayWinStyle);
#endif
#ifdef OS#u WIN
#包括
类FixPopupDelayWinStyle:公共QWindowsStyle
{
公众:
int styleHint(styleHint提示,常量QStyleOption*opt=0,常量QWidget*widget=0,QStyleHintReturn*returnData=0)常量
{
if(hint==QStyle::SH_ToolButton_PopupDelay)
返回0;
return QWindowsStyle::styleHint(提示、选项、小部件、返回数据);
}
};
#恩迪夫
#如果你赢了
_工具栏包含带有菜单->设置样式的操作(新的FixPopupDelayWinStyle);
#恩迪夫

正确的方法是自己在工具栏中插入QToolButton,而不是QAction。QToolButton类有一个popupMode属性,您可以在其中设置所需的模式(延迟、instantPopup、菜单)

以下是一个工作示例:

QToolBar *myToolBar = new QToolBar(this);
QToolButton *myButton = new QToolButton();

QMenu *helloMenu = new QMenu(this);
QAction *actionWorld = helloMenu->addAction(tr("World"));
QAction *actionUniverse = helloMenu->addAction(tr("Universe"));

//set popup mode to InstantPopup so there will be no delay
myButton->setPopupMode(QToolButton::InstantPopup);
myButton->setMenu(helloMenu);
myButton->setIcon(helloIcon);
myButton->setText(tr("Hello"));

myToolBar->addWidget(myButton);

那没用;我不认为这是由于处理而导致的减速,而是在显示菜单之前编程的延迟。可能有一种方法可以改变“弹出模式”。。。QToolButton的文档说默认的popupDelay是600ms,我想这就是我所经历的延迟。但是我没有使用工具按钮,也没有办法访问操作的等效QToolButton,是吗?有没有简单的方法来调整延迟的菜单时间?我认为默认值有点慢
helloMenu->sizeHint();

// or you can try
qDebug() << helloMenu->sizeHint();
#ifdef OS_WIN
#include <QWindowsStyle>

class FixPopupDelayWinStyle : public QWindowsStyle
{
public:
    int styleHint(StyleHint hint, const QStyleOption* opt = 0, const QWidget* widget = 0, QStyleHintReturn* returnData = 0 ) const
    {
        if (hint == QStyle::SH_ToolButton_PopupDelay)
            return 0;
        return QWindowsStyle::styleHint(hint, opt, widget, returnData);
    }
};
#endif


#ifdef OS_WIN
_toolBar_contains_action_with_Menu->setStyle(new FixPopupDelayWinStyle);
#endif
QToolBar *myToolBar = new QToolBar(this);
QToolButton *myButton = new QToolButton();

QMenu *helloMenu = new QMenu(this);
QAction *actionWorld = helloMenu->addAction(tr("World"));
QAction *actionUniverse = helloMenu->addAction(tr("Universe"));

//set popup mode to InstantPopup so there will be no delay
myButton->setPopupMode(QToolButton::InstantPopup);
myButton->setMenu(helloMenu);
myButton->setIcon(helloIcon);
myButton->setText(tr("Hello"));

myToolBar->addWidget(myButton);