Continue with the previous content today. Now that we have entered the 2D plot, let's continue to study the QPainter stuff! Anti-sample is an important concept in graphics to prevent the appearance of the phenomenon of "sawtooth. Many system plotting APIs have built-in anti-sample algorithms. However, by default, they are usually disabled, and Qt is no exception. Let's take a look at the code. This Code only provides the paintEvent function, and you can easily replace the relevant code in the previous chapter. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif "/> void PaintedWidget: paintEvent (QPaintEvent * event)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif "/> QPainter painter (this );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif"/> painter. setPen (QPen (Qt: black, 5, Qt: DashDotLine, Qt: RoundCap ));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif "/> painter. setBrush (Qt: yellow );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif "/> painter. drawEllipse (50,150,200,150 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif "/> painter. setRenderHint (QPainter: Antialiasing, true );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif"/> painter. setPen (QPen (Qt: black, 5, Qt: DashDotLine, Qt: RoundCap ));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif "/> painter. setBrush (Qt: yellow );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif "/> painter. drawEllipse (300,150,200,150 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif "/>} Check the effect after running: 650) this. width = 650;" onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1RI63935-12.png "/> The reverse sample technology is not used on the left, and the reverse sample technology is used on the right. The difference between the two can be easily seen. Next let's take a look at the relevant code. To try the paint brush style, a new paint brush: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif "/> Painter. setPen (QPen (Qt: black, 5, Qt: DashDotLine, Qt: RoundCap); the first parameter is the paint color, set it to black. The second parameter is the width of the paint brush, Which is 5px. The third parameter is the paint brush style. We use DashDotLine, as shown in the same name, it is a short-term and a point-to-point type; the fourth is a RoundCap, that is, a circular pen cap. Then we fill the image with a yellow paint brush and draw an ellipse. The next one is very similar to the previous one. The only difference is that there is an additional 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1RIBC0-0.gif "/> Painter. setRenderHint (QPainter: Antialiasing, true); but this sentence is clear, that is, set the attribute of Antialiasing to true. If you have learned graphics, you will know that this long word is "reverse sample ". After this setting, we enable the QPainter anti-sample function. I still remember that we once said that QPainter is a state machine. Therefore, as long as we open it here, all the code will be drawn in a reversed way. Here, you will find that the anti-spam effect is actually much better than the non-spam effect, so why not enable anti-spam by default? This is because anti-image processing is a complicated algorithm. In some applications that do not require high image quality, anti-image processing is not required. In order to improve efficiency, General graphics rendering systems, such as Java2D and OpenGL, do not reverse sample by default. There is another question: Since the image quality is much higher than that of the image without anti-image, what is the role of the image without anti-image? We have mentioned that in some environments with low image quality requirements or performance constraints, such as embedded and mobile phone environments, there is no need to reverse the sample. In addition, in some applications that require precise pixel operations, there is no backsample. See the following figure: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1RIB353-15.png "/> It is a 1-pixel point drawn using Photoshop pencil and paint brush tools that is scaled down to a 3200% view. The Pencil tool in Photoshop does not reverse the image, but the paint brush needs to reverse the image. When the image is zoomed in, you will know that if there is a backsample, the operation cannot be accurate to 1 pixel. This is because it is difficult for you to control the image to 1 pixel. This is not a defect of the Photoshop paint brush tool, but a problem of the anti-sample algorithm. If you want to know why, please refer to the anti-sample principle section in computer graphics.
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/237447