Qt基于字体库的提示控件

引言

之前在另一篇博文《Qt利用字体库实现图标的动态换肤》已经完成了字库的引入,实现两个基础的图标控件类IconButton和IconLabel,可以覆盖多数的使用场景,但随着重构的铺开还需要对其他频繁使用的控件进行封装,当然这些都是基于原有的基础上进行封装。

在这里插入图片描述
如上图所示,通过图标和文字的搭配进行提示展示,按照布局方式不同可分为四种,代码实现也是通过调整布局,较为简单。由于存在多个构造函数,为简化代码使用C++11的委托构造函数,也就是同一个类中一个构造函数可以调用另外一个构造函数。

完整代码

class IconTipsLabel : public QWidget
{
    Q_OBJECT

public:
    enum IconLayoutStyle {
        ILS_TOP,
        ILS_BOTTOM,
        ILS_LEFT,
        ILS_RIGHT,
    };
    explicit IconTipsLabel(int iconIndex, IconLayoutStyle layoutStyle, QWidget *parent = nullptr);
    explicit IconTipsLabel(int iconIndex, QWidget *parent = nullptr);
    explicit IconTipsLabel(IconLayoutStyle layoutStyle, QWidget *parent = nullptr);

    void setIconSize(int size);
    void setIconObjName(QString str);
    void setTips(QString str);
    void setTipsObjName(QString str);

    QHBoxLayout* bottomLayout() { return m_bottomLayout; }

    void setIconIndex(int iconIndex);

private:
    IconLabel* m_iconLabel;
    QLabel* m_tipsLabel;
    QHBoxLayout* m_bottomLayout;
};
IconTipsLabel::IconTipsLabel(int iconIndex, IconTipsLabel::IconLayoutStyle layoutStyle, QWidget *parent)
    : QWidget(parent)
{
    m_iconLabel = new IconLabel(iconIndex, this);
    m_iconLabel->setMinimumSize(40, 40);

    m_tipsLabel = new QLabel(this);

    m_bottomLayout = new QHBoxLayout;

    QLayout* contextLyaout;

    switch (layoutStyle) {
    case ILS_TOP:
    case ILS_BOTTOM:
        contextLyaout = new QVBoxLayout;
        if (layoutStyle == ILS_BOTTOM) {
            contextLyaout->addWidget(m_tipsLabel);
            contextLyaout->addWidget(m_iconLabel);
        }
        else {
            contextLyaout->addWidget(m_iconLabel);
            contextLyaout->addWidget(m_tipsLabel);
        }
        contextLyaout->setAlignment(m_iconLabel, Qt::AlignHCenter);
        contextLyaout->setAlignment(m_tipsLabel, Qt::AlignHCenter);
        break;
    case ILS_LEFT:
        contextLyaout = new QHBoxLayout;
        contextLyaout->addWidget(m_iconLabel);
        contextLyaout->addWidget(m_tipsLabel);
        break;
    case ILS_RIGHT:
        contextLyaout = new QHBoxLayout;
        contextLyaout->addWidget(m_tipsLabel);
        contextLyaout->addWidget(m_iconLabel);
        break;
    }

    auto mainLayout = new QVBoxLayout(this);
    mainLayout->addStretch();
    mainLayout->addLayout(contextLyaout);
    mainLayout->addLayout(m_bottomLayout);
    mainLayout->addStretch();

    mainLayout->setAlignment(contextLyaout, Qt::AlignHCenter);
}

IconTipsLabel::IconTipsLabel(int iconIndex, QWidget *parent)
    : IconTipsLabel(iconIndex, ILS_TOP, parent)
{

}

IconTipsLabel::IconTipsLabel(IconLayoutStyle layoutStyle, QWidget *parent)
    : IconTipsLabel(IconFont::ICON_CAMERA, layoutStyle, parent)
{
    m_iconLabel->hide();
}

void IconTipsLabel::setIconSize(int size)
{
    m_iconLabel->setFixedSize(size, size);
}

void IconTipsLabel::setIconObjName(QString str)
{
    m_iconLabel->setObjectName(str);
}

void IconTipsLabel::setTips(QString str)
{
    m_tipsLabel->setText(str);
}

void IconTipsLabel::setTipsObjName(QString str)
{
    m_tipsLabel->setObjectName(str);
}

void IconTipsLabel::setIconIndex(int iconIndex)
{
    m_iconLabel->setIcon(iconIndex);
    m_iconLabel->show();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Arui丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值