Creating a transparent image.



  • I am working on a project that requires me to make an image semi-transparent so that I can then plot a graph on that image.

    Problem: I am unable to make the whole image semi-transparent. The image is displayed using QLabel->setPixmap. I open the image and save it as a QImage and try to manipulate its alpha channel and then convert it to Pixmap.

    Code:

    void Interface::updateImageDataGraph()
    {
        QRgb value;
        QImage original(imageSetPath + "/" + openImageName);
        original.load(imageSetPath + "/" + openImageName);
        original = QImage(previewSize, previewSize, QImage::Format_ARGB32);
        for(int i=0; i<original.height(); i++){
            QRgb *line = (QRgb *)original.scanLine(i);
            for(int j=0; j<original.width(); j++){
                QRgb pixelData = line[j];
                unsigned int red = qRed(pixelData);
                unsigned int green = qGreen(pixelData);
                unsigned int blue = qBlue(pixelData);
                qDebug()<<"RGB at ("<<i<<","<<j<<") is "<<red<<green<<blue<<endl;
                value = qRgba(red, green, blue, 127);
                original.setPixel(i,j, value);
            }
        }
        //original.fill(Qt::transparent);
        imagePixmap.convertFromImage(original);
        //imagePixmap.fill(Qt::transparent);
        imagePixmap = imagePixmap.scaledToHeight(previewSize);
        /*QPainter painter(this);
        painter.begin(&imagePixmap);
    
        painter.setOpacity(0.5);
        painter.end();*/
        imageLabel->setPixmap(imagePixmap);
        
        prevDataSeries->replace(imageDataSeries->pointsVector());
    }
    


  • I would try creating a new QPixmap using fromImage, instead of reusing the original pixmap. Maybe you retain some kind of color information that affects the outcome.



  • @Jay_Ladhad

    When using the setPixel () function in the current code, the x, y coordinate system is being used incorrectly. Change like this:

    original.setPixel(j, i, value);

    And it is simple to using the combination of subclassing QLabel / overloading initPainter() / QPainter::setOpacity() or QGraphicsOpacityEffect.



  • @Devopia53 Thank you so much. I am completely new to this environment and would love to improve the efficiency of my program using the predefined functions.


Log in to reply
 

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