質問

I was trying to add shadow effect in the qlabel using QGraphicsDropShadowEffect. so i added the effect like this .

QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect(); effect->setBlurRadius(0); effect->setColor(QColor(255, 0, 0)); effect->setOffset(1,1); label1->setGraphicsEffect(effect);

but the effect is looking weird as for other widget like spinbox it is working proper .

this is the image shot of how qlabel looks . enter image description here

the right one is a qspinbox , it works properly for it but for qlable ,it looks odd. it behaves same as for all the labels in the parent .

but if i take the code and make a sample application it works properly ..

the sample app

 QApplication app(argc,argv);
    QMainWindow* window = new QMainWindow();
    QWidget* centralWidget = new QWidget();

    window->setCentralWidget(centralWidget);

    window->setWindowTitle("QLabel With Shadow");
    QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    QLabel* label = new QLabel();
    sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
    label->setSizePolicy(sizePolicy);
    label->setMinimumSize(QSize(0, 0));
    label->setMaximumSize(QSize(16777215, 16777215));
    label->setStyleSheet(QString::fromUtf8("font-size: 12px,0.2em;                          \n"
                                           "font-family: Segoe Regular;  \n"
                                           "color: rgb(0, 0, 0);"));
    label->setText(QApplication::translate("EyGuiProgresColorUi", "Contrast", 0, QApplication::UnicodeUTF8));

    label->setParent(centralWidget);

    QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
    effect->setBlurRadius(0);
    effect->setColor(QColor(255, 0, 0));
    effect->setOffset(1,1);
    label->setGraphicsEffect(effect);

    window->show();

    return app.exec();

this sample application gives me what i require .enter image description here please help me ..

役に立ちましたか?

解決

I know this is quite an old topic. However, there is no answer provided all over the internet, and Qt does not say anything about this unexpected and non-standard behavior. More on that, I had exactly the same issue. Thus, I will provide with a solution which worked for me at least hoping other developers in the future will find this solution.

The problem for me was setting style sheet dynamically on elements without specifying selector. When you do so, and you are adding QGraphicsDropShadowEffect as an effect, then you get this strange behavior. So, in order not to find in this strange situation, I had to ensure that I am setting style sheets in such a way that all selectors are specified.

For instance, if you want to set a color for QLabel (let us say object name is myLabel) widget, then you can do in two different ways:

// Without specifying selector
label->setStyleSheet("color:#000000;");

// By specifying selector
label->setStyleSheet("#myLabel{color:#000000;}");

In both ways, you would have the same result. However, the latter one would not spoil QGraphicsDropShadowEffect effect. I believe author of this question had the same or similar problem.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top