赞
踩
QToolButton 类为命令或选项提供了一个快速访问按钮,通常在 QToolBar 中使用。
工具按钮是一种特殊按钮,可提供对特定命令或选项的快速访问。 与普通命令按钮相反,工具按钮通常不显示文本标签,而是显示图标。
按钮的外观和尺寸可通过 setToolButtonStyle() 和 setIconSize() 进行调整。工具按钮可以在弹出菜单中提供其他选项。 可以使用 setMenu() 设置弹出菜单。 使用 setPopupMode() 配置可用于带有菜单集的工具按钮的不同模式。 默认模式是 DelayedPopupMode,有时与 Web 浏览器中的“后退”按钮一起使用。 按住按钮一段时间后,会弹出一个菜单,显示要跳转到的可能页面列表。 超时取决于样式,请参阅 QStyle::SH_ToolButton_PopupDelay。
- Qt::ToolButtonStyle toolButtonStyle() const
- 此属性保存工具按钮是仅显示图标、仅显示文本还是在图标旁边/下方显示文本。
默认值为 Qt::ToolButtonIconOnly。
要使工具按钮的样式遵循系统设置,请将此属性设置为 Qt::ToolButtonFollowStyle。 在 Unix 上,将使用桌面环境中的用户设置。 在其他平台上,Qt::ToolButtonFollowStyle 仅表示图标。
- void setIconSize(const QSize &size)
- 此属性保存用于此按钮的图标大小。
默认大小由 GUI 样式定义。 这是图标的最大尺寸。 较小的图标不会放大。
- void setArrowType(Qt::ArrowType type)
- 此属性保存按钮是否显示箭头而不是普通图标。这将显示一个箭头作为 QToolButton 的图标。默认情况下,此属性设置为 Qt::NoArrow。
运行效果图:
- QVBoxLayout* vLayout = new QVBoxLayout();
- QHBoxLayout* hLayout = new QHBoxLayout();
-
- m_upButton = new QToolButton();
- m_doButton = new QToolButton();
- m_lfButton = new QToolButton();
- m_rgButton = new QToolButton();
-
- //By default, this property is set to Qt::NoArrow.
- m_upButton->setArrowType(Qt::UpArrow);
- //The default is Qt::ToolButtonIconOnly.
- m_upButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
- m_upButton->setIconSize(QSize(48, 48));
- m_upButton->setText(QString::fromLocal8Bit("塔塔"));
- m_upButton->setStyleSheet("QToolButton{border: none; background: rgb(68, 69, 73); color: rgb(0, 160, 230);}");
-
- m_doButton->setArrowType(Qt::DownArrow);
- m_doButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
- m_doButton->setIconSize(QSize(48, 48));
- m_doButton->setText(QString::fromLocal8Bit("塔塔"));
- m_doButton->setStyleSheet("QToolButton{border: none; background: rgb(68, 69, 73); color: rgb(0, 160, 230);}");
-
- m_lfButton->setArrowType(Qt::LeftArrow);
- m_lfButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
- m_lfButton->setText(QString::fromLocal8Bit("塔塔"));
- m_lfButton->setIconSize(QSize(48, 48));
- m_lfButton->setStyleSheet("QToolButton{border: none; background: rgb(68, 69, 73); color: rgb(0, 160, 230);}");
-
- m_rgButton->setArrowType(Qt::RightArrow);
- m_rgButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
- m_rgButton->setIconSize(QSize(48, 48));
- m_rgButton->setText(QString::fromLocal8Bit("塔塔"));
- m_rgButton->setStyleSheet("QToolButton{border: none; background: rgb(68, 69, 73); color: rgb(0, 160, 230);}");
-
- hLayout->addWidget(m_upButton);
- hLayout->addWidget(m_doButton);
- hLayout->addWidget(m_lfButton);
- hLayout->addWidget(m_rgButton);
- void setDefaultAction(QAction *action)
- 将默认操作设置为 action。
如果工具按钮具有默认操作,则该操作定义按钮的属性,例如文本、图标、工具提示等。
运行效果图:
- m_ColorButton1 = new QToolButton();
- //QAction* action = m_ColorButton->defaultAction();//默认返回值为NULL
- QAction* action = new QAction(QString::fromLocal8Bit("塔塔"));
- action->setIcon(QIcon(":/Res/Icon/ship.png"));
- action->setToolTip(QString::fromLocal8Bit("加油"));
- m_ColorButton1->setIconSize(QSize(64, 64));
- m_ColorButton1->setDefaultAction(action);
- m_ColorButton1->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
- void setMenu(QMenu *menu)
运行效果图:
- m_ColorButton2 = new QToolButton();
-
- QAction* action2 = new QAction(QString::fromLocal8Bit("塔塔2"));
- action2->setIcon(QIcon(":/Res/Icon/Picture.ico"));
- action2->setToolTip(QString::fromLocal8Bit("加油2"));
-
- QMenu* menu = new QMenu();
- menu->addAction(QIcon(":/Res/Icon/Home.ico"), QString::fromLocal8Bit("主页"));
- menu->addAction(QIcon(":/Res/Icon/Wifi.ico"), QString::fromLocal8Bit("Wifi"));
- menu->addAction(QIcon(":/Res/Icon/Update.ico"), QString::fromLocal8Bit("更新"));
- menu->addAction(QIcon(":/Res/Icon/Folder.ico"), QString::fromLocal8Bit("文件夹"));
- m_ColorButton2->setPopupMode(QToolButton::MenuButtonPopup);
- m_ColorButton2->setIconSize(QSize(64, 64));
- m_ColorButton2->setDefaultAction(action2);
- m_ColorButton2->setMenu(menu);
-
- m_ColorButton2->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
源码:
- QVBoxLayout* vLayout = new QVBoxLayout();
- QHBoxLayout* hLayout = new QHBoxLayout();
-
- m_upButton = new QToolButton();
- m_doButton = new QToolButton();
- m_lfButton = new QToolButton();
- m_rgButton = new QToolButton();
-
- //By default, this property is set to Qt::NoArrow.
- m_upButton->setArrowType(Qt::UpArrow);
- //The default is Qt::ToolButtonIconOnly.
- m_upButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
- m_upButton->setIconSize(QSize(48, 48));
- m_upButton->setText(QString::fromLocal8Bit("塔塔"));
- m_upButton->setStyleSheet("QToolButton{border: none; background: rgb(68, 69, 73); color: rgb(0, 160, 230);}");
-
- m_doButton->setArrowType(Qt::DownArrow);
- m_doButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
- m_doButton->setIconSize(QSize(48, 48));
- m_doButton->setText(QString::fromLocal8Bit("塔塔"));
- m_doButton->setStyleSheet("QToolButton{border: none; background: rgb(68, 69, 73); color: rgb(0, 160, 230);}");
-
- m_lfButton->setArrowType(Qt::LeftArrow);
- m_lfButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
- m_lfButton->setText(QString::fromLocal8Bit("塔塔"));
- m_lfButton->setIconSize(QSize(48, 48));
- m_lfButton->setStyleSheet("QToolButton{border: none; background: rgb(68, 69, 73); color: rgb(0, 160, 230);}");
-
- m_rgButton->setArrowType(Qt::RightArrow);
- m_rgButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
- m_rgButton->setIconSize(QSize(48, 48));
- m_rgButton->setText(QString::fromLocal8Bit("塔塔"));
- m_rgButton->setStyleSheet("QToolButton{border: none; background: rgb(68, 69, 73); color: rgb(0, 160, 230);}");
-
- hLayout->addWidget(m_upButton);
- hLayout->addWidget(m_doButton);
- hLayout->addWidget(m_lfButton);
- hLayout->addWidget(m_rgButton);
-
- m_ColorButton1 = new QToolButton();
- //QAction* action = m_ColorButton->defaultAction();//默认返回值为NULL
- QAction* action = new QAction(QString::fromLocal8Bit("塔塔"));
- action->setIcon(QIcon(":/Res/Icon/ship.png"));
- action->setToolTip(QString::fromLocal8Bit("加油"));
- m_ColorButton1->setIconSize(QSize(64, 64));
- m_ColorButton1->setDefaultAction(action);
- m_ColorButton1->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
-
- m_ColorButton2 = new QToolButton();
-
- QAction* action2 = new QAction(QString::fromLocal8Bit("塔塔2"));
- action2->setIcon(QIcon(":/Res/Icon/Picture.ico"));
- action2->setToolTip(QString::fromLocal8Bit("加油2"));
-
- QMenu* menu = new QMenu();
- menu->addAction(QIcon(":/Res/Icon/Home.ico"), QString::fromLocal8Bit("主页"));
- menu->addAction(QIcon(":/Res/Icon/Wifi.ico"), QString::fromLocal8Bit("Wifi"));
- menu->addAction(QIcon(":/Res/Icon/Update.ico"), QString::fromLocal8Bit("更新"));
- menu->addAction(QIcon(":/Res/Icon/Folder.ico"), QString::fromLocal8Bit("文件夹"));
- m_ColorButton2->setPopupMode(QToolButton::MenuButtonPopup);
- m_ColorButton2->setIconSize(QSize(64, 64));
- m_ColorButton2->setDefaultAction(action2);
- m_ColorButton2->setMenu(menu);
-
- m_ColorButton2->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
-
- QHBoxLayout* hLayout2 = new QHBoxLayout();
-
- hLayout2->addWidget(m_ColorButton1);
- hLayout2->addWidget(m_ColorButton2);
-
- vLayout->addLayout(hLayout);
- vLayout->addLayout(hLayout2);
-
- setLayout(vLayout);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。