本文介绍了试图在Qt中只更新窗口小部件中的矩形,但整个窗口小部件的区域将更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这发生在Qt模拟器(手机)。我试图只更新小部件区域的一部分,但整个小部件更新。



为了说明,下面的代码:

  void Widget mousePressEvent(QMouseEvent * event)
{
update(0,0,10,10);
}

void Widget :: paintEvent(QPaintEvent * event)
{
qDebug()< event-> rect();
}

点击窗口小部件时会显示以下调试输出:





$ b

$ b

编辑



我在Linux上运行相同的代码,它的工作原理,调试输出


解决方案

在大多数GUI框架中,我已经看到你不能只更新应用程序窗口/窗口部件的一部分。即使API中有一些函数来更新一些矩形(例如update(x,y,x,y)) - 它只是通知框架它需要更新至少给定矩形和框架可以更新屏幕的更大部分。



我不知道它是如何工作在手机Qt,但它是这样在桌面版本,因为在大多数OSes GUI应用程序不会在任何地方存储它的图像,如果你最小化然后显示你的窗口,你需要重新创建整个表面。



这意味着你可以不要依赖于这样的假设,即你需要绘制一些东西,然后当你需要时,你将在一些自定义矩形中添加其他绘制操作。你应该实现一些通用的绘制功能,可以从头重绘一切,并将画优化到框架。


This happens in Qt Simulator (for phones). I'm trying to update only a portion of a widget's area, but the entire widget is updated instead.

To illustrate, the following code:

void Widget::mousePressEvent(QMouseEvent *event)
{
    update(0, 0, 10, 10);
}

void Widget::paintEvent(QPaintEvent *event)
{
    qDebug() << event->rect();
}

Gives the following debug output when I click on the widget:

Which is the entire area of the widget.

What am I doing wrong here?


Edit

I ran the same code on Linux, and it worked as it should, the debug output was

解决方案

In most GUI framework I've seen you can't update just some part of the application window/widget. Even if there's some function in API to update some rectangle - like update(x,y,x,y) you're using - it's just to inform the Framework that it needs to update at least the given rectangle, and framework can update a bigger part of the screen.

I'm not sure how it works in phone Qt but it's done this way in desktop version because in most OSes GUI application doesn't store its 'image' anywhere, and if you minimize and then show your window you need to recreate entire surface.

All this means that you can't rely on the assumption, that you'll paint something and then you'll be adding other paint operations in some custom rectangle when you need. You should implement some general 'paint' function, that can redraw everything from scratch and leave the painting optimization to the framework.

这篇关于试图在Qt中只更新窗口小部件中的矩形,但整个窗口小部件的区域将更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 00:15