PDA

View Full Version : Getting white text with black outline in most/all widgets



anr78
12th July 2011, 22:40
So far I've made my way in the world of Qt by looking at examples and using the excellent docs. I've managed to make all the stuff work, but it doesn't have a wholesome look & feel.

Part of the application is text overlayed on video, and to make that readable in all conditions I wanted white text with a black outline/glow/shadow. Since I didn't find a font-ish way to solve this, I ended up creating a widget where I paint 3 QLabels on top of each other (one white, and two black ones that are slightly offset). This works, but makes it hard getting other widgets with text to look similar. I also use QMessageBox, QMenu and QPushButton, and would like to make the text look similar there.

Anything I have overlooked that can give me text in all widgets with the look I want? Or must I create my own QStyle or maybe override some central function to paint all text this way?

Santosh Reddy
13th July 2011, 04:56
I don't think you can use QStyle for this, you can create all custom widgets (MyLabel, MyMessageBox, MyMenu, MyMenu...) and override the paintEvent() in all of them. In paintEvent() you could do something like this


void MyLabel::paintEvent(QPaintEvent * event)
{
QPainter painter(this);

QFont font("Arial", 48, QFont::Normal, true);
painter.setFont(font);

QRect top_rect = event->rect();
QRect bot_rect = QRect(top_rect.left() + 4
, top_rect.top() + 4
, top_rect.right() + 4
, top_rect.bottom() + 4);

painter.setPen(QPen(Qt::yellow));
painter.drawText(bot_rect, text());
painter.setPen(QPen(Qt::black));
painter.drawText(top_rect, text());
}

anr78
14th July 2011, 21:06
Thanks a lot for the suggestion. Will test it as soon as my vacation is over.

Edit: found the time to do a quick test now. Think I will find a way to make this work, though my test with QMessageBox didn't exactly do what I expected :)

Santosh Reddy
20th July 2011, 08:45
I posted a solution in another post
http://www.qtcentre.org/threads/34111-QPushButton-Outline-white-text-in-black-on-transparent-background

anr78
5th August 2011, 21:04
Thanks! For some reason I do not get my e-mail alerts, so it takes a while before I notice replies :)