1、正常情况下,如果宽度足够:
QLabel *textLabel = new QLabel(this);
textLabel->setStyleSheet("background-color:red");
textLabel->setAlignment(Qt::AlignCenter);
textLabel->setFixedWidth(500);
QString mStr = "hello world";
textLabel->setText(mStr);
2、控件宽度不够(把宽度改为100),不处理:
如图,无法显示完全。
3、处理之后:
代码:
QLabel *textLabel = new QLabel(this);
textLabel->setStyleSheet("background-color:red");
textLabel->setAlignment(Qt::AlignCenter);
textLabel->setFixedWidth(100);
QString mStr = "hello world";
textLabel->setText(mStr);
QFontMetrics fontMetrics(textLabel->font());
int fontSize = fontMetrics.width(mStr);
if (fontSize > textLabel->width()) {
textLabel->setText(fontMetrics.elidedText(mStr, Qt::ElideRight, textLabel->width()));
textLabel->setToolTip(mStr);
} else {
textLabel->setText(mStr);
textLabel->setToolTip("");
}
当然,我们也可以改为省略左边的内容(Qt::ElideLeft):
又或是省略中间的内容(Qt::ElideMiddle):
文字过长自动省略号的Label:
.h:
#ifndef FIXLABEL_H
#define FIXLABEL_H
#include <QLabel>
//文本长自动省略并添加悬浮
class FixLabel : public QLabel
{
Q_OBJECT
public:
explicit FixLabel(QWidget *parent = nullptr);
explicit FixLabel(QString text , QWidget *parent = nullptr);
~FixLabel();
void setText(const QString &text, bool saveTextFlag = true);
protected:
void paintEvent(QPaintEvent *event);
private:
QString mStr;
};
#endif // FIXLABEL_H
.cpp:
#include "fixlabel.h"
#include <QFontMetrics>
#include <QDebug>
FixLabel::FixLabel(QWidget *parent):
QLabel(parent)
{
}
FixLabel::FixLabel(QString text , QWidget *parent):
QLabel(parent)
{
this->setText(text);
}
FixLabel::~FixLabel()
{
}
void FixLabel::paintEvent(QPaintEvent *event)
{
QFontMetrics fontMetrics(this->font());
int fontSize = fontMetrics.width(mStr);
if (fontSize > this->width()) {
this->setText(fontMetrics.elidedText(mStr, Qt::ElideRight, this->width()), false);
this->setToolTip(mStr);
} else {
this->setText(mStr, false);
this->setToolTip("");
}
QLabel::paintEvent(event);
}
void FixLabel::setText(const QString & text, bool saveTextFlag)
{
if (saveTextFlag)
mStr = text;
QLabel::setText(text);
}
文字过长自动省略号的button:
#ifndef _FIXBUTTON_H_
#define _FIXBUTTON_H_
#include <QPushButton>
class FixButton : public QPushButton
{
Q_OBJECT
public:
FixButton(QWidget *parent = nullptr);
~FixButton();
void setText(const QString &text, bool saveTextFlag = true);
private:
void paintEvent(QPaintEvent *event);
private:
QString mStr;
};
#endif
#include "fixbutton.h"
#include <QFontMetrics>
#include <QDebug>
FixButton::FixButton(QWidget *parent):
QPushButton(parent)
{
}
FixButton::~FixButton()
{
}
void FixButton::paintEvent(QPaintEvent *event)
{
QFontMetrics fontMetrics(this->font());
int fontSize = fontMetrics.width(mStr);
int showTextSize = this->width() - this->contentsMargins().left() - this->contentsMargins().right();
if (fontSize > showTextSize) {
this->setText(fontMetrics.elidedText(mStr, Qt::ElideRight, showTextSize), false);
this->setToolTip(mStr);
} else {
this->setText(mStr, false);
this->setToolTip("");
}
QPushButton::paintEvent(event);
}
void FixButton::setText(const QString & text, bool saveTextFlag)
{
if (saveTextFlag)
mStr = text;
QPushButton::setText(text);
}