PDA

View Full Version : Error after putting in QGraphicView Codes



020394
5th September 2013, 05:07
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
terminate called recursively
D:\project1\Qt\build-QtCreatorMP-Desktop-Debug\debug\QtCreatorMP exited with code 3


I am getting this errors after putting QGraphicsView code into it

h file


private:
QGraphicsScene *scene;
QGraphicsRectItem*rectangle;
QGraphicsLineItem*line;

cpp file

scene=new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QBrush yelloBrush(Qt::yellow);
QBrush magentaBrush(Qt::magenta);
QBrush greenBrush(Qt::green);

QPen Linepen(Qt::black);
Linepen.setWidth(2);




//BASAL
rectangle=scene->addRect(50,220,100,-50,Linepen,magentaBrush);//LAST 1st HOUR BAR CHART
rectangle=scene->addRect(200,220,100,-50,Linepen,magentaBrush);//LAST 2nd HOUR BAR CHART
rectangle=scene->addRect(350,220,100,-50,Linepen,magentaBrush);//LAST 3th HOUR BAR CHART
rectangle=scene->addRect(500,220,100,-50,Linepen,magentaBrush);//LAST 4TH HOUR BAR CHART
rectangle=scene->addRect(650,220,100,-50,Linepen,magentaBrush);//LAST 5th HOUR BAR CHART
//BASAL

//PATIENT
//TO ADD ON TOP OF THE GRAPH ,BREAL Y +Y OF BASAL
rectangle=scene->addRect(50,220-50,100,-50,Linepen,yelloBrush);//LAST 1st HOUR BAR CHART
rectangle=scene->addRect(200,220-50,100,-50,Linepen,yelloBrush);//LAST 2nd HOUR BAR CHART
rectangle=scene->addRect(350,220-50,100,-50,Linepen,yelloBrush);//LAST 3th HOUR BAR CHART
rectangle=scene->addRect(500,220-50,100,-50,Linepen,yelloBrush);//LAST 4TH HOUR BAR CHART
rectangle=scene->addRect(650,220-50,100,-50,Linepen,yelloBrush);//LAST 5th HOUR BAR CHART
//PATIENT

//CLINICIAN
//TO ADD ON TOP OF THE GRAPH ,BREAL Y +Y OF BASAL AND CLINICIAN
rectangle=scene->addRect(50,220-50-50,100,-50,Linepen,greenBrush);//LAST 1st HOUR BAR CHART
rectangle=scene->addRect(200,220-50-50,100,-50,Linepen,greenBrush);//LAST 2nd HOUR BAR CHART
rectangle=scene->addRect(350,220-50-50,100,-50,Linepen,greenBrush);//LAST 3th HOUR BAR CHART
rectangle=scene->addRect(500,220-50-50,100,-50,Linepen,greenBrush);//LAST 4TH HOUR BAR CHART
rectangle=scene->addRect(650,220-50-50,100,-50,Linepen,greenBrush);//LAST 5th HOUR BAR CHART
//CLINICIAN
line=scene->addLine(0,220,790,220,Linepen);//x axis
line=scene->addLine(790,220,780,215,Linepen);//> for x axis
line=scene->addLine(790,220,780,225,Linepen);//> for x axis

line=scene->addLine( 20,0,20,240,Linepen);//y axis
line=scene->addLine(20,0,15,10,Linepen);//> for y axis
line=scene->addLine(20,0,25,10,Linepen);//> for y axis


line=scene->addLine( 15,215,25,215,Linepen);//y Coordinate (1)
line=scene->addLine( 15,210,25,210,Linepen);//y Coordinate (2
line=scene->addLine( 15,205,25,205,Linepen);//y Coordinate (3)
line=scene->addLine( 15,200,25,200,Linepen);//y Coordinate (4)
line=scene->addLine( 15,195,25,195,Linepen);//y Coordinate (5)
line=scene->addLine( 15,190,25,190,Linepen);//y Coordinate (6)
line=scene->addLine( 15,185,25,185,Linepen);//y Coordinate (7)
line=scene->addLine( 15,180,25,180,Linepen);//y Coordinate (8)
line=scene->addLine( 15,175,25,175,Linepen);//y Coordinate (9)

line=scene->addLine( 10,170,30,170,Linepen);//y Coordinate (10)
line=scene->addLine( 15,165,25,165,Linepen);//y Coordinate (11)
line=scene->addLine( 15,160,25,160,Linepen);//y Coordinate (12
line=scene->addLine( 15,155,25,155,Linepen);//y Coordinate (13)
line=scene->addLine( 15,150,25,150,Linepen);//y Coordinate (14)
line=scene->addLine( 15,145,25,145,Linepen);//y Coordinate (15)
line=scene->addLine( 15,140,25,140,Linepen);//y Coordinate (16)
line=scene->addLine( 15,135,25,135,Linepen);//y Coordinate (17)
line=scene->addLine( 15,130,25,130,Linepen);//y Coordinate (18)
line=scene->addLine( 15,125,25,125,Linepen);//y Coordinate (19)

