代码之家  ›  专栏  ›  技术社区  ›  stanleyxu2005

qt绘制内容丢失

  •  2
  • stanleyxu2005  · 技术社区  · 15 年前

    我正在写一个信息屏幕程序。我创建了一个全屏小部件并在上面绘制内容。

    为了延长TFT显示设备的生命周期,我想实现像素移动功能。换言之,在每一个 X 分钟,我将屏幕切换到左/右/上/下 Y 像素。

    我的方法如下:

    1. 我使用两层(两个Qwidget)。
    2. 我在顶层绘制内容。
    3. 当执行像素移动时,我只是移动顶层以获得指定的偏移量。
    4. 然后在底层填充背景色。

    但是,我发现了一个问题:

    如果我向顶层移动10个像素,那么10个像素的内容将从屏幕中消失。但当我把这个层向下移动10个像素时。10个像素的内容将不会更新,它已不存在。

    如何保存这10个像素的内容?有没有什么神奇的小部件标志来解决这个问题?

    更新1: 代码是用D语言编写的,但很容易理解:

    class Canvas: QWidget
    {
        private QPixmap content;
    
        this(QWidget parent)
        {
            super(parent);
            setAttribute(Qt.WA_OpaquePaintEvent, true);
        }
    
        public void requestForPaint(QPixmap content, QRegion region)
        {
            this.content = content;
            update(region);
        }
    
        protected override void paintEvent(QPaintEvent event)
        {
            if (this.content !is null)
            {
                QPainter painter = new QPainter(this);
                painter.setClipping(event.region);
                painter.fillRect(event.region.boundingRect, new QColor(0, 0, 0));
                painter.drawPixmap(event.region.rect, this.content);
                this.content = null;
                painter.setClipping(false);
            }
        }
    }
    
    class Screen: QWidget
    {
        private Canvas canvas;
    
        this()
        {
            super(); // Top-Level widget
            setAutoFillBackground(True);
            this.canvas = new Canvas(this);
            showFullScreen();
        }
    
        public void requestForPaint(QPixmap content, QRegion region)
        {
            this.canvas.requestForPaint(content, region);
        }
    
        private updateBackgroundColor(QColor backgroundColor)
        {
            QPalette newPalette = palette();
            newPalette.setColor(backgroundRole(), backgroundColor);
            setPalette(newPalette);
        }
    
        public shiftPixels(int dx, int dy)
        {
            this.canvas.move(dx, dy);
            updateBackgroundColor(new QColor(0, 0, 0)); // Just a demo background color
        }
    }
    
    Screen screen = new Screen;
    screen.requestForPaint(some_content, some_region);
    screen.shiftPixels(0, -10);
    screen.shiftPixels(0, 10);
    
    2 回复  |  直到 15 年前
        1
  •  2
  •   Caleb Huitt - cjhuitt    15 年前

    看看代码,我的第一个猜测是您的区域可能是错误的。尝试每次重新绘制整个小部件,看看是否能解决丢失的10像素问题。如果是这样,那么试着找出为什么你的区域没有覆盖新暴露的部分。

    有一种可能性是:我注意到你 Screen::requestForPaint 直接调用的方法 Canvas::requestForPaint 与该地区无关。在qt中,类似的东西的坐标通常被假定为局部坐标,因此如果不考虑画布小部件的当前位置,可能会得到一个不正确的区域。

        2
  •  1
  •   elcuco    15 年前

    为什么不直接设置小部件的位置…?另一个选项可能是使用qpainter::translate(-1,-1)或类似的工具。