2014-10-03 72 views

回答

0

這可以通過覆蓋子類中標籤的paint事件來完成。例如:

#include <QRect> 
#include <QLabel> 
#include <QPainter> 

class QEngravedLabel : public QLabel 
{ 
public: 

    explicit QEngravedLabel(QWidget *parent=0, Qt::WindowFlags f=0) 
     : QLabel(parent, f){}; 
    explicit QEngravedLabel(const QString &text, QWidget *parent=0, Qt::WindowFlags f=0) 
     : QLabel(text,parent,f){}; 

protected: 

    virtual void paintEvent(QPaintEvent *pe) override 
    { 
     QRect toPaint(pe->rect()); 
     QPainter painter(this); 

     toPaint.translate(0,1); 
     painter.setPen(QColor("#CCC")); // light shadow on bottom 
     painter.drawText(toPaint, this->alignment() ,this->text()); 

     toPaint.translate(0,-2); 
     painter.setPen(QColor("#333")); // dark shadow on top 
     painter.drawText(toPaint, this->alignment() ,this->text()); 

     toPaint.translate(0,1); 
     painter.setPen(QColor("#000000")); // text 
     painter.drawText(toPaint, this->alignment() ,this->text()); 
    } 
}; 

這些陰影顏色是爲淺灰色背景定製的。

2

比覆蓋更容易paintEvent使用QGraphicsEffect,正好QGraphicsDropShadowEffect

QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect(); 
effect->setBlurRadius(5); 
effect->setXOffset(5); 
effect->setYOffset(5); 
label->setGraphicsEffect(effect); 

,結果是這樣的:

enter image description here

如果你要的顏色陰影,你可以很容易地通過QGraphicsDropShadowEffect::setColor成員函數實現這一目標。

希望這會有所幫助。

+0

陰影是一種更容易的方法來完成與我所要求的完全不同的事情。陰影效果使文本看起來像在頁面上方浮動。雕刻的效果使文本看起來沉入頁面,所以此方法不回答問題。 – 2014-10-03 16:36:46

+0

對不起,您要求提供「文字陰影」效果,我並不熟悉「刻有」字樣...... – 2014-10-03 17:25:37