2 QPixmap to 1 QPixmap: Problem with invisible pixels



  • Hey guys!

    I'm writing a function to put a QPixmap on an other QPixmap.
    This works fine with this function:

    @void AImage::addPixmap(int x, int y, QPixmap pixmap){
    QImage pix = pixmap.toImage();
    QImage image = tile_image->getPixmap().toImage();

    int w = pix.width();
    int h = pix.height();
    
    float dX = 0;
    float dY = 0;
    
    for(int i = 0; i < image.width(); i++){
        for(int j = 0; j < image.height(); j++){
            if(i >= x && i <= x + w && j >= y && j <= y+h){
                if(QColor(pix.pixel(dX,dY)).){              // I want to check here if the pixel is invisible.. but how?
                    image.setPixel(i,j,pix.pixel(dX,dY));
                }
                dY++;
            }
        }
        if(dY > 0){
            dX++;
        }
        dY = 0;
    }
    
    QPixmap outPut = QPixmap::fromImage(image);
    tile_image->setPixmap(outPut);
    

    }@

    But how can I detect invisible pixels? I have a png file with some invisible pixels and don't want to put them on the background QPixmap.

    I hope you can help me and sorry for my bad English :)



  • I am not an expert on Image convertion but I found this on doc: "Alpha-Blended Drawing":http://qt-project.org/doc/qt-5/qcolor.html#alpha-blended-drawing

    bq. QColor also support alpha-blended outlining and filling. The alpha channel of a color specifies the transparency effect, 0 represents a fully transparent color, while 255 represents a fully opaque color.

    So I think your if-statement should be some thing like:
    @if(QColor(pix.pixel(dX,dY)).alpha() < 255 )@

    Hope it helps! :)



  • Thank you for the help :) but I tried this before. It won't work because every pixel of the image has an alpha of 255.

    I hope we get this problem solved :)



  • That means that the image is all opaque and there is no invisible or transparent pixels: for me invisible is equal to transparent with alpha = 0. Maybe this assumption is incorrect, and is the root-cause of your problem.



  • Yeah but there is a transparent background

    http://s29.postimg.org/5w2ix3eib/shield.png(http://s29.postimg.org/5w2ix3eib/shield.png)

    I uploaded the image. You have to save it then you will see there is a transparent background.

    But thank you for your help :)


  • Moderators

    do you depend on this code? I mean there is a way easier method using QPainter:
    @
    void AImage::addPixmap(int x, int y, const QPixmap & pixmap)
    {
    QPixmap sourcePix = tile_image->getPixmap();

        QPainter p(sourcePix);
             p.drawPixmap( x, y, pixmap );
        p.end();
    
        tile_image->setPixmap(sourcePix);
    

    }
    @


Log in to reply
 

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