我正在寫一個信息屏幕程序。我創建了一個全屏小部件並在其上繪製內容。Qt畫內容丟失
爲了延長TFT顯示設備的生命週期,我想實現像素移位功能。換言之,在每個X分鐘內,我將屏幕左移/右移/上/下移Y像素。
我的方法如下:
- 我使用兩個層(2 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);
你能發表一些重現問題的示例代碼嗎?我從來沒有見過一個小部件無法重繪時,它變得可見。 –