QT 使用 Tool Box时, 发现只能展开一个不符合需求。 我想实现类似效果抽屉控件,动态创建添加,而且可以同时展开多个。找到了实现的代码,修改一下放在项目里使用,效果如下:
简单使用代码:
//创建抽屉盒子对象
LockerWidget* lockerWidget = new LockerWidget();
//准备好抽屉的名字集合、widget集合
QStringList names;
names << "抽屉一" << "抽屉二" << "抽屉三" << "抽屉四";
ChannelMapping* channelMapping = new ChannelMapping();
MediaEffect* mediaEffect = new MediaEffect();
ProgramBaseAttr* programBaseAttr = new ProgramBaseAttr();
ProgramWinAttr* programWinAttr = new ProgramWinAttr();
QList<QWidget*> widgets;
widgets << channelMapping << mediaEffect << programBaseAttr << programWinAttr;
//向抽屉盒子中添加抽屉
lockerWidget->add_locker(names,widgets);
ui->scrollArea->setWidget(lockerWidget);
lockerwidget.h
#ifndef LOCKERWIDGET_H
#define LOCKERWIDGET_H
#include <QWidget>
#include <QToolButton>
#include <QLabel>
#include <QPushButton>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QDebug>
#include <QPixmap>
#include <QHBoxLayout>
#define OPEN_ICON ":/image/boxopen.png"
#define CLOSE_ICON ":/image/boxclose.png"
class LockerButton : public QPushButton
{
Q_OBJECT
public:
explicit LockerButton(QWidget* parent = nullptr);
void SetImageLabel(const QPixmap &pixmap);
void SetTextLabel(QString text);
QLabel* GetImageHandle();
QLabel* GetTextH