工具盒类即为QToolBox。它提供了一种列状的层叠窗体,而QToolButton提供了一种快速访问命令或选择项的按钮,通常在工具条中使用。
抽屉效果是软件界面设计中的一种常用形式,可以以一种动态直观的方式在大小有限的界面上扩展出更多的功能。
下面我们以代码形式来介绍具体用法:
头文件内容为:
#ifndef DRAWER_H
#define DRAWER_H
#include<QToolBox>
#include<QToolButton>
class Drawer : public QToolBox
{
public:
Drawer();
private:
QToolButton *toolBtn11;
QToolButton *toolBtn12;
QToolButton *toolBtn13;
QToolButton *toolBtn14;
QToolButton *toolBtn15;
QToolButton *toolBtn21;
QToolButton *toolBtn22;
QToolButton *toolBtn31;
QToolButton *toolBtn32;
};
#endif // DRAWER_H
cpp文件内容为:
#include "drawer.h"
#include<QGroupBox>
#include<QVBoxLayout>
Drawer::Drawer()
{
setWindowTitle(tr("My QQ"));//设置窗口的标题
toolBtn11=new QToolButton;
toolBtn11->setText(tr("小明"));//设置按钮名称
toolBtn11->setIcon(QPixmap("1.png"));//头像
toolBtn11->setIconSize(QPixmap("1.png").size());//头像大小
toolBtn11->setAutoRaise(true);
toolBtn11->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolBtn12=new QToolButton;
toolBtn12->setText(tr("大花猫"));
toolBtn12->setIcon(QPixmap("2.png"));
toolBtn12->setIconSize(QPixmap("2.png").size());
toolBtn12->setAutoRaise(true);
toolBtn12->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolBtn13=new QToolButton;
toolBtn13->setText(tr("丽丽"));
toolBtn13->setIcon(QPixmap("3.png"));
toolBtn13->setIconSize(QPixmap("3.png").size());
toolBtn13->setAutoRaise(true);
toolBtn13->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolBtn14=new QToolButton;
toolBtn14->setText(tr("赵四"));
toolBtn14->setIcon(QPixmap("4.png"));
toolBtn14->setIconSize(QPixmap("4.png").size());
toolBtn14->setAutoRaise(true);
toolBtn14->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolBtn15=new QToolButton;
toolBtn15->setText(tr("孙点"));
toolBtn15->setIcon(QPixmap("5.png"));
toolBtn15->setIconSize(QPixmap("5.png").size());
toolBtn15->setAutoRaise(true);
toolBtn15->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
QGroupBox *groupBox1=new QGroupBox;//创建抽屉
QVBoxLayout *layout1=new QVBoxLayout(groupBox1);//创建抽屉内各个按钮的布局
layout1->setMargin(10);
layout1->setAlignment(Qt::AlignCenter);
layout1->addWidget(toolBtn11);
layout1->addWidget(toolBtn12);
layout1->addWidget(toolBtn13);
layout1->addWidget(toolBtn14);
layout1->addWidget(toolBtn15);
layout1->addStretch();//在按钮之后插入一个占位符,是的所有按钮能够靠上对齐,并且在整个抽屉大小发生改变时保证按钮的大小不发生变化
toolBtn21=new QToolButton;
toolBtn21->setText(tr("王五"));
toolBtn21->setIcon(QPixmap("6.png"));
toolBtn21->setIconSize(QPixmap("6.png").size());
toolBtn21->setAutoRaise(true);
toolBtn21->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolBtn22=new QToolButton;
toolBtn22->setText(tr("张三"));
toolBtn22->setIcon(QPixmap("7.png"));
toolBtn22->setIconSize(QPixmap("7.png").size());
toolBtn22->setAutoRaise(true);
toolBtn22->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
QGroupBox *groupBox2=new QGroupBox;//创建抽屉
QVBoxLayout *layout2=new QVBoxLayout(groupBox2);//创建抽屉内各个按钮的布局
layout2->setMargin(10);
layout2->setAlignment(Qt::AlignCenter);
layout2->addWidget(toolBtn21);
layout2->addWidget(toolBtn22);
toolBtn31=new QToolButton;
toolBtn31->setText(tr("陈热"));
toolBtn31->setIcon(QPixmap("8.png"));
toolBtn31->setIconSize(QPixmap("8.png").size());
toolBtn31->setAutoRaise(true);
toolBtn31->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolBtn32=new QToolButton;
toolBtn32->setText(tr("小李"));
toolBtn32->setIcon(QPixmap("9.png"));
toolBtn32->setIconSize(QPixmap("9.png").size());
toolBtn32->setAutoRaise(true);
toolBtn32->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
QGroupBox *groupBox3=new QGroupBox;//创建抽屉
QVBoxLayout *layout3=new QVBoxLayout(groupBox3);//创建抽屉内各个按钮的布局
layout3->setMargin(10);
layout3->setAlignment(Qt::AlignCenter);
layout3->addWidget(toolBtn31);
layout3->addWidget(toolBtn32);
this->addItem((QWidget*)groupBox1,tr("我的好友"));
this->addItem((QWidget*)groupBox2,tr("陌生人"));
this->addItem((QWidget*)groupBox3,tr("黑名单"));
}
文中的图片放在build-MyQQExample-Desktop_Qt_5_9_3_MinGW_32bit-Debug目录下即可
运行后展现的效果为: