A
A
Artem2014-08-26 11:14:08
Qt
Artem, 2014-08-26 11:14:08

How to make border in QLabel text?

Guys, who knows how to make a border (enable object outline drawing) for text inside a QLabel widget?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
EXL, 2014-08-26
@Properrr

If you need to highlight the outline of the QLabel itself, then you can simply use Qt Style Sheets (QSS):
vaw5jdE.png

ui->label->setStyleSheet("QLabel {"
                             "border-style: solid;"
                             "border-width: 1px;"
                             "border-color: black; "
                             "}");

If you need to highlight the contour of the text, then Shadow Effect can help:
FEZVbrJ.png
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect(this);
effect->setOffset(-1, -1);
effect->setColor(Qt::yellow);
ui->label->setGraphicsEffect(effect);

Text outline with blur:
vAuWBUn.png
QGraphicsDropShadowEffect *eff = new QGraphicsDropShadowEffect(this);
eff->setOffset(0, 0);
eff->setBlurRadius(5.0);
eff->setColor(Qt::red);
ui->label->setGraphicsEffect(eff);

Stroke text with the overloaded paintEvent() method :
nDejj4M.png
void Widget::paintEvent(QPaintEvent *)
{
    int off = 10;
    QPainter painter(this);
    QPainterPath path;
    QFont drawFont("Sans", 20);
    path.addText(off, drawFont.pointSize() + off, drawFont, text());
    painter.setRenderHints(QPainter::Antialiasing);
    painter.strokePath(path, QPen(QColor("#FF8C00"), 4));
    painter.fillPath(path, QBrush(Qt::black));
    resize(path.boundingRect().size().toSize().width() + off * 2, path.boundingRect().size().toSize().height() + off * 2);
}

A
Artem, 2014-08-26
@Properrr

Yes, it is necessary to select the contour of the text itself. But ShadowEffect, unfortunately, is not quite suitable, since not the entire outline of the characters is selected =(
That's how I would like (as in Qml)
1.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question