• 6721阅读
  • 2回复

[提问]QImage上面画线的问题 [复制链接]

上一主题 下一主题
离线wmlong
 
只看楼主 倒序阅读 楼主  发表于: 2011-05-11
大家好,我正在做一个小程序,首先打开一个图像,然后检测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();
}
离线wmlong
只看该作者 1楼 发表于: 2011-05-11
自己顶一下。。快来人啊
离线cutemmll
只看该作者 2楼 发表于: 2011-05-11
如果直接在mouseMoveEvent中进行画线操作,画出来的线是不连贯的。其实是一连串的点,而且看你的代码是直接操作图片,这样貌似也不好。废话结束,我直接帖源代码了:
drawlinewidget.h
  1. #ifndef DRAWLINEWIDGET_H
  2. #define DRAWLINEWIDGET_H
  3. #include <QWidget>
  4. #include <QDebug>
  5. #include <QVector>
  6. #include <QPainterPath>
  7. class QPixmap;
  8. class QPaintEvent;
  9. class DrawLineWidget : public QWidget
  10. {
  11.     Q_OBJECT
  12. public:
  13.     enum ColorType
  14.     {
  15.         Init = 0,
  16.         Red ,
  17.         Blue
  18.     };
  19.     explicit DrawLineWidget(QWidget *parent = 0);
  20.     //设置图片的路径
  21.     void setPixmap(const QString& path);
  22. signals:
  23. public slots:
  24. protected:
  25.     void paintEvent(QPaintEvent *);
  26.     bool eventFilter(QObject *, QEvent *);
  27. private:
  28.     QPixmap pixmap;
  29.     ColorType type;
  30.     bool needUpdate;
  31.     QPainterPath  tempPath;
  32.     QPainterPath  redPath;
  33.     QPainterPath  bluePath;
  34. };
  35. #endif // DRAWLINEWIDGET_H


drawlinewidget.cpp
  1. #include <QPainter>
  2. #include <QPaintEvent>
  3. #include "drawlinewidget.h"
  4. DrawLineWidget::DrawLineWidget(QWidget *parent)
  5.     :  QWidget(parent)
  6.     ,  needUpdate(false)
  7.     ,  type(Init)
  8. {
  9.     installEventFilter(this);
  10. }
  11. void DrawLineWidget::setPixmap(const QString &path)
  12. {
  13.     if(path.isEmpty())
  14.         return;
  15.     if(pixmap.load(path))
  16.     {
  17.         qDebug()<<"load image ok";
  18.         update();
  19.     }
  20. }
  21. void DrawLineWidget::paintEvent(QPaintEvent *)
  22. {
  23.     QPainter painter(this);
  24.     painter.setRenderHint(QPainter::Antialiasing);
  25.     painter.drawPixmap(0,0,pixmap);
  26.     if(needUpdate)
  27.     {
  28.         painter.setPen(Qt::red);
  29.         painter.drawPath(redPath);
  30.         if(type == Red)
  31.             painter.drawPath(tempPath);
  32.         painter.setPen(Qt::blue);
  33.         painter.drawPath(bluePath);
  34.         if(type == Blue)
  35.             painter.drawPath(tempPath);
  36.     }
  37. }
  38. bool DrawLineWidget::eventFilter(QObject *, QEvent *e)
  39. {
  40.     if(e->type() == QEvent::MouseMove)
  41.     {
  42.         QMouseEvent* et = static_cast<QMouseEvent*>(e);
  43.         tempPath.lineTo(et->pos());
  44.         needUpdate = true;
  45.         update();
  46.     }else if(e->type() == QEvent::MouseButtonPress)
  47.     {
  48.         tempPath = QPainterPath();
  49.         QMouseEvent* et = static_cast<QMouseEvent*>(e);
  50.         if(et->button() == Qt::LeftButton)
  51.             type = Red;
  52.         else if(et->button() == Qt::RightButton)
  53.             type = Blue;
  54.         tempPath.moveTo(et->pos());
  55.     }else if(e->type() == QEvent::MouseButtonRelease)
  56.     {
  57.         if(!tempPath.isEmpty())
  58.         {
  59.             if(type == Red)
  60.                 redPath.addPath(tempPath);
  61.             else
  62.                 bluePath.addPath(tempPath);
  63.         }
  64.     }
  65.     return false;
  66. }

[ 此帖被cutemmll在2011-05-11 17:35重新编辑 ]
c------------enjoy qt & enjoy life-----------++
快速回复
限100 字节
 
上一个 下一个