Transparency effect between image and drawing



  • I want to give a transparency effect to an image, so the underlying drawing could be seen through. Has anybody used this king of effect, often visible in Windows desktop, with Qt ?
    I'm grateful for any indication.


  • Moderators

    @Furkas Hi! Are we talking about QWidgets or QtQuick? Do you want to make a whole window transparent or just an object within a window?



  • @Wieland,
    I'm talking about QWidgets, and I want just one transparent object, I think a QLabel may be cool.


  • Moderators

    see QGraphicsOpacityEffect class in case you want to make the whole widget transparent
    Its also possible to add a linear opacity (gradient-like), but there is more work involved.



  • @raven-worx Thanks for your advice. It seems to be the right tool. But I have troubles with this class. I get an exception "Read acces violation" any time I try to call setGraphicsEffect() with this effect on a widget in my UI. I have not found in the manual what is the restriction.
    It's ok if I add a QLabel outside of the UI.


  • Moderators

    hard to tell ... you're probably trying to accessing a uninitialized/deleted variable.
    show some code pls.



  • @raven-worx Here the significant code :
    #include <QMessageBox>
    #include <QStandardPaths>
    #include <QFileDialog>
    #include <QImageReader>
    #include <QTimer>
    #include <QtWidgets>

    #include "radararea.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    imageLabel = new QLabel;
    imageLabel->setBackgroundRole(QPalette::Base);
    imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    imageLabel->setScaledContents(true);

    effect = new QGraphicsOpacityEffect;
    effect->setOpacity(0.6);
    
    //ui->imaLabel->setGraphicsEffect(effect); // Nok
    //ui->spinBoxGain->setGraphicsEffect(effect); // Nok
    //ui->pushButtonACAuto->setGraphicsEffect(effect); // Nok
    //imageLabel->setGraphicsEffect(effect);  // ok
    
    ui->setupUi(this);
    
    //ui->imaLabel->setGraphicsEffect(effect); // Nok
    //ui->spinBoxGain->setGraphicsEffect(effect); // Nok
    //ui->pushButtonACAuto->setGraphicsEffect(effect); // Nok
    //imageLabel->setGraphicsEffect(effect);  // ok
    
    ui->comboBoxAntType->addItem(tr("Antenne F1"));
    ui->comboBoxAntType->addItem(tr("Antenne F2"));
    ui->comboBoxAntType->addItem(tr("Antenne F3"));
    
    connect(ui->comboBoxAntType, SIGNAL(currentIndexChanged(int)), SLOT(on_ComboChanged(int)));
    

    ......
    I think UI is not available before the call to setupUI(), but it's the same after.


  • Moderators

    yes indeed as you said, dont access any member of the ui variable before calling setupUi() on it.
    Also - as the docs of QWidget::setGraphicsEffect() are clearly stating - you cannot reuse a graphicseffect for multiple widgets.



  • @raven-worx I noted. So, I tried one after another.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.