自定义 QLabel

#ifndef QXTLABEL_H
#define QXTLABEL_H

#include <QLabel>
#include <QTime>

class QxtLabel : public QLabel
{
    Q_OBJECT
    Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
    Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
    Q_PROPERTY(Qt::TextElideMode elideMode READ elideMode WRITE setElideMode)

public:
    QxtLabel(QWidget* parent = 0, Qt::WindowFlags flags = 0);
    QxtLabel(const QString& text, QWidget* parent = 0, Qt::WindowFlags flags = 0);
    virtual ~QxtLabel();

    QString text() const;

    Qt::Alignment alignment() const;
    void setAlignment(Qt::Alignment alignment);

    Qt::TextElideMode elideMode() const;
    void setElideMode(Qt::TextElideMode mode);

    virtual QSize sizeHint() const;
    virtual QSize minimumSizeHint() const;

public Q_SLOTS:
    void setText(const QString& text);

Q_SIGNALS:
    void clicked();
    void textChanged(const QString& text);

protected:
    virtual void changeEvent(QEvent* event);
    virtual void mousePressEvent(QMouseEvent* event);
    virtual void mouseReleaseEvent(QMouseEvent* event);
    virtual void paintEvent(QPaintEvent* event);

private:
	QString textReal;
	Qt::Alignment align;
	Qt::TextElideMode elideModeReal;
	QTime time;
};

#endif // QXTLABEL_H


#include "qxtlabel.h"
#include <QTime>
#include <QEvent>
#include <QPainter>
#include <QFontMetrics>
#include <QApplication>

/*!
    \class QxtLabel
    \inmodule QxtGui
    \brief The QxtLabel widget is a label which is able to show elided and rotated plain text.

    QxtLabel is a label which is able to show elided and rotated plain text.
    In addition, QxtLabel provides a signal for clicking.

    \image qxtlabel.png "QxtLabel in action."

    Usage:
    \code
    QxtLabel* label = new QxtLabel(tr("Hello, World!"), this);
    label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    label->setElideMode(Qt::ElideRight);
    label->setRotation(Qxt::CounterClockwise);
    connect(label, SIGNAL(clicked()), this, SLOT(labelClicked()));
    \endcode

    \sa QLabel
 */

/*!
    \fn QxtLabel::clicked()

    This signal is emitted whenever the label has been clicked.

    \bold {Note:} A combination of mouse button press and release in shorter
    time than QApplication::doubleClickInterval is considered
    as a click.

    \sa QApplication::doubleClickInterval
 */

/*!
    \fn QxtLabel::textChanged(const QString& text)

    This signal is emitted whenever the \a text has changed.
 */

/*!
    Constructs a new QxtLabel with \a parent and \a flags.
 */
QxtLabel::QxtLabel(QWidget* parent, Qt::WindowFlags flags) : QLabel(parent, flags)
{
	elideModeReal = Qt::ElideMiddle;
}

/*!
    Constructs a new QxtLabel with \a text, \a parent and \a flags.
 */
QxtLabel::QxtLabel(const QString& text, QWidget* parent, Qt::WindowFlags flags) : QLabel(parent, flags)
{
	textReal = text;
	elideModeReal = Qt::ElideMiddle;
}

/*!
    Destructs the label.
 */
QxtLabel::~QxtLabel()
{}

/*!
    \property QxtLabel::text
    \brief the text of the label
 */
QString QxtLabel::text() const
{
    return textReal;
}

void QxtLabel::setText(const QString& text)
{
    if (textReal != text)
    {
        textReal = text;
        // qxt_d().updateLabel();
        emit textChanged(text);
		update();
    }
}

/*!
    \property QxtLabel::alignment
    \brief the alignment of the text

    The text is aligned according to this property.
    The default value is Qt::AlignCenter.

    \sa text, Qt::Alignment
 */
Qt::Alignment QxtLabel::alignment() const
{
    return align;
}

void QxtLabel::setAlignment(Qt::Alignment alignment)
{
    if (align != alignment)
    {
        align = alignment;
        update(); // no geometry change, repaint is sufficient
    }
}

