You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Friday, January 20th 2012, 5:52pm

Grid painted in GraphicsScene drawBackground causes artifacts(?)

Hi there,
I did a lot of researching but couldn't find a solution that works, so I hope you'll be able to help me :)
First of all a picture, then the code:



Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
void AsaGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect){
  
  QColor BaseColor(250,250,250);
  QColor LightGrey1(220,220,220);
  QColor LightGrey2(220,220,220);
  QColor MiddleGrey(200,200,200);
  QColor DarkGrey(180,180,180); 
  
  
  int k=1; //// if zoomed to a certain scale, 'k' is used to produce new lines 

  if (ScaleFactor>2){
	k=2;
	LightGrey2.setRgb(200,200,200);
	MiddleGrey.setRgb(160,160,160);
	DarkGrey.setRgb(120,120,120);
  }
  
  const int gridSize = 10*ScaleFactor/k;
  qreal left = int(rect.left()) - ( int(rect.left()) % (gridSize*10) );
   qreal bottom = int(rect.bottom() )  - ( int(rect.bottom()) %  (gridSize*10) )   +  int(this->height())%(gridSize*10) + gridSize*10;
  
  QVarLengthArray<QLineF, 100> linesVert;
  QVarLengthArray<QLineF, 100> linesHor;
  
  for (qreal x = left; x < rect.right(); x += gridSize)
	linesVert.append(QLineF(x, rect.top(), x, rect.bottom()));
  for (qreal y = bottom; y > rect.top(); y -= gridSize)
	linesHor.append(QLineF(rect.left(), y, rect.right(), y));
  
  

  
  

   painter->fillRect(rect, BaseColor );
   for (int i=0; i<linesVert.size(); i++){
 	if(i%(1*k)==0){
  	if( i%(5*k)==0){
	if( i%(10*k)==0)
	  painter->setPen(DarkGrey);
	else
	  painter->setPen(MiddleGrey); 
  	}
  	else painter->setPen(LightGrey2);
 	}
 	else
   	painter->setPen(LightGrey1);
	painter->drawLine(linesVert[i]);
   }
   painter->setPen(BaseColor);	 	//
   painter->drawLine(linesVert[0]); // remove Line at x=0
   
   
   for (int i=0; i<linesVert.size(); i++){
 	if(i%(1*k)==0){
  	if( i%(5*k)==0){
	if( i%(10*k)==0)
	  painter->setPen(DarkGrey);
	else
	  painter->setPen(MiddleGrey); 
  	}
  	else painter->setPen(LightGrey2);
 	}
 	else
   	painter->setPen(LightGrey1);
	painter->drawLine(linesHor[i]);
   }
  

}



Thoses lines appear if I scroll trough the scene, if I add Widgets to the scene (in most cases the lines go to a node which was added before) and if I zoom/scale the scene.
I tried

Source code

1
2
  	this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  	this->setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);

and also I update the scene after I release the scenes scrollbars. But it keeps happening. I doubt that it comes from performance reasons or from the drawBackground function, but... well I have no idea how to make sure where it comes from.

2

Friday, January 20th 2012, 6:19pm

sorry, whats the problem? The light grey diagonals?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Friday, January 20th 2012, 9:22pm

yeah :)

4

Friday, January 20th 2012, 10:01pm

well, if your 'drawBackground' method doesnt draw them, then it's not much good showing ONLY that method ;)

What happens if you draw ONLY the bacground?
What happens if you draw NO background?

You say that happens when you add an item to the scene - lets see that code.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

5

Friday, January 20th 2012, 10:27pm

:P
I doubt that 'drawBackground' draws them, but I'm not sure at all, since this artifacts also appear if there are no Items added to the scene.
So: If I draw ONLY the background it also happens,
if I draw NO background, it's alright

6

Saturday, January 21st 2012, 12:16am

your drawbackground method asserts on me.

your last line painter->drawLine(linesHor);

linesHor is too small - you are comparing i to linesvert size, but looping for linesHor

When do you call drawbackground? if you are scrolling at the same time, that's probably your problem.

I feel it is time to regurgitate my sig - you need to post a compilable example.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 3 times, last edit by "Amleto" (Jan 21st 2012, 12:42am)


7

Saturday, January 21st 2012, 11:19am

hmmm what does 'asserts on me' mean? :P
Omg I feel sooo stupid. That worked. changing linesVert to linesHor (was a mistake of course - I should stop copying code -.-) worked perfekt. I am soooo relieved :) Woke up half an hour ago and this is already the best moment of my day ^^ (but also I'm ashamed -.-) Thank you very much!!

8

Saturday, January 21st 2012, 1:02pm

If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.