slightly more refined solution using the fact that QImage uses "a matrix" internally
QDataStream outStream(&outputFile);
outStream << image.height()<< image.width(); /* You need this to recover the image size, if not needed remove*/
QImage testImage= image.convertToFormat(QImage::Format_Mono,Qt::MonoOnly); /* 1 bit= 1 pixel*/
testImage.invertPixels(); /* black is 1 and white is 0 normally, you need the opposite so invert*/
const int bytesInWidth =
testImage.width()/8
+ (testImage.width()%8>0 ? 1:0);
/*This is image.width()/8 rounded up */
for(int i=0;i<testImage.height();++i)
outStream.writeRawData((const char*)(testImage.constScanLine(i)),bytesInWidth);