/*!
    \property QxtLabel::elideMode
    \brief the elide mode of the text

    The text is elided according to this property.
    The default value is Qt::ElideMiddle.

    \sa text, Qt::TextElideMode
 */
Qt::TextElideMode QxtLabel::elideMode() const
{
    return elideModeReal;
}

void QxtLabel::setElideMode(Qt::TextElideMode mode)
{
    if (elideModeReal != mode)
    {
        elideModeReal = mode;
        //qxt_d().updateLabel();
    }
}

/*!
    \reimp
 */
QSize QxtLabel::sizeHint() const
{
    const QFontMetrics& fm = fontMetrics();
    QSize size(fm.width(textReal), fm.height());
    return size;
}

/*!
    \reimp
 */
QSize QxtLabel::minimumSizeHint() const
{
    switch (elideModeReal)
    {
    case Qt::ElideNone:
        return sizeHint();
    default:
    {
        const QFontMetrics& fm = fontMetrics();
        QSize size(fm.width("..."), fm.height());
        return size;
    }
    }
}

/*!
    \reimp
 */
void QxtLabel::paintEvent(QPaintEvent* event)
{
    QLabel::paintEvent(event);
    QPainter p(this);
    QRect r = contentsRect();

    const QString elidedText = fontMetrics().elidedText(textReal, elideModeReal, r.width());
    p.drawText(r, elidedText);
}

/*!
    \reimp
 */
void QxtLabel::changeEvent(QEvent* event)
{
    QLabel::changeEvent(event);
    switch (event->type())
    {
    case QEvent::FontChange:
    case QEvent::ApplicationFontChange:
        //qxt_d().updateLabel();
        break;
    default:
        // nothing to do
        break;
    }
}

/*!
    \reimp
 */
void QxtLabel::mousePressEvent(QMouseEvent* event)
{
    QFrame::mousePressEvent(event);
    time.start();
}

/*!
    \reimp
 */
void QxtLabel::mouseReleaseEvent(QMouseEvent* event)
{
    QFrame::mouseReleaseEvent(event);
    if (time.elapsed() < qApp->doubleClickInterval())
        emit clicked();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过继承 QLabel 类来实现自定义 QLabel,然后可以通过以下方法设置背景颜色、文字颜色和滚动效果。 设置背景颜色: 可以通过 `setStyleSheet` 方法设置样式表来实现设置背景颜色,示例代码如下: ```cpp class MyLabel : public QLabel { public: MyLabel(QWidget* parent = nullptr); private: void init(); }; MyLabel::MyLabel(QWidget* parent) : QLabel(parent) { init(); } void MyLabel::init() { setStyleSheet("background-color: red;"); } ``` 设置文字颜色: 可以通过 `setStyleSheet` 方法设置样式表来实现设置文字颜色,示例代码如下: ```cpp class MyLabel : public QLabel { public: MyLabel(QWidget* parent = nullptr); private: void init(); }; MyLabel::MyLabel(QWidget* parent) : QLabel(parent) { init(); } void MyLabel::init() { setStyleSheet("color: blue;"); } ``` 设置文字滚动: 可以通过继承 `QLabel` 类,并重写 `paintEvent` 方法来实现文字滚动效果,示例代码如下: ```cpp class MyLabel : public QLabel { public: MyLabel(QWidget* parent = nullptr); private: void init(); void paintEvent(QPaintEvent *ev) override; QTimer* timer; int pos = 0; }; MyLabel::MyLabel(QWidget* parent) : QLabel(parent) { init(); } void MyLabel::init() { setText("Hello World!"); timer = new QTimer(this); connect(timer, &QTimer::timeout, [this](){ if (pos == width()) { pos = -fontMetrics().width(text()); } else { pos++; } update(); }); timer->start(10); } void MyLabel::paintEvent(QPaintEvent *ev) { QPainter painter(this); painter.drawText(pos, 0, text()); } ``` 以上代码通过重写 `paintEvent` 方法来实现文字滚动效果,每隔一定时间更新 `pos` 变量的值,然后重新绘制 `QLabel` 即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值