函数声明:void QPainter::drawImage ( const QRectF & target, const QImage & image, const QRectF & source, Qt::ImageConversionFlags flags = Qt::AutoColor )
举例:
QRectF target(10.0, 20.0, 80.0, 60.0); //建立目标矩形
QRectF source(0.0, 0.0, 70.0, 40.0); //建立源矩形,用来框定源图像文件中要显示的部分
QImage image(":/images/myImage.png"); //建立QImage类对象image
QPainter painter(this);
painter.drawImage(target, image, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
//(10.0, 20.0, 80.0, 60.0)位置上,大小自适应
1: void ImageWidget::paintEvent(QPaintEvent *event) {
2: QPainter painter(this);
3: QRectF target(0, 0, this->width(), this->height());
4: if(!img.isNull())
5: painter.drawImage(target,img);
6: //painter.drawImage(0,0,img);
7: //img.save("test.bmp");
8: }