QToolBox提供了一种列状的层叠窗体,QToolButton提供了一种快速访问命令或选择项的按钮,通常在工具条中使用。抽屉效果是软件界面设计中的一种常用的形式,可以以一种直观的方式有限放大小的界面上扩展出的更多功能。
下面以QQ抽屉效果为例:
1.当鼠标离开时,按钮自动恢复为弹起的状态
btntool1_2->setAutoRaise(true);
2.ToolButtonStyle的属性主要用来描述文字和图标的显示方式。有5种
Qt::ToolButtonIconOnly; //只显示图标
Qt::ToolButtonTextOnly; //只显示文字
Qt::ToolButtonTextBesideIcon; //文字将显示在图标旁边
Qt::ToolButtonTextUnderIcon; //文字将显示在图标下面
Qt::ToolButtonFollowStyle; // 遵循Style标准
.h文件
#ifndef DRAWER_H
#define DRAWER_H
#include <QWidget>
#include <QToolBox>
#include <QToolButton>
class Drawer : public QToolBox
{
Q_OBJECT
public:
explicit Drawer(QWidget *parent = nullptr);
signals:
private:
QToolButton *btntool1_1;
QToolButton *btntool1_2;
QToolButton *btntool1_3;
QToolButton *btntool2_1;
QToolButton *btntool2_2;
QToolButton *btntool3_1;
QToolButton *btntool3_2;
};
#endif // DRAWER_H
.cpp
#include "drawer.h"
#include <QGroupBox>
#include <QVBoxLayout>
Drawer::Drawer(QWidget *parent) : QToolBox(parent)
{
setWindowTitle(QStringLiteral("My QQ"));
//1.
btntool1_1=new QToolButton;
btntool1_1->setText(QStringLiteral("张三"));
btntool1_1->setIcon(QPixmap(":/new/head/11.jpg"));
btntool1_1->setIconSize(QPixmap(":/new/head/11.jpg").size());
btntool1_1->setAutoRaise(true);
btntool1_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
btntool1_2=new QToolButton;
btntool1_2->setText(QStringLiteral("李四"));
btntool1_2->setIcon(QPixmap(":/new/head/12.jpg"));
btntool1_2->setIconSize(QPixmap(":/new/head/12.jpg").size());
btntool1_2->setAutoRaise(true);
btntool1_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
btntool1_3=new QToolButton;
btntool1_3->setText(QStringLiteral("王二"));
btntool1_3->setIcon(QPixmap(":/new/head/13.jpg"));
btntool1_3->setIconSize(QPixmap(":/new/head/13.jpg").size());
btntool1_3->setAutoRaise(true);
btntool1_3->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
QGroupBox *groupBox1=new QGroupBox;
QVBoxLayout *layout1=new QVBoxLayout(groupBox1);
layout1->setMargin(10);
layout1->setAlignment(Qt::AlignHCenter);
//加入抽屉内的各个按钮
layout1->addWidget(btntool1_1);
layout1->addWidget(btntool1_2);
layout1->addWidget(btntool1_3);
//2.
//插入一个占位符
layout1->addStretch();
btntool2_1=new QToolButton;
btntool2_1->setText(QStringLiteral("小王"));
btntool2_1->setIcon(QPixmap(":/new/head/21.jpg"));
btntool2_1->setIconSize(QPixmap(":/new/head/21.jpg").size());
btntool2_1->setAutoRaise(true);
btntool2_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
layout1->addStretch();
btntool2_2=new QToolButton;
btntool2_2->setText(QStringLiteral("小张"));
//btntool2_2->setIcon(QPixmap("31.jpg"));
//btntool2_2->setIconSize(QPixmap("32.jpg").size());
btntool2_2->setAutoRaise(true);
btntool2_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
QGroupBox *groupBox2=new QGroupBox;
QVBoxLayout *layout2=new QVBoxLayout(groupBox2);
layout2->setMargin(10);
layout2->setAlignment(Qt::AlignHCenter);
//加入抽屉内的各个按钮
layout2->addWidget(btntool2_1);
layout2->addWidget(btntool2_2);
//3.
btntool3_1=new QToolButton;
btntool3_1->setText(QStringLiteral("小陈"));
btntool3_1->setIcon(QPixmap(":/new/head/31.jpg"));
btntool3_1->setIconSize(QPixmap(":/new/head/31.jpg").size());
btntool3_1->setAutoRaise(true);
btntool3_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
layout1->addStretch();
btntool3_2=new QToolButton;
btntool3_2->setText(QStringLiteral("小李"));
btntool3_2->setIcon(QPixmap("32.png"));
// btntool3_2->setIconSize(QPixmap("31.jpg").size());
btntool3_2->setAutoRaise(true);
btntool3_2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
QGroupBox *groupBox3=new QGroupBox;
QVBoxLayout *layout3=new QVBoxLayout(groupBox3);
layout3->setMargin(10);
layout3->setAlignment(Qt::AlignHCenter);
//加入抽屉内的各个按钮
layout3->addWidget(btntool3_1);
layout3->addWidget(btntool3_2);
//将准备好的抽屉插入ToolBox中
this->addItem((QWidget*)groupBox1,QStringLiteral("我的好友"));
this->addItem((QWidget*)groupBox2,QStringLiteral("陌生人"));
this->addItem((QWidget*)groupBox3,QStringLiteral("黑名单"));
QString strbtnleStyle = "color: rgb(50, 50, 50);"
"font-family: Microsoft YaHei;"
"font-size: 16px;"
"font-weight: bold;";
btntool1_1->setStyleSheet(strbtnleStyle);
btntool1_2->setStyleSheet(strbtnleStyle);
btntool1_3->setStyleSheet(strbtnleStyle);
btntool2_1->setStyleSheet(strbtnleStyle);
btntool2_2->setStyleSheet(strbtnleStyle);
btntool3_1->setStyleSheet(strbtnleStyle);
btntool3_2->setStyleSheet(strbtnleStyle);
}
运行结果: