First, I have a QMainWindow with
Qt Code:
  1. setAttribute(Qt::WA_TranslucentBackground);
To copy to clipboard, switch view to plain text mode 
set to make "non-opaque" widgets transparent, as described in the docs. I load a QPixmap using QPixmap::fromImageReader and show it in a QLabel somewhere down my Widget hierarchy. I then have a function that operates on the QPixmap by constructing a new QPixmap from it by drawing only select parts of it with a QPainter. Specifically, I create a new QPixmap by cutting out selected QRect, like this:
Qt Code:
  1. QPixmap remove_from_pixmap(const QPixmap& pm, const QRect& to_remove) {
  2. QPixmap dest(pm.size());
  3. QPainter p(&dest);
  4. if (to_remove.top() > 0)
  5. p.drawPixmap(0, 0, pm, 0, 0, pm.width(), to_remove.top());
  6. // ...
  7. return dest;
To copy to clipboard, switch view to plain text mode 

So far so good, everything works as expected, the cut-out parts show transparent on my QLabel. But now I want to consider the case where the cut-out selection cover a "whole side" of the QPixmap, effectively shrinking it - I want to resize it by cropping any zero-opacity-only columns/rows of pixels. For this I need to find the bounding box by checking which pixels are transparent and which are not .. this is where I am stuck and the docs do not help at all. I also found various threads about similar problems but none of the suggested solutions work.

If I convert to QImage, QImage::format returns QImage::Format_RGB32 and every pixel gives me an alpha value of 255, even if it is transparent. The same if I do
Qt Code:
  1. ...convertToFormat(QImage::Format_ARGB32)
To copy to clipboard, switch view to plain text mode 
or QImage::Format_ARGB32_Premultiplied. I thought maybe I can use QPixmap::mask() since it says it creates a mask from the alpha value, but I cannot find anything about how to get out the bits, besides converting to QImage and then converting to QImage::Format_Mono - but then accessing e.g. ...pixelColor(0,0) gives me out of bounds errors, and I don't know what a Qrgb pixel of a QBitmap is supposed to be in the first place. It is one bit per pixel, how can there be rgb? Is there any way I can get this bit, and does it even represent the alpha value I need?

Or am I going in the wrong direction with QBitmap? There has to be some way, after all when I draw the QLabel, the transparent pixels are actually drawn transparent, so the information exists somewhere. But where and how do I get it?