Important: Please read the Qt Code of Conduct -

QImage::setPixelColor vs QImage::setPixel

  • hi,

    till now i used setPixelColor to change some Colors in my image. But since i compiled my code on a different pc with an older version i looked a little further in the function description.

    It was introduced in 5.6 and the interesting part is this:

    Warning: This function is expensive due to the call of the internal detach() function called within; if performance is a concern, we recommend the use of scanLine() or bits() to access pixel data directly.

    So i changed it to void QImage::setPixel on the pc, because this is compatible with the older version of Qt (i think 4.8 is installed).

    So now i wonder what is better? setPixel or setPixelColor. For me both works, but setPixelColor says it is bad for runtime.

  • Lifetime Qt Champion


    Usually you would take the function that does the right thing fast.

  • Both do the job. How can I know what is faster??
    I don't even see the different between the two functions

  • Lifetime Qt Champion

    If you want to know the difference: benchmark.

  • The functions are mostly the same and exist as convenience, so use the one which is most convenient with the data you have. Note that since QImage is implicitly shared, both functions detach if the image is referenced more than once.
    Depending on your use case, you may avoid them altogether, but that is only in case this is performance critical code and you share the image before doing the pixel operations. I would only investigate more if you need major performance improvements.

Log in to reply