1、自定义Button控件:左边图标 + 右边文本
#ifndef BTNLEFTICONRIGHTTEXT_H
#define BTNLEFTICONRIGHTTEXT_H
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
class CBtnLeftIconRightText : public QPushButton
{
Q_OBJECT
public:
CBtnLeftIconRightText(QWidget *parent) : QPushButton(parent)
{
//创建水平布局器
m_pHBoxLayout = new QHBoxLayout(this);
m_pHBoxLayout->setContentsMargins(8, 2, 10, 2);
m_pHBoxLayout->setSpacing(5);
//创建Icon标签,并插入水平布局器
m_pLabIcon = new QLabel(this);
m_pLabIcon->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
m_pLabIcon->setFixedWidth(24);
m_pHBoxLayout->addWidget(m_pLabIcon);
//创建Text标签,并插入水平布局器
m_pLabText = new QLabel(this);
m_pLabText->setObjectName("pLabBtnRightText");
m_pLabText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_pLabText->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
m_pHBoxLayout->addWidget(m_pLabText);
}
~CBtnLeftIconRightText(){}
void SetText(QString strText)
{
//设置显示文本内容
m_pLabText->setText(strText);
}
void SetBtnIcon(QString strIconNor, QString strIconDis)
{
//设置样式表属性,设置Icon图标(使能打开时显示strIconNor,使能关闭时显示strIconDis)
m_pLabIcon->setStyleSheet(QString("QLabel{border: none;image: url(%1);}QLabel:disabled{border: none;image: url(%2);}").arg(strIconNor).arg(strIconDis));
}
void SetChecked(bool bChecked)
{
//设置该Button是否已选中,设置Icon标签的使能开关(用作Icon切换图标是的判断)
setChecked(bChecked);
m_pLabIcon->setEnabled(bChecked);
}
bool IsEnabled()
{
//获取当前Icon标签的使能开关
return m_pLabIcon->isEnabled();
}
private:
QLabel* m_pLabIcon;
QLabel* m_pLabText;
QHBoxLayout* m_pHBoxLayout;
};
#endif // BTNLEFTICONRIGHTTEXT_H
2、自定义Label控件:文本太长省略 + 提示
#ifndef LABELTOOLTIP_H
#define LABELTOOLTIP_H
#include <QLabel>
class CLabelToolTip : public QLabel
{
Q_OBJECT
public:
CLabelToolTip(QWidget *parent) : QLabel(parent)
{
m_mode = Qt::ElideRight;
}
~CLabelToolTip(){}
void setText(const QString & strText)
{
m_strText = strText;
QFontMetrics elidfont(font());
QString strRetText = elidfont.elidedText(strText, m_mode, width());
if (strRetText != strText)
setToolTip(strText);
else
setToolTip("");
QLabel::setText(strRetText);
}
protected:
void resizeEvent(QResizeEvent *event)
{
setText(m_strText);
}
private:
QString m_strText;
Qt::TextElideMode m_mode;
};
#endif // LABELTOOLTIP_H
效果如下:
3、后续更新。。。