环境:win10,vs2017,qt5.9.7。
通常我们使用toolbar时,添加一个按钮toolbutton,会以文字或图标形式显示,但是如果想要以图标加文字显示,就需要用到Qt::ToolButtonStyle属性。
Constant | Description | 描述 |
Qt::ToolButtonIconOnly | Only display the icon. | 只显示图标 |
Qt::ToolButtonTextOnly | Only display the text. | 只显示文字 |
Qt::ToolButtonTextBesideIcon | The text appears beside the icon. | 文字在图标旁 |
Qt::ToolButtonTextUnderIcon | The text appears under the icon. | 文字在图标下 |
Qt::ToolButtonFollowStyle | Follow the style. | 根据QStyle::StyleHint格式显示 |
上表显示了几种用法,下面主要介绍Qt::ToolButtonTextBesideIcon和Qt::ToolButtonTextUnderIcon两种。
StopPosDevAction = new QAction(QIcon((":/ico/ico/restart_64.png")),g_C2Q("停止定位"),this);
connect(StopPosDevAction,SIGNAL(triggered()),this,SLOT(StopPosDev()));
ui->mainToolBar->addAction(StopPosDevAction);
ui->mainToolBar->setIconSize(QSize(30,30));
ui->mainToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
先是Qt::ToolButtonTextBesideIcon,由于在主界面Mainwindow中使用,所以用的mainToolBar,显示创建动作StopPosDevAction,然后connect到槽函数StopPosDev,将动作添加到mainToolBar,再设置按钮样式,效果如下。
StopPosDevAction = new QAction(QIcon((":/ico/ico/restart_64.png")),g_C2Q("停止定位"),this);
connect(StopPosDevAction,SIGNAL(triggered()),this,SLOT(StopPosDev()));
ui->mainToolBar->addAction(StopPosDevAction);
ui->mainToolBar->setIconSize(QSize(30,30));
ui->mainToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
然后是文字在下方的样式,基本一致,效果如下。