line=scene->addLine( 10,120,30,120,Linepen);//y Coordinate (20)
line=scene->addLine( 15,115,25,115,Linepen);//y Coordinate (21)
line=scene->addLine( 15,110,25,110,Linepen);//y Coordinate (22)
line=scene->addLine( 15,105,25,105,Linepen);//y Coordinate (23)
line=scene->addLine( 15,100,25,100,Linepen);//y Coordinate (24)
line=scene->addLine( 15,95,25,95,Linepen); //y Coordinate (25)
line=scene->addLine( 15,90,25,90,Linepen); //y Coordinate (26)
line=scene->addLine( 15,85,25,85,Linepen); //y Coordinate (27)
line=scene->addLine( 15,80,25,80,Linepen); //y Coordinate (28)
line=scene->addLine( 15,75,25,75,Linepen); //y Coordinate (29)
line=scene->addLine( 10,70,30,70,Linepen); //y Coordinate (30)

line=scene->addLine( 15,65,25,65,Linepen); //y Coordinate (31)
line=scene->addLine( 15,60,25,60,Linepen); //y Coordinate (32)
line=scene->addLine( 15,55,25,55,Linepen); //y Coordinate (33)
line=scene->addLine( 15,50,25,50,Linepen); //y Coordinate (34)
line=scene->addLine( 15,45,25,45,Linepen); //y Coordinate (35)
line=scene->addLine( 15,40,25,40,Linepen); //y Coordinate (36)
line=scene->addLine( 15,35,25,35,Linepen); //y Coordinate (37)
line=scene->addLine( 15,30,25,30,Linepen); //y Coordinate (38)
line=scene->addLine( 15,25,25,25,Linepen); //y Coordinate (39)
line=scene->addLine( 10,20,30,20,Linepen); //y Coordinate (40)


yText0=scene->addText("0");
yText0->setPos(7,215);


I Put the above cpp codes inside a timer(1000) which runs every sec . I am doing this as in the future i am going to change the height to a variable which will get data from a database and set the height of it . That why i use the timer so it can keep updating . Anyway to solve this ?

ChrisW67
5th September 2013, 05:23
Your program is exhausting your system's memory. Start with the observation that you are creating a whole graphics scene repeatedly and this is not being cleaned up.

020394
5th September 2013, 05:37
I see so what should i do ? Does the problem lies with the QGraphicView ?

ChrisW67
5th September 2013, 05:38
No, the problem is not in Qt. Fix your code. We cannot see that, you can.

020394
5th September 2013, 05:50
I check the processes my program took up 1.6++ gb mem . I check my codes. I implemented the graphic view in a timer. guess that causes it to keep running and adding to the memory . Is there a way to like reset the graphic view and call it back again ?

Added after 6 minutes:

95329533


The above are my codes, feel free to take a look .

ChrisW67
5th September 2013, 06:01
Your program is creating a new scene graph every millisecond (or thereabouts) as a result of the timer expiring. You never delete the old one... you exhaust system memory rather quickly.

The other timer (the one that actually updates a clock display) is probably pegging your CPU at about 100% busy-polling when that is entirely unnecessary.

020394
5th September 2013, 06:08
I see , I tried using a 1sec timer , The memory is increasing slower than before . It is ok to put the QGraphic codes in a timer right ? Is it ok to keep creating a new scene graph using a timer ? How to delete the old one ?

ChrisW67
5th September 2013, 08:06
Is it ok to keep creating a new scene graph using a timer ?
Technically fine. Probably not sane though. Ask yourself, "Why am I creating a whole new graphical display every second?" Typical usage would be to construct the scene once and manipulate it as time goes by.

How to delete the old one ?
The C++ delete operator or scene->deleteLater(). Fix the busy polling if you want deleteLater() to function well.

020394
6th September 2013, 05:01
I see . i was thinking of that too , but i tried to put it out of the timer but the graph would not update efficiently , need to switch to another tab and back in order to see the change . For the deleteLater(), should i put it in another timer to delete the scene every 2-5 sec ?

Added after 1 46 minutes:

Ok i tried to solve it , not sure whether it is the right way . I added a scene->update() inside the same timer and create the new scene graph outside the timer .

020394
6th September 2013, 10:23
The C++ delete operator or scene->deleteLater(). Fix the busy polling if you want deleteLater() to function well.
I tried using deletelater() but it seems there is a problem , the program seems to stop working . You said about Busy polling , how to do that? I tried to use destroyed() on my scene/ui->graphicview but it seems that it does not work as the memory of the process is still running .

Added after 1 15 minutes:

Besides using the deletelater(), I seem to find another method not sure whether I did it correctly . I used scene->clear() and put it in the timer . then later sceneupdate() after that to update the ones i need to show .