根据这个博主的内容进行修改的。https://liang.blog.csdn.net/article/details/80295663
原文中用的Widget 作为画布,我修改为了用QScrollArea中的label作为画布,将图片导入到label中,在label 上进行画画等操作。我画的线一直没在label中的图片里显示。请问怎么修改。改变之后,空白页可以使用画笔吗,但是坐标不一致,画笔的线和图画相差了很远
void markingPapers::paintEvent(QPaintEvent *)
{
if(m_openimg == 0)//不是打开图片的,每一次新建一个空白的画布
{
m_pixmap = QPixmap(size());//新建pixmap
m_pixmap.fill(Qt::white);//背景色填充为白色
}
QPixmap pix = m_pixmap;//以m_pixmap作为画布
QPainter p(&pix);//将m_pixmap作为画布
unsigned int i1=0,i2=0,i3=0,i4=0,i5=0;//各种图形的索引
for(int c = 0;c<m_shape.size();++c)//控制用户当前所绘图形总数
{
if(m_shape.at(c) == 1)//线条
{
const QVector<QPoint>& line = m_lines.at(i1++);//取出一条线条
for(int j=0; j<line.size()-1; ++j)//将线条的所有线段描绘出
{
p.drawLine(line.at(j), line.at(j+1));
}
}
else if(m_shape.at(c) == 2)//矩形
{
p.drawRect(m_rects.at(i2++));
}
else if(m_shape.at(c) == 3)//椭圆
{
p.drawEllipse(m_oval.at(i3++));
}
else if(m_shape.at(c) == 4)//直线
{
p.drawLine(m_strline.at(i4).topLeft(),m_strline.at(i4).bottomRight());
i4++;
}
else if(m_shape.at(c) == 5)//文本
{
p.drawText(m_textsite.at(i5),m_text.at(i5));
i5++;
}
}
p.end();
p.begin(ui->lb_paper);//将当前窗体作为画布
p.drawPixmap(0,0, pix);//将pixmap画到窗体
// ui->lb_paper->setPixmap(pix);//这行一定要有,不然不会实时更新到窗口
}
void markingPapers::setImg(paper papers)
{ ui->lb_paperName->setText(QString("%1").arg(papers.papername));
// ui->lb_total->setText(QString("学生总数数量:%1").arg(total));
// qDebug()<<papers.papername;
QString OpenFile = papers.paperpath;
QPixmap pixmap;
// QString picPath = QFileDialog::getOpenFileName(this,tr("打开"),"","Image Files(*.jpg *.png)");
if(!OpenFile.isEmpty())//用户选择了文件
{
// QPixmap pix;
// pix.load(OpenFile);//加载图片.
// QPainter p(&m_pixmap);
// p.drawPixmap(0,30,pix);//添加工具栏的空间
// m_openimg = 1;//设置文件打开标志
// update();//触发窗体重绘,将图片画到窗体
if(pixmap.load(OpenFile))
{
ui->lb_paper->setPixmap(OpenFile);
// QPixmap pix;
// pix.load(OpenFile);
// QPainter p(&pix);
// p.drawPixmap(ui->lb_paper->x(),ui->lb_paper->y(),pixmap);
// m_openimg = 1;
// update();
}
}
void markingPapers::mouseMoveEvent(QMouseEvent *e)
{
if(m_pull && ((m_type == 2 && m_rects.last().contains(e->pos()))
|| (m_type == 3 && m_oval.last().contains(e->pos()) )))
{
setCursor(Qt::SizeAllCursor);//拖拽模式下,并且在拖拽图形中,将光标形状改为十字
}
else
{
setCursor(Qt::ArrowCursor);//恢复原始光标形状
m_pull = 0;
}
if(m_leftpress)
{
if(m_type == 1)//铅笔画线
{
if(m_lines.size()<=0) return;//线条集合为空,不画线
QVector<QPoint>& lastLine = m_lines.last();//最后添加的线条,就是最新画的
lastLine.append(e->pos());//记录鼠标的坐标(线条的轨迹)
update();//触发窗体重绘
}
else if(m_type == 2)
{
if(m_pull == 0)//非拖拽
{
QRect& lastRect = m_rects.last();//拿到新矩形
lastRect.setBottomRight(e->pos());//更新矩形的右下角坐标
}
else//拖拽模式
{
QRect& lastRect = m_rects.last();//拿到最后添加的矩形
if(lastRect.contains(e->pos()))//在矩形的内部
{
int dx = e->pos().x()-m_begin.x();//横向移动x
int dy = e->pos().y()-m_begin.y();//纵向移动y
lastRect = lastRect.adjusted(dx,dy,dx,dy);//更新矩形的位置
m_begin = e->pos();//刷新拖拽点起始坐标
}
}
update();//触发窗体重绘
}
else if(m_type == 3)
{
if(m_pull == 0)//非拖拽
{
QRect& lastEllipse = m_oval.last();//拿到新椭圆
lastEllipse.setBottomRight(e->pos());//更新椭圆的右下角坐标)
}
else//拖拽
{
QRect& lastEllipse = m_oval.last();//拿到最后添加的矩形
if(lastEllipse.contains(e->pos()))//在椭圆内部
{
int dx = e->pos().x()-m_begin.x();//横向移动x
int dy = e->pos().y()-m_begin.y();//纵向移动y
lastEllipse = lastEllipse.adjusted(dx,dy,dx,dy);
m_begin = e->pos();//刷新拖拽点起始坐标
}
}
update();//触发窗体重绘
}
else if(m_type == 4)
{
QRect& lastLine = m_strline.last();//拿到新直线
lastLine.setBottomRight(e->pos());//更新直线另一端)
update();//触发窗体重绘
}
}
}
void markingPapers::mouseReleaseEvent(QMouseEvent *e)
{
if(m_leftpress)
{
if(m_type == 1)
{
QVector<QPoint>& lastLine = m_lines.last();//最后添加的线条,就是最新画的
lastLine.append(e->pos());//记录线条的结束坐标
m_leftpress = false;//标志左键释放
}
else if(m_type == 2)
{
QRect& lastRect = m_rects.last();//拿到新矩形
if(!m_pull)
{
lastRect.setBottomRight(e->pos());//不是拖拽时,更新矩形的右下角坐标)
//刚画完矩形,将光标设置到新矩形的中心位置,并进入拖拽模式
this->cursor().setPos(this->cursor().pos().x()-lastRect.width()/2,this->cursor().pos().y()-lastRect.height()/2);
m_pull = 1;
}
m_leftpress = false;
}
else if(m_type == 3)
{
QRect& lastEllipse = m_oval.last();//拿到新椭圆
if(!m_pull)
{
lastEllipse.setBottomRight(e->pos());//不是拖拽时,更新椭圆的右下角坐标)
//刚画完椭圆,将光标设置到新椭圆的中心位置,并进入拖拽模式
this->cursor().setPos(this->cursor().pos().x()-lastEllipse.width()/2,this->cursor().pos().y()-lastEllipse.height()/2);
m_pull = 1;
}
m_leftpress = false;
}
else if(m_type == 4)
{
QRect& lastLine = m_strline.last();//拿到新矩形
lastLine.setBottomRight(e->pos());//更新矩形的右下角坐标)
m_leftpress = false;
}
}
}