Taco de Wolff
Taco de Wolff

Reputation: 1758

Qt text with shadow

I see this in other applications, even though the appearance is ever so slightly, the effect is a much high contrast -> better readability.

The tabs on the left in Qt Designer for example, or the tabs at the top in Adobe Photoshop: the text has some sort of shadow, only ever 1 pixel surrounding the text with a contrasting colour.

Is there a simple way to do this with Qt? Or a more complex one?

Thank you.

Upvotes: 0

Views: 7997

Answers (3)

Stephen Chu
Stephen Chu

Reputation: 12832

Maybe QGraphicsDropShadowEffect?

Upvotes: 5

Uga Buga
Uga Buga

Reputation: 1784

Here is the way I did text shadow on all buttons with Qt5. I am not sure if this is possible with Qt4.

class MyProxyStyle : public QProxyStyle
{
public:

    void drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal, bool enabled, const QString &text, QPalette::ColorRole textRole /* = QPalette::NoRole */) const
    {
        if (textRole == QPalette::ButtonText && dynamic_cast<QAbstractButton*>(painter->device()))
        {
            QPalette palShadow(pal);
            palShadow.setColor(QPalette::ButtonText, QColor(0, 0, 0, 100));
            QProxyStyle::drawItemText(painter, rect.adjusted(1, 1, 1, 1), flags, palShadow, enabled, text, textRole);
        }
        QProxyStyle::drawItemText(painter, rect, flags, pal, enabled, text, textRole);
    }
};

...somewhere in main()

QApplication a;
a.setStyle(new MyProxyStyle);

If you remove the QAbstractButton dynamic_cast the menu titles will also be shadowed which is not always desirable.

Upvotes: 1

JimDaniel
JimDaniel

Reputation: 12703

There are a couple ways of achieving this effect, but conceptually you need to look at it as being two text layers with a slight offset.

I have done this before by re-implementing the paintEvent() method of a QWidget and drawing the text layers myself. Or you can reimplement the drawItemText() method of a custom QStyle. But basically that is how it is done.

Upvotes: 1

Related Questions