【问题标题】:How to stop the background from repeating in a QpaintEvent如何阻止背景在 QpaintEvent 中重复
【发布时间】:2012-08-20 14:47:53
【问题描述】:

我有一个paintEvent,每次我画一些东西时我都会添加背景, 问题是我需要图像停止重复。

例如,如果我的背景图像是 20 像素 x 20 像素,而我的 绘制区域为 40pixels x 40pixels,背景为 4 张图片,我只需要一张。

这是我的代码。

1. QPainter painter; // painter for the canvas
2. painter.begin(this);
3. QPixmap backgroundImage;
4. backgroundImage.load("multimedia/monitor_image.png");
5. painter.fillRect(event->rect(), QBrush(backgroundImage));

感谢您的帮助!

【问题讨论】:

    标签: qt4 qpainter qpixmap paintevent


    【解决方案1】:

    有很多函数可以帮助缩放图片:

    http://doc.qt.nokia.com/4.7-snapshot/qpixmap.html

    如果您知道高度,请尝试“scaleToHeight”。

    这样的事情应该可以工作:

    backgroundImage = backgroundImage.scaledToHeight(painter.height);
    

    在调用 fillRect 之前执行此操作,如果它是一个完美的正方形,则应该没有问题。

    【讨论】:

      猜你喜欢
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      • 2019-11-12
      • 2021-02-08
      相关资源
      最近更新 更多