2014-02-28 153 views
3

我的目的是提供一个小部件,我可以在其中显示图像,单击此图像上的任意位置并显示单击的位置。 为此,我使用了Qt's image viewer example在QLabel中的可缩放QPixmap上绘图

void drawGCP(int x, int y) 
{ 
    // paint on a "clean" pixmap and display it 
    // origPixmap is the pixmap with nothing drawn on it 
    paintedPixmap = origPixmap.copy(); 
    QPainter painter(&paintedPixmap); 
    painter.setPen(Qt::red); 
    painter.drawLine(x - lineLength, y, x+lineLength, y); 
    painter.drawLine(x, y - lineLength, x, y + lineLength); 
    label->setPixmap(paintedPixmap); 
} 

这工作完全正常:当我点击我只是简单地添加上绘制像素图的方法。当我放大时,问题就出现了。标签被调整大小,我不知道如何添加图像。 这是我想要的。但是,我画的十字也是模糊的,线条是几个屏幕像素宽。我希望十字的宽度始终为1像素。

我试图用QGraphicsView和QGraphicsPixmapItem做同样的事情。 QGraphicsView有一个内置的缩放,但这里模糊不适用。

如何在由可缩放QLabel显示的QPixmap上绘制1像素宽的线?

更新: 这是我如何调用drawGCP:

void ImageGCPPicker::scaleImage(double factor) 
{ 
    scaleFactor *= factor; 
    if (scaleFactor < MIN_ZOOM) 
    { 
     scaleFactor /= factor; 
     return; 
    } 
    if (scaleFactor > MAX_ZOOM) 
    { 
     scaleFactor /= factor; 
     return; 
    } 

    horizontalScrollBar()->setValue(int(factor * horizontalScrollBar()->value() + ((factor - 1) * horizontalScrollBar()->pageStep()/2))); 
    verticalScrollBar()->setValue(int(factor * verticalScrollBar()->value() + ((factor - 1) * verticalScrollBar()->pageStep()/2))); 

    label->resize(scaleFactor * label->pixmap()->size()); 
    drawGCP(GCPPos.x(), GCPPos.y()); 
} 

正如你可以看到我画了十字缩放完成后。

+1

变焦之后绘制交叉。 – UmNyobe

+0

@UmNyobe也许就是这么简单。但我不知道如何去做与我所做的不同的事情。你能看看我的更新吗? – undu

回答

0

问题是,你正在画你的十字,然后缩放,当你应该放大然后绘画。

label->setPixmap(paintedPixmap); 

将规模paintedPixmap,与你的十字架已经画上了像素图。您可以修改drawGCP如下:

  1. 创建具有标签尺寸
  2. 一个paintedPixmap使用比例系数调整位置int x, int y
  3. 漆十字paintedPixmap新坐标。

基本上

void drawGCP(int x, int y, double factor) 
{ 
    //redundant with factor 
    bool resizeneeded = (label->size() != origPixmap(size)); 
    paintedPixmap = resizeneeded ? origPixmap.scaled(label->size()) : origPixmap.copy() ; 
    QPainter painter(&paintedPixmap); 
    painter.setPen(Qt::red); 

    //adjust the coordinates (can be done when passing parameters) 
    x *= factor; 
    y *= factor; 
    painter.drawLine(x - lineLength, y, x+lineLength, y); 
    painter.drawLine(x, y - lineLength, x, y + lineLength); 

    //will not scale pixmap internally 
    label->setPixmap(paintedPixmap); 
} 
+0

谢谢你的回答。不幸重新调整是永远是错误的。我认为它是label-> size()== origPixmap.size(); – undu

+0

@undu我犯了一个错误。它应该是'label-> size()!= origPixmap(size)'。我显然没有运行这个代码,所以可能会有这样的小错误 – UmNyobe