大家好,我正在做一个小程序,首先打开一个图像,然后检测mouseMoveEvent,在鼠标左键移动的地方画红色,右键移动的地方画蓝色。但是打开图片之后在图片上移动鼠标画不出线来,哪位大侠指点下,感激不尽。。
#include <QtGui>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
openAction=new QAction(tr("&Open"),this);
fileMenu=menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAction);
connect(openAction,SIGNAL(triggered()),this,SLOT(openImage()));
}
bool MainWindow::openImage()
{
QString fileName=QFileDialog::getOpenFileName(this,tr("Select an Image"),QDir::currentPath());
if(!img.load(fileName))
return false;
update();
updateGeometry();
return true;
}
void MainWindow::paintEvent(QPaintEvent */*event*/)
{
QPainter painter(this);
painter.drawImage(0,0,img);
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
QRgb foregroundColor=Qt::red;
QRgb backgourndColor=Qt::blue;
if(event->button()==Qt::LeftButton)
img.setPixel(event->pos(),foregroundColor);
else if(event->button()==Qt::RightButton)
img.setPixel(event->pos(),backgourndColor);
update();
}