[英]How to add a circle to a widget in Qt without needlessly repainting everything?
我正在學習 QPainter,並且我創建了一個簡單的小部件,每次用戶單擊小部件時,都會在該點出現一個新圓圈。
但是Qt不允許在paintEvent之外繪畫,所以每次我想畫一個新的圓時,我需要使小部件區域無效並重新繪制所有以前的圓。 這似乎不是很有效 - 如果有數百甚至數千個元素怎么辦。
最好不要刪除以前的圓圈,我只是在小部件的頂部繪制了新的圓圈。 但是在 Qt 上,如果不首先使(並因此擦除)先前的內容無效,我就無法繪制。
在 Qt 中處理這種情況的推薦方法是什么?
處理這種情況的推薦方法是使用QGraphicsScene和QGraphicsView ,然后使用QGraphicsItems填充場景。 根據文檔,這正是該框架的設計目的。
簡而言之,您將覆蓋QGraphicsScene::mousePressEvent()
,並在新方法中創建一個new QGraphicsEllipseItem
。
無需使整個小部件無效。 update() 和 repaint() 可以獲取您想要重新繪制的坐標,因此只重新繪制發生變化的部分。
void update ( int x, int y, int w, int h )
void update ( const QRect & rect )
void update ( const QRegion & rgn )
void repaint ( int x, int y, int w, int h )
void repaint ( const QRect & rect )
void repaint ( const QRegion & rgn )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.