繁体   English   中英

在QLabel中绘制可缩放的QPixmap

[英]Paint over a zoomable QPixmap in a QLabel

我的目的是提供一个小部件,我可以在其中显示图像,单击此图像上的任意位置并显示我单击的位置。 为此,我使用了Qt的图像查看器示例 我点击时添加了一个在pixmap上绘制的方法:

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());
}

如您所见,我在缩放完成后绘制了十字形。

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

label->setPixmap(paintedPixmap);

将缩放paintedPixmap你的十字架已经画在像素图上 您可以按如下方式修改drawGCP

  1. 创建一个带有标签尺寸paintedPixmap
  2. 使用比例因子调整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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM