×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Painting only the dirty area of the widget in Qt

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(5 intermediate revisions by one user not shown)
Line 1: Line 1:
{{KBCS}}
+
{{Archived|timestamp=20120613044252|user=[[User:Hamishwillee|&lt;br /&gt;----]]|[[:Category:Qt Quick|Qt Quick]] should be used for all UI development on mobile devices. The approach described in this article (using C++ for the Qt app UI) is deprecated.}}
{{ArticleMetaData
+
{{ArticleMetaData <!-- v1.2 -->
|id=CS001516
+
|platform=Qt
+
|devices=Nokia 5800 XpressMusic, Nokia N900
+
|category=Qt
+
|subcategory=UI
+
|creationdate=November 25, 2009
+
|keywords=WA_OpaquePaintEvent, QPainter::setClipRegion()
+
 
+
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
+
|devices= Nokia 5800 XpressMusic, Nokia N900
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
+
|platform= Qt
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices.) -->
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|author=[[User:Tepaa]]
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= WA_OpaquePaintEvent, QPainter::setClipRegion()
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20091020
 +
|author= [[User:Tepaa]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory= UI
 +
|id= CS001516
 
}}
 
}}
  
Line 22: Line 30:
 
This example shows how to paint only the area of the widget whose content has been marked as invalid and needs to be updated. This will speed up the updating of the screen.
 
This example shows how to paint only the area of the widget whose content has been marked as invalid and needs to be updated. This will speed up the updating of the screen.
  
When the user draws a line using the stylus on the screen, the line is first painted into a <tt>QPixmap</tt> picture and then the picture is updated to the screen. The application only updates the dirty area of the picture.
+
When the user draws a line using the stylus on the screen, the line is first painted into a {{Icode|QPixmap}} picture and then the picture is updated to the screen. The application only updates the dirty area of the picture.
  
Dirty area painting can be handled by setting the <tt>Qt::WA_OpaquePaintEvent</tt> attribute of the widget as <tt>true</tt> and defining the dirty rect to <tt>QPainter</tt> with <tt>QPainter::setClipRegion()</tt>.
+
Dirty area painting can be handled by setting the {{Icode|Qt::WA_OpaquePaintEvent}} attribute of the widget as {{Icode|true}} and defining the dirty rect to {{Icode|QPainter}} with {{Icode|QPainter::setClipRegion()}}.
  
  
Line 30: Line 38:
  
 
* Qt is installed on your platform.
 
* Qt is installed on your platform.
** Download Qt for Symbian release from here: [http://qt.nokia.com/downloads]
+
** Download Qt release from here: [http://qt.nokia.com/downloads]
 
** Qt on Maemo can be found here: [http://qt4.garage.maemo.org/ Qt4 Maemo port]
 
** Qt on Maemo can be found here: [http://qt4.garage.maemo.org/ Qt4 Maemo port]
  
Line 36: Line 44:
 
==Header==
 
==Header==
  
<code cpp>
+
<code cpp-qt>
 
#include <QtGui>
 
#include <QtGui>
  
Line 73: Line 81:
 
==Source==
 
==Source==
  
<code cpp>
+
<code cpp-qt>
 
Widget::Widget(QWidget *parent) :
 
Widget::Widget(QWidget *parent) :
 
     QWidget(parent)
 
     QWidget(parent)
Line 201: Line 209:
 
* [http://doc.trolltech.com/4.5/qpainter.html QPainter]
 
* [http://doc.trolltech.com/4.5/qpainter.html QPainter]
  
[[Category:Qt]][[Category:Code Examples]][[Category: UI]][[Category:Code Snippet]]
+
[[Category:Qt C++ UI]][[Category:Code Snippet]][[Category: UI]][[Category:Code Snippet]][[Category:MeeGo Harmattan]] [[Category:Symbian]]

Latest revision as of 04:14, 11 October 2012

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (using C++ for the Qt app UI) is deprecated.

Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic, Nokia N900
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: WA_OpaquePaintEvent, QPainter::setClipRegion()
Created: tepaa (20 Oct 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

[edit] Overview

This example shows how to paint only the area of the widget whose content has been marked as invalid and needs to be updated. This will speed up the updating of the screen.

When the user draws a line using the stylus on the screen, the line is first painted into a QPixmap picture and then the picture is updated to the screen. The application only updates the dirty area of the picture.

Dirty area painting can be handled by setting the Qt::WA_OpaquePaintEvent attribute of the widget as true and defining the dirty rect to QPainter with QPainter::setClipRegion().


[edit] Preconditions

  • Qt is installed on your platform.


[edit] Header

#include <QtGui>
 
class Widget: public QWidget
{
Q_OBJECT
 
public:
Widget(QWidget *parent = 0);
~Widget();
 
void mouseMoveEvent(QMouseEvent *);
void mousePressEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
 
void paintEvent(QPaintEvent *);
void resizeEvent(QResizeEvent *);
 
private:
void paintToPixmap();
void paintLine(QPainter&);
QRect dirtyRect();
 
private:
QPoint p1;
QPoint p2;
 
QPen pen;
QColor color;
 
QPixmap pixmap;
};


[edit] Source

Widget::Widget(QWidget *parent) :
QWidget(parent)
{
p1 = QPoint(-1, -1);
p2 = QPoint(-1, -1);
 
color = QColor(Qt::black);
pen.setColor(color);
pen.setStyle(Qt::SolidLine);
pen.setWidth(2);
}
 
Widget::~Widget()
{
}
 
void Widget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
// Create pixmap that size is same as screen has
pixmap = QPixmap(event->size());
pixmap.fill(Qt::white);
}
 
void Widget::mousePressEvent(QMouseEvent *event)
{
p1 = event->pos();
p2 = QPoint(-1, -1);
 
// Opaquea paint, we paint only changed area of the screen
setAttribute(Qt::WA_OpaquePaintEvent, true);
}
 
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if (p2.x() == -1) {
p2 = event->pos();
}
else {
p1 = p2;
p2 = event->pos();
}
// Paint to pixmap and then pixmap on screen
paintToPixmap();
}
 
void Widget::mouseReleaseEvent(QMouseEvent *)
{
// Stop using opaquea paint
setAttribute(Qt::WA_OpaquePaintEvent, false);
 
// Update screen
update();
}
 
void Widget::paintToPixmap()
{
// Paint line to pixmap
QPainter painter;
painter.begin(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(pen);
paintLine(painter);
painter.end();
// Draw pixmap on the screen
update();
}
 
void Widget::paintLine(QPainter& painter)
{
if (p1.x() != -1 && p2.x() != -1) {
painter.drawLine(p1, p2);
}
}
 
QRect Widget::dirtyRect()
{
int left, top, width, height;
if (p1.x() == -1 || p2.x() == -1) {
return rect();
}
 
if (p1.x() < p2.x()) {
left = p1.x();
width = p2.x() - p1.x();
}
else {
left = p2.x();
width = p1.x() - p2.x();
}
 
if (p1.y() < p2.y()) {
top = p1.y();
height = p2.y() - p1.y();
}
else {
top = p2.y();
height = p1.y() - p2.y();
}
return QRect(left - 15, top - 15, width + 30, height + 30);
}
 
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
 
// Paint only changed area of the screen
if (testAttribute(Qt::WA_OpaquePaintEvent)) {
painter.setClipRegion(dirtyRect());
}
 
// Update pixmap into screen. If dirty
// area is set painter updates only that
painter.drawPixmap(QPoint(0, 0), pixmap);
}


[edit] Postconditions

The screen updates quickly.


[edit] See also

This page was last modified on 11 October 2012, at 04:14.
157 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×