mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//ui->showimg->setGeometry(0,500,500,500);
bb = new showImage(this);//依赖于主窗口,也就是说传进来的参数this
bb1 = new showImage(this);//同样的
bb->setGeometry(0,0,500,500);//相对于主窗口坐标及大小
bb1->setGeometry(500,0,500,500);
bb->show();//显示
bb1->show();
}
MainWindow::~MainWindow()
{
delete ui;
delete bb;
delete bb1;
}
void MainWindow:: paintEvent(QPaintEvent *event)
{
//QImage image;
//image.load("bb.jpg"); //用QImage导入图片
//QPainter draw(this); //创建QPainter,将指针传入该设备
//QRect rect(0,0,this->width(),this->height()); //创建绘图区域
//draw.drawImage(rect,image); //绘图,drawImage有很多重载函数,可根据自己的需要调用相应函数
}
//show.h
#ifndef SHOW_H
#define SHOW_H
#include <QImage>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
class showImage:public QWidget
{
public:
showImage(QWidget *parent = 0);
~showImage();
QImage show1;
void paintEvent(QPaintEvent *e);
};
#endif // SHOW_H
//show.cpp
#include "show.h"
showImage::showImage(QWidget *parent):QWidget(parent)
{
show1.load("C:\\Users\\zoule\\Desktop\\TestUnit\\hh.jpg");//文件路径
}
showImage::~showImage()
{
}
void showImage::paintEvent(QPaintEvent *e)
{
QPainter pp(this);
QRect Temp(0,0,this->width(),this->height());
pp.drawImage(Temp,show1);
}
结果: