QToolButton设置弹出菜单
QToolButton *toolButton= new QToolButton;
QMenu *menu=new QMenu();
menu->addMenu(tr("0"));
menu->addMenu(tr("1"));
menu->addMenu(tr("3"));
toolButton->setIcon( QPixmap( "images//photo1.png" ) );
toolButton->setIconSize( QPixmap( "images//photo1.png").size() );
toolButton->setToolButtonStyle( Qt::ToolButtonTextUnderIcon);
toolButton->setPopupMode(QToolButton::InstantPopup );
toolButton->setMenu(menu);
弹出菜单的弹出方式有以下几种模式参考(参考Qt4.1.0参考文档):
ToolButtonPopupMode
Describes how a menu should be popped up for tool buttons that has a menu set or contains a list of actions.
| Constant | Value | Description |
|---|---|---|
| QToolButton::DelayedPopup | 0 | After pressing and holding the tool button down for a certain amount of time (the timeout is style dependant, see QStyle::SH_ToolButton_PopupDelay), the menu is displayed. A typical application example is the "back" button in some web browsers's tool bars. If the user clicks it, the browser simply browses back to the previous page. If the user presses and holds the button down for a while, the tool button shows a menu containing the current history list |
| QToolButton::MenuButtonPopup | 1 | In this mode the tool button displays a special arrow to indicate that a menu is present. The menu is displayed when the arrow part of the button is pressed. |
| QToolButton::InstantPopup | 2 | The menu is displayed, without delay, when the tool button is pressed. In this mode, the button's own action is not triggered. |
本文介绍如何在Qt中使用QToolButton创建并设置弹出菜单。通过实例代码展示了添加菜单项、设置图标、调整样式及选择弹出模式的方法。

7580

被折叠的 条评论
为什么被折叠?



