Qt 代码实现抽屉效果的工具盒类。
.h头文件
#ifndef DIALOG_H
#define DIALOG_H
#include <QToolBox>
#include <QToolButton>
class Dialog : public QToolBox
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
QToolButton *toolBtn_1;
QToolButton *toolBtn_2;
QToolButton *toolBtn_3;
QToolButton *toolBtn_4;
QToolButton *toolBtn_5;
QToolButton *toolBtn_6;
QToolButton *toolBtn_7;
QToolButton *toolBtn_8;
};
#endif // DIALOG_H
.cpp源文件
#include "dialog.h"
#include "ui_dialog.h"
#include <QGroupBox>
#include <QVBoxLayout>
#include <QPushButton>
Dialog::Dialog(QWidget *parent) :
QToolBox(parent)
{
toolBtn_1 = new QToolButton;
toolBtn_1->setText(tr("猫"));
toolBtn_1->setAutoRaise(true);
toolBtn_2 = new QToolButton;
toolBtn_2->setText(tr("狗"));
toolBtn_2->setAutoRaise(true);
toolBtn_3 = new QToolButton;
toolBtn_3->setText(tr("猪"));
toolBtn_3->setAutoRaise(true);
QGroupBox *groupAnimal = new QGroupBox;
QVBoxLayout *layout1 = new QVBoxLayout(groupAnimal);
layout1->addWidget(toolBtn_1);
layout1->addWidget(toolBtn_2);
layout1->addWidget(toolBtn_3);
this->addItem((QWidget*)groupAnimal,tr("动物"));
toolBtn_4 = new QToolButton;
toolBtn_4->setText(tr("树"));
toolBtn_4->setAutoRaise(true);
toolBtn_5 = new QToolButton;
toolBtn_5->setText(tr("草"));
toolBtn_5->setAutoRaise(true);
toolBtn_6 = new QToolButton;
toolBtn_6->setText(tr("花"));
toolBtn_6->setAutoRaise(true);
QGroupBox *groupPlant = new QGroupBox;
QVBoxLayout *layout2 = new QVBoxLayout(groupPlant);
layout2->addWidget(toolBtn_4);
layout2->addWidget(toolBtn_5);
layout2->addWidget(toolBtn_6);
this->addItem((QWidget*)groupPlant,tr("植物"));
toolBtn_7 = new QToolButton;
toolBtn_7->setText(tr("鹦鹉"));
toolBtn_7->setAutoRaise(true);
toolBtn_8 = new QToolButton;
toolBtn_8->setText(tr("麻雀"));
toolBtn_8->setAutoRaise(true);
QGroupBox *groupBird = new QGroupBox;
QVBoxLayout *layout3 = new QVBoxLayout(groupBird);
layout3->addWidget(toolBtn_7);
layout3->addWidget(toolBtn_8);
this->addItem((QWidget*)groupBird,tr("鸟类"));
}
Dialog::~Dialog()
{
//delete ui;
}
效果实现:
后续公众号会发布系列教程,更多内容请关注公众号:程序猿学习日记