2014-09-21 458 views
1

我的GUI中有一個標籤,將圖像顯示爲QPixmap。我希望能夠通過單擊圖像上的任意位置來選擇起點,然後通過單擊圖像的其他部分在其他位置創建第二個點,從而在圖像上繪製出連續的線條。放置第二個點後,這兩個點應該立即連接,並且我希望能夠通過在圖像上放置更多點來繼續同一行。如何使用點在QPixmap上繪製一條線

雖然我知道如何繪製QPixmap上的東西,但我需要用來獲取點座標的鼠標事件令我感到困惑,因爲我對Qt還是比較新的。

任何解決方案的例子將不勝感激。

+0

請參閱我的編輯,我添加了非常重要的改進,如我所承諾的。 – Chernobyl 2014-09-21 15:16:56

回答

3

爲了這個目的,我建議你使用QGraphicsView。使用我的代碼片段,完美的作品。

子類QGraphicsScene

#ifndef GRAPHICSSCENE_H 
#define GRAPHICSSCENE_H 

#include <QGraphicsScene> 
#include <QPoint> 
#include <QMouseEvent> 
class GraphicsScene : public QGraphicsScene 
{ 
    Q_OBJECT 
public: 
    explicit GraphicsScene(QObject *parent = 0); 

signals: 

protected: 
    void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent); 

public slots: 
    private: 

    QPolygon pol; 

}; 

#endif // GRAPHICSSCENE_H 

.cpp文件:

#include "graphicsscene.h" 
#include <QDebug> 
#include <QGraphicsSceneMouseEvent> 
#include <QPainter> 

GraphicsScene::GraphicsScene(QObject *parent) : 
    QGraphicsScene(parent) 
{ 
addPixmap(QPixmap("G:/2/qt.jpg"));//your pixmap here 
} 

void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) 
{ 
    //qDebug() << "in"; 
    if (mouseEvent->button() == Qt::LeftButton) 
    { 

     QPoint pos = mouseEvent->scenePos().toPoint(); 
     pol.append(pos); 
     if(pol.size() > 1) 
     { 
       QPainterPath myPath; 
       myPath.addPolygon(pol); 
       addPath(myPath,QPen(Qt::red,2)); 
     } 
    } 
} 

用法:

#include "graphicsscene.h" 
//... 
GraphicsScene *scene = new GraphicsScene(this); 
ui->graphicsView->setScene(scene); 
ui->graphicsView->show(); 

結果:

enter image description here

如果你想保存新的像素圖(或只是讓像素映射)的圖像,使用此代碼:

QPixmap pixmap(ui->graphicsView->scene()->sceneRect().size().toSize()); 
QString filename("example.jpg"); 
     QPainter painter(&pixmap); 
     painter.setRenderHint(QPainter::Antialiasing); 
     ui->graphicsView->scene()->render(&painter, pixmap.rect(),pixmap.rect(), Qt::KeepAspectRatio); 
     painter.end(); 

     pixmap.save(filename); 

隨着render()你也可以抓住你的場景的不同區域。

但是這段代碼可以更好:我們創建並繪製相同的多邊形。如果我們可以記住最後一個繪製的點,那麼我們可以逐行繪製(行的開始是最後一行的結束)。在這種情況下,我們不需要所有的點,我們只需要最後一點。

正如我答應(代碼改進):只提供額外的變量QPoint last;而不是QPolygon pol;並使用下面的代碼:

void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) 
{ 
    //qDebug() << "in"; 
    if (mouseEvent->button() == Qt::LeftButton) 
    { 

     QPoint pos = mouseEvent->scenePos().toPoint(); 
     if(last.isNull()) 
     { 
      last = pos; 
     } 
     else 
     { 
      addLine(QLine(last,pos),QPen(Qt::red,2)); 
      last = pos; 
     } 
    } 
} 

正如你所看到的,你只存儲最後一點,油漆只有最後一行。用戶可以點擊幾千次,現在你不需要存儲這些不必要的點,並做這不必要的重新繪製。

+0

爲了回答這個問題,你可以添加代碼來獲取結果作爲'QPixmap'。 – hyde 2014-09-21 12:54:57

+0

@hyde已經完成。 – Chernobyl 2014-09-21 13:05:32