-
QGraphicsScene/QGraphicsView performance problems
Hi!
I am trying to write a microchip CAD datafile display program using the QGraphics View framework. It is the first time i use this, and it seems to be very convenient, very little (of my own) code is needed to do what I want to do.
However, my data files may range from small to quite large, anything between a few 100kb and up to a few 100Mb. At about 20Mb, my application begins to choke, i think mainly due to memory consumption. A Linux amd64 dual core system gets completely stuck when loading a 40Mb file, I had to terminate to avoid a complete system crash.
My MacOSX ppc G5 dual core behaves just the same.
I have not tried Windows yet.
I use Qt4.3.3 and recently Qt4.4.0 snapshot, same results on each.
I have a feeling there is a lot of stuff in the QGraphics View that is not needed for my app, I just need to draw, scroll and zoom, no select, move or transform.
Is the QGraphics View framework the right thing to use for me?
If so, can I turn off some of the bells and whistles to gain performance?
Or should I use some different methods?
Any suggestions are appreciated.
-
Re: QGraphicsScene/QGraphicsView performance problems
If it chokes while loading the data, there is a chance the slowdown is not related to graphics view at all but instead to the loading routine. Use a profiler to find the bottleneck of the application. Also if you have multiple cores, try to design your application in such a way that all cores are used (especially when loading the data).
-
Re: QGraphicsScene/QGraphicsView performance problems
Just realized how not to use itemized data, so I am drawing in myQGraphicsView::drawBackground() instead. Now it doesn't use much memory, so I can load large files.
Now the problem is back to performance speed again, and I need to check in my own code wether the shapes are in the visible view or not, and find a way to sift through the data real fast. Also, the scrolling is directly depending on how fast I can draw.
I found that it was impossible to draw a mouse rubberband if GraphicsView was not cashed.
Is there a way to cache the background drawing?
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
Just realized how not to use itemized data, so I am drawing in myQGraphicsView::drawBackground() instead.
But now you are not using the graphics view architecture at all. You could as well use a plain QWidget instead.
-
Re: QGraphicsScene/QGraphicsView performance problems
Yes, I realize that. Yet the way of using methods of zooming in/out and the scrollers are the same. which is convenient for me right now. I am only a Qt beginner, so I would need to study how to do the same with more "standard" methods. Will I gain much performance if I change to QWidget? I suspect I would never get it working for a 150Mb data file if I use QGraphicsItems with QGraphicsScene/QGraphicsView.
BTW, I am using QDataStream to repeatedly read my data, even for updates. I guess this is not terribly efficient. Can you recommend another way?
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
Will I gain much performance if I change to QWidget?
You'll lose the overhead caused by everything that QGraphicsView has and QWidget has not.
Quote:
I suspect I would never get it working for a 150Mb data file if I use QGraphicsItems with QGraphicsScene/QGraphicsView.
I'm not so sure. One graphics item takes about 50 bytes of memory on average. How many items would you have in a 150MB file? 1M? That's less than 100MB of RAM used for the items.
Quote:
BTW, I am using QDataStream to repeatedly read my data, even for updates. I guess this is not terribly efficient. Can you recommend another way?
Hard to say without knowing what your data represents. But if you do that constantly then that is surely inefficient.
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
Is there a way to cache the background drawing?
But really you need to provide some more information. How many items, how complex are these items? Drawing them manually in drawbackground is not really the best way to go about it.
-
Re: QGraphicsScene/QGraphicsView performance problems
Typically, I might want to display a few thousand up to 10-20 million trapezoidal shapes, either as filled or empty, with different colours.
When I have used itemized drawing, I do
Code:
if(fill) scene->addPolygon(Trapezoid,AreaShotPen,AreaShotBrush);
else scene->addPolygon(Trapezoid,AreaShotPen);
or
Code:
if(fill) scene->addRect(outRect,AreaShotPen, AreaShotBrush);
else scene->addRect(outRect,AreaShotPen);
This should be simple, but about 1 million shapes seems to be the limit right now, both for memory consumption and the patience of the user.
I am using QRectF and QPolygonF and true microns as internal numbers, would it improve performance if I use integer QRect and QPolygon instead?
-
Re: QGraphicsScene/QGraphicsView performance problems
I wouldn't use polygons here. You have a pretty simple shape, you can create your own item class which will be much faster.
-
Quote:
Quote:
I suspect I would never get it working for a 150Mb data file if I use QGraphicsItems with QGraphicsScene/QGraphicsView.
I'm not so sure. One graphics item takes about 50 bytes of memory on average. How many items would you have in a 150MB file? 1M? That's less than 100MB of RAM used for the items.
I have a hard time believing that. A file of 600,000 shapes costs me about 500Mb of additional RAM, which indicates almost 1kb per item. I must be doing something seriously wrong....
Quote:
I wouldn't use polygons here. You have a pretty simple shape, you can create your own item class which will be much faster.
Sorry, I am too new to Qt to implement such a thing. Could you direct me to some example?
Also, polygons are mostly only about 10-20% of the total population, the rest are rectangles. Still worthwile to make a new class?
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
I have a hard time believing that. A file of 600,000 shapes costs me about 500Mb of additional RAM, which indicates almost 1kb per item. I must be doing something seriously wrong....
Because you are using QGraphicsPolygonItem. You can probably use something simpler instead.
Quote:
Originally Posted by
bnilsson
Sorry, I am too new to Qt to implement such a thing. Could you direct me to some example?
See examples bundled with Qt.
Quote:
Also, polygons are mostly only about 10-20% of the total population, the rest are rectangles. Still worthwile to make a new class?
Yes, because they are the most heavy of your items so they probably occupy about 50% of the memory used by items. A simple esitmate of the memory can be calculated as follows:
total_per_item = sizeof(QGraphicsItem) + sizeof(data_stored_within_item) where the latter is:
in case of a rect item: sizeof(QRectF) + sizeof(QPen) + sizeof(QBrush) = sizeof(QPointF)+sizeof(QSizeF) + sizeof(QPen)+sizeof(QBrush) = 2*sizeof(double) + 2*sizeof(double) + ...
in case of a polygon item: sizeof(QPolygonF) + sizeof(QPen) + sizeof(QBrush) = sizeof(QVector<QPointF>) + ... = n * sizeof(QPointF) + ... = n*2*double + ... = at least twice as much as for QRectF with n>=4.
So a rect item data occupies probably about 70-80 bytes and a polygon item probably around 100. Add all the additional stuff kept by all the classes to that and multiply by the number of items.
BTW. Also consider using fewer more complex items instead of more simpler ones. Maybe you can "connect" your trapezoids into more complex shapes?
-
Re: QGraphicsScene/QGraphicsView performance problems
Thanks for your engagement, but I don't seen go get anywhere.
I managed to implement a subclass of QGraphicsItem, but it does not draw, I have no idea why. Could you help me?
Code:
#ifndef SHAPEITEM_H
#define SHAPEITEM_H
#include <QtGui>
#include <QGraphicsItem>
#include <QObject>
{
public:
ShapeItem(void);
ShapeItem
(const QRect *rect
);
private:
int x0,y0,x1,y1,x2,y2,x3,y3;
int xmin,ymin,xmax,ymax;
};
#endif
Code:
#include "shapeitem.h"
ShapeItem
::ShapeItem(const QRect *rect
){
x0 = rect->x();
y0 = rect->y();
x1 = rect->x() + rect->width();
y1 = y0;
x2 = x1;
y2 = y0 + rect->height();
x3 = x0;
y3 = y2;
xmin = x0;
xmax = x1;
ymin = y0;
ymax = y2;
}
ShapeItem
::ShapeItem(const QPolygon *poly
){
poly->point(0,&x0,&y0);
poly->point(1,&x1,&y1);
poly->point(2,&x2,&y2);
poly->point(3,&x3,&y3);
xmin = xmax = x0;
ymin = ymax = y0;
if(x1<xmin) xmin = x1;
if(x2<xmin) xmin = x2;
if(x3<xmin) xmin = x3;
if(y1<ymin) ymin = y1;
if(y2<ymin) ymin = y2;
if(y3<ymin) ymin = y3;
if(x1>xmax) xmax = x1;
if(x2>xmax) xmax = x2;
if(x3>xmax) xmax = x3;
if(y1>ymax) ymax = y1;
if(y2>ymax) ymax = y2;
if(y3>ymax) ymax = y3;
}
ShapeItem::boundingRect() const
{
qreal penWidth = 1;
return QRectF(xmin
- penWidth
/ 2, ymin
- penWidth
/ 2,
xmax + penWidth / 2, ymax + penWidth / 2);
}
ShapeItem::shape() const
{
path.addRect(boundingRect());
return path;
}
void
{
painter->drawLine ( x0,y0,x1,y1 );
painter->drawLine ( x1,y1,x2,y2 );
painter->drawLine ( x2,y2,x3,y3 );
painter->drawLine ( x3,y3,x0,y0 );
}
Not at all optimized, as soon as it draws me anything in the scene, I will test different variants. At this stage, numItems=0 in myQGraphicsItems::drawIntems, so I'm stuck.
I am using it lile this:
Code:
ShapeItem item(&outRect);
scene->addItem(&item);
//scene->addRect(outRect,solidBlue);
The commented-out line draws as it should, while the active lines does not.
-
Re: QGraphicsScene/QGraphicsView performance problems
Create the item on heap.
Code:
ShapeItem *item = new ShapeItem(...);
scene->addItem(item);
-
Re: QGraphicsScene/QGraphicsView performance problems
Thanks, now it is working.
Unfortunately, now the app started gobbling up all memory even for a smaller file, so it got worse. I will have to figure out why...
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
Unfortunately, now the app started gobbling up all memory even for a smaller file, so it got worse. I will have to figure out why...
Use valgrind. The Massif tool might be especially helpful.
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Use valgrind. The Massif tool might be especially helpful.
For the moment I'm on MacOSX, so this is not applicable.
-
Re: QGraphicsScene/QGraphicsView performance problems
The behaviour was a bit better using Qt4.4.0 than Qt4.3.3, so maybe it's not in my code entirely. But still in the 1Gb range for a 40Mb file.
The behaviour was very strange indeed (excessive cpu and memory load when zooming IN) on my amd46 Debian using Qt4.3.3, and this particular problem was fixed by the Qt4.4.0 snapshot.
Anyway, standard usage of QGraphicsView/QGraphiscScene was not dramatically improved using Qt4.4.0.
-
Re: QGraphicsScene/QGraphicsView performance problems
Could you provide a minimal compilable example reproducing the problem so that we might try it ourselves?
-
Re: QGraphicsScene/QGraphicsView performance problems
Sure, the project is not very big, I will try to prepare something.
I am new to this forum, what are the provisions for uploading?
-
Re: QGraphicsScene/QGraphicsView performance problems
Use the attachments feature ("Manage attachments" button below the advanced editor).
-
1 Attachment(s)
What would be your primary platform?
I will have to arrange some way for you to pick up the datafiles, they will be too large for an attachment. I think I have some space at my ISP's web hotel, I will do this tomorrow.
What will be your primary project platform?
Here is the project. It is currently tested only for Mac, I have not built this particular project for Linux yet.
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
What would be your primary platform?
Mine is Linux/x86.
Quote:
I will have to arrange some way for you to pick up the datafiles, they will be too large for an attachment.
Can't you just provide some small application that would generate such example datafile? They don't have to make sense and they don't have to be larger than a megabyte. It's just a test app.
Quote:
Here is the project.
It'd be best if you provided something small. It's easier to debug then.
BTW. I just had a quick look at the code you provided and I have some comments:
- I'm not sure if manipulating the update mode the way you do is a good idea, you should set the update mode once and never touch it again
- I'm not sure if beginning by reimplementing the main drawing routine is a good idea. I'd start with the base class implementation first, it's probably trying to be smarter than your implementation
- why do you use the data stream if you only read raw data and seek through the file? Operate on the file directly instead
- try not to fall of the scale with your dimensions, 10nm = 10E-8, pretty close to 32bit precision.
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
wysota
Mine is Linux/x86.
Were you able to build the application?
Quote:
Can't you just provide some small application that would generate such example datafile? They don't have to make sense and they don't have to be larger than a megabyte. It's just a test app.
The example datafile supplided, "testexp_c.j51" should enable you to thest the function, but maybe not show the problem. I don't have a J51 generator ready, but I can try to make one. It would not take long, I hope.
Quote:
It'd be best if you provided something small. It's easier to debug then.
I can strip it down further if you want. Should I?
Quote:
BTW. I just had a quick look at the code you provided and I have some comments:
- I'm not sure if manipulating the update mode the way you do is a good idea, you should set the update mode once and never touch it again
If I turn off update permantly it doesn't scroll. If I turn it on a large file will be drawn twice or more, taking maybe 10-30s extra before the user can zoom in to any region of interest.
Do you have any suggestion on how to handle this?
Quote:
- I'm not sure if beginning by reimplementing the main drawing routine is a good idea. I'd start with the base class implementation first, it's probably trying to be smarter than your implementation
I dont' quite uderstand. Please expalin.
This is my very forst Qt application, I took the outline from a basic example (a text editor) described in the documentation. My plan was to clean it up later, and evenually make a multidocument application. But I got stuck before I got to that point.
Quote:
- why do you use the data stream if you only read raw data and seek through the file? Operate on the file directly instead
Sure. In one of my later versions I make a linked list from the data analyzed.
Quote:
- try not to fall of the scale with your dimensions, 10nm = 10E-8, pretty close to 32bit precision.
10-8 compared to 1 meter, yes. In the drawing unity means 1 micrometer, so the precision is no problem here.
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
Were you able to build the application?
I had no data so I didn't even try.
Quote:
I can strip it down further if you want. Should I?
You could get rid of the loading routines and just generate polygons and rectangles in the application.
Quote:
Do you have any suggestion on how to handle this?
Yes, find out why it draws twice.
Quote:
I dont' quite uderstand. Please expalin.
Don't reimplement anything from the scene or the view. Just you the classes provided only changing their properties and adding items to the scene.
Edit:
The application works just fine with the provided data. It uses about 15MB of RAM which I assume is quite fine.
-
1 Attachment(s)
Re: QGraphicsScene/QGraphicsView performance problems
Here is a stripped down version with no loader and a limited set of redundant functions. I did not want to change the UI part so there are some empty action handling methods, please ignore them.
It draws an area set by "chipsize", divided into fields of size "fieldsize", whuch are in turn divided into subfields of size "subfieldsize", and in each subfield there is one rectangle and one polygon. Chip, field and subfield boundaries are drawn in blue color, while the rectangles and polygons are drawn in black.
Chipsize can range from 50,000 microns up to 125,000 microns, the field size is 800 microns, and subfield size is 100 microns. I kept this sceme to be able to relate to my real needs.
Here, 125,000 micron chipsize is beyond the capacity of any of my machines (1 Mac G5 1GB RAM, and 1 amd64 1GB RAM) while 50,000 micron chipsize can be managed with some patience.
Note that Qt4.3.3 has real problems when zooming in on the amd64, the Qt4.4.0 snapshot is much better. They really must have done something there.
Some logs:
Chipsize 50000.000 um, 3844 fields, 246016 subfields, 246016 rects, 246016 polys
Chipsize 100000.000 um, 15625 fields, 1000000 subfields, 1000000 rects, 1000000 polys
Chipsize 125000.000 um, 24336 fields, 1557504 subfields, 1557504 rects, 1557504 polys
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
wysota
Don't reimplement anything from the scene or the view. Just you the classes provided only changing their properties and adding items to the scene.
Please define "reimplement".
I need to tell you I never took the course in C++ :) so sometimes I have problems with the programming lingo. I just look at examples and try to do the same, and then fill in my own stuff.
It would be great if you could explain with "more words".
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
Please define "reimplement".
"Subclass and rewrite a method which is defined in the base class and has the same signature (return value, name and arguments)".
Use QGraphicsView and QGraphicsScene instead of MyQGraphics... classes.
-
Re: QGraphicsScene/QGraphicsView performance problems
Ok, I think I understand.
But this removes the possibility to add new methods for the class, right?
Now I have a myQGraphicsView member pointing at the coordinate display in the main window, and I give it a value by the method setCoordDisplay(QLineEdit *in);
Please suggest how I should transfer the result form QGraphicsView::mouseMoveEvent to the coordinate display field in the main window using some alternate method.
If new methods can be added, please advice me on how the header should be written.
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
But this removes the possibility to add new methods for the class, right?
You can add new methods, just don't reimplement the ones that already exist and do something - namely drawItems().
-
Re: QGraphicsScene/QGraphicsView performance problems
So you mean that the problem was NOT the new class myQGraphicsView, but the fact that drawItems was re-implemented?
If so, what about mouse*Event methods?
-
Re: QGraphicsScene/QGraphicsView performance problems
I didn't say anything like that. I said you shouldn't start programming by reimplementing methods that do something useful. The base class implementation is probably sufficient for you.
-
Re: QGraphicsScene/QGraphicsView performance problems
I am sorry, but I am getting confused. Let's take this very slowly.
Quote:
Use QGraphicsView and QGraphicsScene instead of MyQGraphics... classes.
You suggest that I should not create a derived class, but instead override the existing methods when needed, such as mouseMoveEvent, mousePressEvent and mouseReleaseEvent, and not override drawItems.
Did I understand this correctly?
I appreciate your patience.
-
Re: QGraphicsScene/QGraphicsView performance problems
Let's close the discussion about "reimplementation", after reading all of your comments again I now understand your point. Thanks for your patience.
-
Re: QGraphicsScene/QGraphicsView performance problems
If you want to override anything, then you need to subclass. The rest is correct.
-
Re: QGraphicsScene/QGraphicsView performance problems
-
Re: QGraphicsScene/QGraphicsView performance problems
Ok, I finally managed to take a look at the app. The example you gave us occupies about 200MB of memory, which is not that bad for 0.5M items ( ~400B per item). The application is horribly slow, but I see some ways to speed it up. I don't know if this will improve the speed significantly, but it's worth to try. I'm going to get rid of your polygon items first.
Edit: Ok, I'm not going to do that, because it'd be quite hard for me as I don't know your code. But I already know the difference should be huge. From what I understand you make each item very big - it's pos() is set to (0,0) and it's rect is defined by the polygon. As you have only four points, there is no point in creating a path from them - you should draw them manualy and return a very tiny bounding rectangle. Furthermore you should make use of the levelOfDetail while painting items - from a big distance you could paint polygons as rects or even just points. It should make a huge difference. And it's not a matter of memory, it's consumption is fine.
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
wysota
Ok, I finally managed to take a look at the app. The example you gave us occupies about 200MB of memory, which is not that bad for 0.5M items ( ~400B per item). The application is horribly slow, but I see some ways to speed it up. I don't know if this will improve the speed significantly, but it's worth to try. I'm going to get rid of your polygon items first.
Edit: Ok, I'm not going to do that, because it'd be quite hard for me as I don't know your code. But I already know the difference should be huge. From what I understand you make each item very big - it's pos() is set to (0,0) and it's rect is defined by the polygon. As you have only four points, there is no point in creating a path from them - you should draw them manualy and return a very tiny bounding rectangle. Furthermore you should make use of the levelOfDetail while painting items - from a big distance you could paint polygons as rects or even just points. It should make a huge difference. And it's not a matter of memory, it's consumption is fine.
Very true.. Level of detail really makes lot of difference.. The chip demo which qt provides is really a nice demo on how graphicsview applications can be optimized atleast to some extent.
Actually your app didn't run on my system(1Gb ram, 3GHz, pentium 4 HT) until i changed chip size to 5k.
But to be frank enough, I personally feel gv framework is overkill. Infact your app didn't run(50k) even when i turned of all possible bells and whistles. Also you don't need those unnecessary memory strain as well.
If you are sure that you don't need to select or move items here and there, definitely implementing a widget is best option. Remember you always don't have to draw everything as drawing everything at < .25 scale is hardly visible.
-
Re: QGraphicsScene/QGraphicsView performance problems
First, I want to thank you for your engagement in this. This is my VERY first Qt project, and surely must have missed and misunderstood a lot of things.
Quote:
Ok, I'm not going to do that, because it'd be quite hard for me as I don't know your code.
You have all of my relevant code in the example. What is missing?
Quote:
But I already know the difference should be huge. From what I understand you make each item very big - it's pos() is set to (0,0) and it's rect is defined by the polygon.
Here it seems I have missed something really important. I was unaware of pos().
Quote:
Furthermore you should make use of the levelOfDetail while painting items - from a big distance you could paint polygons as rects or even just points.
Again. I was unaware of levelofDetail, I will study it. However, the description in Assistant is a bit cryptic, so I will have to test different settings. Will a higher value improve?
Quote:
Gopala:
But to be frank enough, I personally feel gv framework is overkill.
I tend to have the same opinion. But the gv framework is so convenient! But on the other hand, it is the only thing I have looked at so far. Is it as easy to implement scroll and zoom for a regular widget?
I definitely see this on Linux, but for some reason not on my MacOSX. On Linux, displaying the full chip took time, but was ok finally. Zooming in locked it up completely. On Linux, this was eliminated using Qt4.4.0. Or is this a note of a further improvement?
-
Re: QGraphicsScene/QGraphicsView performance problems
First:
Quote:
From what I understand you make each item very big - it's pos() is set to (0,0) and it's rect is defined by the polygon.
This statement puzzles me.
I just do
Code:
QVector<QPointF> Tpz(4);
<fill in the 4 points>
scene->addPolygon(Trapezoid,AreaShotPen);
Where does the pos() come in?
Secondly, can you apply levelofDetail to a high-level draw such as scene->addRect();?
Or do you have to create a custom QGraphicsItem and apply it to painter->drawRect() to do that?
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Secondly, can you apply levelofDetail to a high-level draw such as scene->addRect();?
Or do you have to create a custom QGraphicsItem and apply it to painter->drawRect() to do that?
You have to create a custom item. Have a look at the code for demos/chip/chip.cpp for reference.
-
Re: QGraphicsScene/QGraphicsView performance problems
BTW can you explain the composition of a chip with a screen shot ? i think you can simplify the drawing by representing less items which draw sub polygons.
I mean I could see an array of rectangular items, with some polygons and rect inside.
The outside rect can be considered as one item.
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
You have all of my relevant code in the example. What is missing?
Knowledge what items represent. I don't want to go through all your code and learn it by trial and error. For example I don't know if you really need 4 points to represent a trapezoid or if those trapezoids have somehow "fixed" properties, for example always a 90deg. angle - then you just need 3 points, or fixed lengths.
Quote:
However, the description in Assistant is a bit cryptic, so I will have to test different settings. Will a higher value improve?
It is nothing you may set. It's a value given by the view to the item's painting routine that tells it how far away the "camera" is from the item. The further, the less details may be seen, so drawing those details becomes obsolete.
Quote:
I tend to have the same opinion. But the gv framework is so convenient!
I don't think it is an overkill. If you design the scene correctly, it will work fine. With a regular widget you'd probably want to have items as well.
Quote:
Originally Posted by
bnilsson
Where does the pos() come in?
It's set to (0,0) by default. The way you do it makes your items very complicated, especially if you'll want later to interact with them somehow. If you have a trapezoid, make it span from (0,0) to the width and height of the trapezoid and them move it into an appropriate position using setPos(). This probably won't yield any performance gain, but will it easier to implement the ShapeItem that doesn't use paths or polygons that are really slow to draw.
Quote:
Secondly, can you apply levelofDetail to a high-level draw such as scene->addRect();?
The rectangle item probably doesn't use LOD, so no. You have to implement your own item for that. It is most essential for your trapezoids. Simplify things wherever possible.
-
Re: QGraphicsScene/QGraphicsView performance problems
This thought has occurred to me, since the pattern is organized in exposure fields and subfields. So the items could be a FieldItem with child SubFieldsItem, which in turn has child ShapeItems. The ShapeItems are the actual stuff that are drawn by my microchip exposure machine which is using the data file as input, and the fields and subfields are placement parameters.
Will this item hierarchy in three levels, if implemented, give any performance gain?
The field content is usually unique for each field, so there is no memory to be gained by repetition. However, if the field and subfield boundaries are to be drawn, these items can be re-used by offset.
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Knowledge what items represent. I don't want to go through all your code and learn it by trial and error. For example I don't know if you really need 4 points to represent a trapezoid or if those trapezoids have somehow "fixed" properties, for example always a 90deg. angle - then you just need 3 points, or fixed lengths.
Ok, now I understand.
I will have the following kinds of objects to draw:
FieldBoundary:
This object is 'virtual', and the drawing of this is optional, to indicate for the viewer how the pattern layout is divided into fields.
It is always rectangular (special case quadratic), the same size and shape throughout the whole drawing. Typically 800 by 800 micrometer in size. The size is predefined in the file header, and the pattern data comes as position x,y referenced from the upper left corner of the chip.
SubFieldBoundary:
This object is 'virtual', and the drawing of this is optional, to indicate for the viewer how the pattern layout in the field is divided into subfields.
It is always rectangular (special case quadratic), the same size and shape throughout the whole drawing. The subfield size is ALWAYS smaller or equal to the field size, typically 100 by 100 microns in size. The size is predefined in the file header, and the pattern data comes as position x,y referenced from the upper left corner of the current field.
The Real shapes below are ALWAYS contained and enclosed within one subfield, and thus always smaller.
XRectangle:
Should always be drawn. The X side is longer than the Y side. The input data comes as x0,y0,w,h, referenced from the upper left corner of the current subfield.
YRectangle:
Should always be drawn. The Y side is longer than the X side. The input data comes as x0,y0,w,h, referenced from the upper left corner of the current subfield.
XTrapezoid:
Top and bottom sides parallell. Degenerated into triangles may occur. The input data comes as x0,y0,x1,x2,x3,y3, referenced from the upper left corner of the current subfield.
YTrapezoid:
Left and right sides parallell. Degenerated into triangles may occur. The input data comes as x0,y0,y1,y2,x3,y3, referenced from the upper left corner of the current subfield.
Line:
Just a line from x0,y0 to x1,y1, referenced from the upper left corner of the current subfield.
Exposure dose index:
In the input data file, each Real shape may have a tag to control the exposure dose. Here we us colors (up to 63 colors) to present this.
Hope this helps.
-
Re: QGraphicsScene/QGraphicsView performance problems
About LOD:
Quote:
t is nothing you may set. It's a value given by the view to the item's painting routine that tells it how far away the "camera" is from the item. The further, the less details may be seen, so drawing those details becomes obsolete.
So, why discuss it?:confused:
Or is there any way I can make use of it?
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
About LOD:
So, why discuss it?:confused:
Or is there any way I can make use of it?
Ofcourse!
Snippet from chip demo is below. I have marked some lines with "==" comment.
See how the chip alters what exactly is drawn when level of detail is so and so.
When levelOfDetail is less, even though you draw everything, the user won't be able to see the full details of the chip so, why bother to draw everything ?
Code:
{
Q_UNUSED(widget);
QColor fillColor
= (option
->state
& QStyle::State_Selected) ? color.
dark(150) : color;
if (option
->state
& QStyle::State_MouseOver) fillColor = fillColor.light(125);
//============Notice here============
if (option->levelOfDetail < 0.2) {
//===============================
if (option->levelOfDetail < 0.125) {
painter
->fillRect
(QRectF(0,
0,
110,
70), fillColor
);
return; // also notice the return===========
}
painter
->setPen
(QPen(Qt
::black,
0));
painter->setBrush(fillColor);
painter->drawRect(13, 13, 97, 57);
return;//========= notice this too
}
QPen oldPen
= painter
->pen
();
int width = 0;
if (option
->state
& QStyle::State_Selected) width += 2;
pen.setWidth(width);
painter
->setBrush
(QBrush(fillColor.
dark(option
->state
& QStyle::State_Sunken ?
120 : 100)));
painter
->drawRect
(QRect(14,
14,
79,
39));
if (option->levelOfDetail >= 1) {
painter
->setPen
(QPen(Qt
::gray,
1));
painter->drawLine(15, 54, 94, 54);
painter->drawLine(94, 53, 94, 15);
painter
->setPen
(QPen(Qt
::black,
0));
}
// Draw text
if (option->levelOfDetail >= 2) {
font.
setStyleStrategy(QFont::ForceOutline);
painter->setFont(font);
painter->save();
painter->scale(0.1, 0.1);
painter
->drawText
(170,
180,
QString("Model: VSC-2000 (Very Small Chip) at %1x%2").
arg(x
).
arg(y
));
painter
->drawText
(170,
200,
QString("Serial number: DLWR-WEER-123L-ZZ33-SDSJ"));
painter
->drawText
(170,
220,
QString("Manufacturer: Chip Manufacturer"));
painter->restore();
}
// Draw lines
QVarLengthArray<QLineF, 36> lines;
if (option->levelOfDetail >= 0.5) {
for (int i = 0; i <= 10; i += (option->levelOfDetail > 0.5 ? 1 : 2)) {
lines.
append(QLineF(18 + 7 * i,
13,
18 + 7 * i,
5));
lines.
append(QLineF(18 + 7 * i,
54,
18 + 7 * i,
62));
}
for (int i = 0; i <= 6; i += (option->levelOfDetail > 0.5 ? 1 : 2)) {
lines.
append(QLineF(5,
18 + i
* 5,
13,
18 + i
* 5));
lines.
append(QLineF(94,
18 + i
* 5,
102,
18 + i
* 5));
}
}
if (option->levelOfDetail >= 0.4) {
};
lines.append(lineData, 6);
}
painter->drawLines(lines.data(), lines.size());
// Draw red ink
if (stuff.size() > 1) {
painter
->setPen
(QPen(Qt
::red,
1, Qt
::SolidLine, Qt
::RoundCap, Qt
::RoundJoin));
painter->setBrush(Qt::NoBrush);
path.moveTo(stuff.first());
for (int i = 1; i < stuff.size(); ++i)
path.lineTo(stuff.at(i));
painter->drawPath(path);
}
}
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
Ok, now I understand.
I will have the following kinds of objects to draw:
FieldBoundary:
This object is 'virtual', and the drawing of this is optional, to indicate for the viewer how the pattern layout is divided into fields.
It is always rectangular (special case quadratic), the same size and shape throughout the whole drawing. Typically 800 by 800 micrometer in size. The size is predefined in the file header, and the pattern data comes as position x,y referenced from the upper left corner of the chip.
SubFieldBoundary:
This object is 'virtual', and the drawing of this is optional, to indicate for the viewer how the pattern layout in the field is divided into subfields.
It is always rectangular (special case quadratic), the same size and shape throughout the whole drawing. The subfield size is ALWAYS smaller or equal to the field size, typically 100 by 100 microns in size. The size is predefined in the file header, and the pattern data comes as position x,y referenced from the upper left corner of the current field.
The Real shapes below are ALWAYS contained and enclosed within one subfield, and thus always smaller.
XRectangle:
Should always be drawn. The X side is longer than the Y side. The input data comes as x0,y0,w,h, referenced from the upper left corner of the current subfield.
YRectangle:
Should always be drawn. The Y side is longer than the X side. The input data comes as x0,y0,w,h, referenced from the upper left corner of the current subfield.
XTrapezoid:
Top and bottom sides parallell. Degenerated into triangles may occur. The input data comes as x0,y0,x1,x2,x3,y3, referenced from the upper left corner of the current subfield.
YTrapezoid:
Left and right sides parallell. Degenerated into triangles may occur. The input data comes as x0,y0,y1,y2,x3,y3, referenced from the upper left corner of the current subfield.
Line:
Just a line from x0,y0 to x1,y1, referenced from the upper left corner of the current subfield.
Exposure dose index:
In the input data file, each Real shape may have a tag to control the exposure dose. Here we us colors (up to 63 colors) to present this.
Hope this helps.
May be you can have only one item to represent the field and its contents and by contents i don't mean child items.
I am using the terminology "item" to represent an entity on scene and which inherits QGraphicsItem.
The field item is the item responsible to draw its subfields and contents of subfield.
Now if levelOfDetail is very less, just draw the outer boundary of field item only.
If levelOfDetail is less, but not very less, then draw subfield boundary also.
Only if levelOfDetail is greater enough so that user can see detail, draw all the shapes.
Again you can finetune more by drawing rect when level of detail is medium and polygon if levelOfDetail is sufficiently large.
And for the values of levelOfDetail, you have to experiment.
Edit:
Quote:
Chipsize 50000.000 um, 3844 fields, 246016 subfields, 246016 rects, 246016 polys
Chipsize 100000.000 um, 15625 fields, 1000000 subfields, 1000000 rects, 1000000 polys
Chipsize 125000.000 um, 24336 fields, 1557504 subfields, 1557504 rects, 1557504 polys
As you can see considering worst case, you can represent all your data by 24336 field items (instead of 24336 + 1557504 + 1557504 items! ) and chip demo has 50k items!!
I think your app can be smooth enough if you design it carefully :)
All the best :)
-
Re: QGraphicsScene/QGraphicsView performance problems
Quote:
Originally Posted by
bnilsson
FieldBoundary:
This object is 'virtual', and the drawing of this is optional, to indicate for the viewer how the pattern layout is divided into fields.
It is always rectangular (special case quadratic), the same size and shape throughout the whole drawing. Typically 800 by 800 micrometer in size. The size is predefined in the file header, and the pattern data comes as position x,y referenced from the upper left corner of the chip.
This is nice. A rectangle which is child of the scene. Coordinates them come relative to the scene.
Quote:
SubFieldBoundary:
This object is 'virtual', and the drawing of this is optional, to indicate for the viewer how the pattern layout in the field is divided into subfields.
It is always rectangular (special case quadratic), the same size and shape throughout the whole drawing. The subfield size is ALWAYS smaller or equal to the field size, typically 100 by 100 microns in size. The size is predefined in the file header, and the pattern data comes as position x,y referenced from the upper left corner of the current field.
A rectangle which is child of the field item. The coordinates then come relative to the field.
Quote:
XRectangle:
Should always be drawn. The X side is longer than the Y side. The input data comes as x0,y0,w,h, referenced from the upper left corner of the current subfield.
Same as above.
Quote:
YRectangle:
Should always be drawn. The Y side is longer than the X side. The input data comes as x0,y0,w,h, referenced from the upper left corner of the current subfield.
Same here.
Quote:
XTrapezoid:
Top and bottom sides parallell. Degenerated into triangles may occur. The input data comes as x0,y0,x1,x2,x3,y3, referenced from the upper left corner of the current subfield.
Identified as four points. Child of the subfield. Coordinates relative to the subfield.
Quote:
YTrapezoid:
Left and right sides parallell. Degenerated into triangles may occur. The input data comes as x0,y0,y1,y2,x3,y3, referenced from the upper left corner of the current subfield.
Same here.
Quote:
Line:
Just a line from x0,y0 to x1,y1, referenced from the upper left corner of the current subfield.
Child of subfield item.
To sum things up:
You need the following items:
* Field - representing Field and Subfield
* Rectangle - representing X and Y rectangles
* Trapezoid - representing X and Y trapezoids
* Line - representing lines, obviously
These together will make it easier to manipulate all objects. All coordinates will be relative to their parents thus easier to calculate.
-
Re: QGraphicsScene/QGraphicsView performance problems
Thanks both of you for two interesting suggestions!
I am sure I will have more questions shortly!
-
Re: QGraphicsScene/QGraphicsView performance problems
I have tried to apply the idea of have Fields as the GraphicsItems to be added to the scene, and have the contents of the Field drawn "manually" in the paint method if the GraphicsItem. The result is very promising, I have no apparent memory problems anymore, and when I apply levelOfDetail it is also reasonably fast.
I store the contents of the field using a QList within the QGraphicsItem Field object. (Is this the best way?)
A few questions remain for me:
1) The first draw is always updated twice, maybe even three times. Is there any way to control this?
2) QGraphicsView keeps track of which Fields to update, whith the knowledge of their bounding boxes and if they are within the field of view. But when zooming in within a Field it is my own responsibility to decide what to draw (inside or outside the view), and for this I need to know the current bounding box of my visible view in the scene context. What methds can I use to get this information?
3) What is fastest to draw, four QLine-s or a QPolygon?