【问题标题】:Tiled image in QGraphicsView foregroundQGraphicsView前景中的平铺图像
【发布时间】:2015-07-16 20:06:22
【问题描述】:

我正在开发一个使用主 QGraphicsView 制作的 Qt 应用程序。

这个视图可以显示和切换不同的QgraphicsScenes。此应用程序需要始终在每个场景前面都有一个叠加层,因此最好的方法是通过 QGraphicsView 的setForegroundBrush() 方法进行叠加。

但我的叠加层是平铺图像,我可以在其中编辑源图像的不透明度和比例。

这是在我的 QGraphicsView 类构造函数中编写的代码:

QString imgPath("path/to/image.png");
QPixmap map(imgPath);
QPainter painter(this);
QRectF zone(0,0,map.width(),map.height());
painter.drawPixmap(zone,map,zone);
QBrush brush = painter.brush();
brush.setStyle(Qt::TexturePattern);
setForegroundBrush(brush);

但是不起作用,什么也没有显示。 我用 QPixmap 测试了一个简单的 QBrush,效果很好,但我需要使用 QPainter 才能编辑图像的不透明度。

【问题讨论】:

    标签: qt qgraphicsview qpixmap


    【解决方案1】:

    最后,我认为在 QGraphicsView 前景中拥有平铺图像的最简单方法是重新实现drawForeground(QPainter *painter, const QRectF &rect)

    void Frontend::drawForeground(QPainter *painter, const QRectF &rect){
        float alpha = 0.15;
        float scale = 2;
        QString imgPath("path/to/image.png");
        QPixmap img(imgPath);
        painter->scale(scale,scale);
        painter->setOpacity(alpha);
        painter->drawTiledPixmap(rect,img);
    }
    

    【讨论】:

      【解决方案2】:

      您不能在其paintEvent 方法之外的小部件上绘画。也许您想让画家处理像素图 (painter(&map)) 而不是小部件 (painter(this))?

      您还可以通过以下方式添加叠加层:

      1. 在派生场景的重新实现的paintEvent 中绘制它,确保不是在场景上绘制,而是在其viewport() 上绘制。视图的paintEvent 调用了一些便捷方法,例如drawBackgrounddrawForeground

      2. 在通用 QWidget 叠加层中绘制它。

      我有several answers,它演示了如何在小部件上以及场景视图上获得覆盖。

      【讨论】:

      • 谢谢,但我只是看到我可以在我的 QGraphicsView 类中重新实现 drawForeground(QPainter *painter) 方法,并且使用这个函数我可以看到覆盖,但不能平铺。是否可以直接在此功能上获得平铺覆盖?
      • @mickaelb91 是的,各种drawXxx 便捷方法是一种无需直接覆盖paintEvent 的方法。如果您可以使用它们 - 就这样做吧。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多