以下两个方式都可以

方式一

QImage * greyScale(QImage * origin){
QImage * newImage = new QImage(origin->width(), origin->height(), QImage::Format_ARGB32);
QRgb * line;
for(int y = 0; yheight(); y++){
    QRgb * line = (QRgb *)origin->scanLine(y);

    for(int x = 0; xwidth(); x++){
        int average = (qRed(line[x]) + qGreen(line[x]) + qRed(line[x]))/3;
        newImage->setPixel(x,y, qRgb(average, average, average));
    }
}
return newImage;

}

方式二

QImage * greyScale2(QImage * origin){
QImage * newImage = new QImage(origin->width(), origin->height(), QImage::Format_ARGB32);
QColor oldColor;
for(int x = 0; xwidth(); x++){
    for(int y = 0; yheight(); y++){
        oldColor = QColor(origin->pixel(x,y));
        int average = (oldColor.red()+oldColor.green()+oldColor.blue())/3;
        newImage->setPixel(x,y,qRgb(average,average,average));
    }
}
return newImage;

}