Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Create overlay that grayscales widget behind



  • Hello I need some help with something I'm trying to do.

    I've got an image that has some color and that is inacessible for an overlay class that should create an overlay that would grayscale the image (to show it's deactived).

    All I've got now is an overlay widget that holds a QPainter rectangle that has a certain opacity and is black, as such :

          QPainter p{this};
          p.fillRect(rect(), {0, 0, 0, 200});
    

    But the colors of the image behind are still visible obviously as all it does is darkening the image widget.

    I am on Qt 5.12.5, c++, windows 10.

    I have seen some solutions with QML but I am sadly not using QML. Any directions or ideas would be great :-)



  • If the image is shown in a single widget (like a QLabel), instead of an overlay (I have no solution for that), you can use QWidget::setGraphicsEffect() + QGraphicsColorizeEffect

    QGraphicsColorizeEffect* effect = new QGraphicsColorizeEffect(this);
    effect->setColor(Qt::gray);
    ui->label->setGraphicsEffect(effect);
    

    P.S. There's a note in the doc:

    Note: Graphics effects are not supported for OpenGL-based widgets, such as QGLWidget, QOpenGLWidget and QQuickWidget.

    Since you don't use QML, I think maybe that'll be fine.


Log in to reply