Qt::WA_OpaquePaintEvent理解
Qt助手中對Qt::WA_OpaquePaintEvent解釋如下:
Indicates that the widget paints all its pixels when it receives a paint event. Thus, it is not required for operations like updating, resizing, scrolling and focus changes to erase the widget before generating paint events. The use of WA_OpaquePaintEvent provides a small optimization by helping to reduce flicker on systems that do not support double buffering and avoiding computational cycles necessary to erase the background prior to painting. Note: Unlike WA_NoSystemBackground, WA_OpaquePaintEvent makes an effort to avoid transparent window backgrounds. This flag is set or cleared by the widget's author.
其含義大體是說:
?指示窗體在收到繪制事件時,繪制它的所有像素。因此,在收到繪制事件之前,對于更新、大小調(diào)整、滾動條滾動、焦點更變等操作時,并不要求窗體擦除窗體背景。使用該標志對那些不支持雙緩沖區(qū)的系統(tǒng)提供了小小的優(yōu)化,從而減小閃爍;同時避免在繪制之前因擦除背景需要計算而耗費時間。不同于WA_NoSystemBackground標志,WA_OpaquePaintEvent 盡量避免窗體背景透明,這個標志是由窗體的開發(fā)者設置或清除。
以上是Qt官方的解釋。個人理解如下:
1):?指示窗體在收到繪制事件時,繪制它的所有像素。這里“收到繪制事件”個人理解應該是第一
????????? 次收到繪制事件時,也即窗體構(gòu)建時,就把窗體所有的像素繪制一遍。
2):窗體一旦在完成1)中說的繪制完一遍所有像素后,此后所有有關窗體的更新、大小改變,焦
???????? 點改變、滾動條滾動等操作都不會擦除窗體背景。也就是說以前畫的東西都不會擦除重繪。
???????? 也就是以前畫的不重新計算、不重新繪制。當以前繪制的東西很費時間時(如:某個曲線、
???????? 某個圖形是經(jīng)過某個復雜的數(shù)學運算得出的),不重繪、不重新計算,這將節(jié)約很多cpu時間
???????? 大大提高效率(這也就是設置本標志后,不閃爍的原因吧)。
一個設置Qt::WA_OpaquePaintEvent標志的例子如下:
#include "QtWidgetsApplication1.h" #include<QPainter>QtWidgetsApplication1::QtWidgetsApplication1(QWidget* parent): QWidget(parent) {ui.setupUi(this);setWindowTitle("parent"); }QtWidgetsApplication1::~QtWidgetsApplication1() {}void QtWidgetsApplication1::paintEvent(QPaintEvent* event) {QPainter painter(this);for (int i = 0; i < 1000; ++i){int r = qrand() % 255;int g = qrand() % 255;int b = qrand() % 255;QPen pen(qRgb(r, g, b));pen.setWidth(2);painter.setPen(pen);int x = qrand() % 800;int y = qrand() % 800;// 其實核心內(nèi)容就是調(diào)用這一個函數(shù)而已painter.drawPoint(x, y);}}main函數(shù)如下:
#include "QtWidgetsApplication1.h" #include <QtWidgets/QApplication> #include<QTime> #include<QThread>int main(int argc, char *argv[]) {QApplication a(argc, argv);QtWidgetsApplication1 w;w.show();w.setAttribute(Qt::WA_OpaquePaintEvent);QTime timer;{timer.start();// 調(diào)用此函數(shù)即相當于Windows中的GetMessage,系列函數(shù),包括了tranlate,分發(fā)函數(shù)等的所有操作// 此處是控制幀數(shù)的關鍵點while (1){a.processEvents();w.repaint();QThread::sleep(1);}}return a.exec(); }運行后彈出窗體如下:
可以看到,窗體背景是黑色即不透明,因為不擦除背景即不擦除以前繪制的,故當?shù)?次繪制事件發(fā)生時,繪制1000個點;第2次繪制事件發(fā)生時,再繪制1000個點,那么第N次后,繪制的總點數(shù)為1000*N個。當屏蔽掉上面main.cpp的如下代碼:
w.setAttribute(Qt::WA_OpaquePaintEvent);則窗體背景為正常顏色且每次繪制之前都會擦除以前繪制的,故N次后窗體依然只有1000個點,如下:
?
總結(jié)
以上是生活随笔為你收集整理的Qt::WA_OpaquePaintEvent理解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: No relevant classes
- 下一篇: 英伟达驱动代码中发现游戏《反恐精英:全球