c++ - Display text inside QGraphicsPolygonItem without copying std::string to QString? -


i using qgraphicsscene draw millions of polygons. plan use qt+opengl later, not drawing more 1 million polygons , qt handles fine. problem arises when try display text inside polygons.

each polygon (a custom class inheriting qgraphicspolygonitem) visual representation of object, , has pointer object associated it. each object has std::string identifier. if display that string inside polygons, should fine memory-wise. however, qt seems need qstring instead, , takes lot of time , space convert every string. creating qgraphicstextobject each polygon , each of needs qstring copy of std::string. there way bypass copy using qt?

cropping scene not desirable. there polygons small text fit inside them, , 1 can see them zooming in scene. these polygons (and text) need not displayed (unless user zooms in), don't think without creating various other issues first.

p.s.: displaying text on-demand (as user hovers mouse on each polygon, instance) possible, it'd ideal if text readily displayed.

have profiled see if conversion bottleneck? font rendering isn't fast, , if tries render millions of texts, slow. won't around string conversions, thing think of optimize when them, , how often.

first, i'd consider use custom item either painting text manually reimplementing qgraphicsitem::paint, or deriving e.g. qgraphicssimpletextitem, allows more tweaking using qgraphicssimpletextitem or qgraphicstextitem directly, both require call settext() , convert string upfront.

one thing conscious when conversion done. custom item, wouldn't need std::string qstring conversion upfront (when calling settext()), store std::string , on demand only, in paint() implementation, i.e. convert on first paint() call , cache.

another potentially expensive calculation boundingrect(). tweaked returning less precise approximation of actual text shape. doesn't hurt if returned rectangle bigger painted rectangle, shouldn't smaller. 1 use hardcoded height * approx. letter width + padding.

then, text not drawn @ cheaper. if view zoomed out, 10^6 items drawn (and paint() called), 1 have hard time read text. i'd reimplement paint() , make use of qgraphicsview's level-of-detail mechanism (see here) , not convert string nor paint below level of detail/zoom level.

if can use qt 5.4, qgraphicsscene::minimumrendersize might come handy. alone won't avoid string conversions nor boundingrect() when not combined other suggestions above.


Popular posts from this blog