我正在写一个信息屏幕程序。我创建了一个全屏小部件并在上面绘制内容。
为了延长TFT显示设备的生命周期,我想实现像素移动功能。换言之,在每一个
X
分钟,我将屏幕切换到左/右/上/下
Y
像素。
我的方法如下:
-
我使用两层(两个Qwidget)。
-
我在顶层绘制内容。
-
当执行像素移动时,我只是移动顶层以获得指定的偏移量。
-
然后在底层填充背景色。
但是,我发现了一个问题:
如果我向顶层移动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);