Qt learning path (24): QPainter

Source: Internet
Author: User

More people support me! Some friends also suggested that the previous event-related tutorials lack examples. Because the event is hard to be used as an example, I didn't write it, but I just wrote about it. What we bring today is a new part about the 2D plot of Qt. This part is better understood than the previous content! Therefore, the examples will also be added. Someone asked Bean what to do with Qt. In fact, bean is working on a Qt drawing program and striving to develop towards Photoshop and GIMP. But after all, this will take a long time and a difficult journey, so it is also open-source on the Internet. If you are interested, you can try it... Well, let's talk less about it. Let's continue learning! Qt's drawing system allows you to draw on screen and print devices using the same API. The entire plotting system is based on three classes: QPainter, QPainterDevice, and QPaintEngine. QPainter is used to perform the painting operation. QPaintDevice is an abstraction of a two-dimensional space, which can be drawn by QPainter; QPaintEngine provides a unified interface for painter painting on different devices. The QPaintEngine class is used between QPainter and QPaintDevice and is usually transparent to developers. Unless you need to customize a device, you must redefine QPaintEngine. Given the hierarchy between the three classes (from the Qt API documentation): 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" QPainter "src =" http://img1.51cto.com/attachment/200911/200911261259240513375.png "/> The main benefit of this implementation is that all plotting follows the same drawing process. In this way, adding new features can be easily added, you can also add a default Implementation Method for unsupported features. In addition, Qt provides an independent QtOpenGL module that allows you to use OpenGL in Qt applications. This module provides an OpenGL module that can be used like other Qt components. The difference is that it uses OpenGL as the display technology and OpenGL functions for rendering. We will introduce this component later. Based on the previous introduction, we know that the Qt plotting system is actually to say that QPainter is used to draw on QPainterDevice, and QPaintEngine is used to communicate between them. Now let's take a look at how to use QPainter. First, we define a component, which is similar to the previous definition: 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/1RRL539-1.gif "/> Class PaintedWidget: public QWidget
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/1RRL539-1.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/1RRL539-1.gif "/> public:
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/1RRL539-1.gif "/> PaintedWidget ();
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/1RRL539-1.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/1RRL539-1.gif "/> protected:
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/1RRL539-1.gif "/> void 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/1RRL539-1.gif "/>}; here we define only one constructor and redefine the paintEvent () function. It can be seen from the name that this is actually an Event Callback Function. Note that, in general, the event functions of Qt are all protected. Therefore, if you want to rewrite the event, you need to inherit the class. As for the event-related things, we have already described them in detail in the previous section, so we will not repeat them here. The constructor is mainly a definition of some sizes and so on, here is not detailed description: 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/1RRL539-1.gif "/> PaintedWidget: PaintedWidget ()
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/1RRL539-1.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/1RRL539-1.gif "/> resize (800,600 );
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/1RRL539-1.gif "/> setWindowTitle (tr (" Paint Demo "));
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/1RRL539-1.gif"/>} we are concerned with the implementation of the paintEvent () function: 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/1RRL539-1.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/1RRL539-1.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/1RRL539-1.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/1RRL539-1.gif "/> painter. drawLine (80,100,650,500 );
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/1RRL539-1.gif "/> painter. setPen (Qt: red );
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/1RRL539-1.gif "/> painter. drawRect (10, 10,100,400 );
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/1RRL539-1.gif "/> painter. setPen (QPen (Qt: green, 5 ));
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/1RRL539-1.gif "/> painter. setBrush (Qt: blue );
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/1RRL539-1.gif "/> painter. drawEllipse (50,150,400,200 );
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/1RRL539-1.gif"/>} to run our program, the following is the main () function: 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/1RRL539-1.gif "/> int main (int argc, char * argv [])
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/1RRL539-1.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/1RRL539-1.gif "/> QApplication app (argc, argv );
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/1RRL539-1.gif "/> PaintedWidget w;
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/1RRL539-1.gif "/> w. show ();
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/1RRL539-1.gif "/> return app.exe c ();
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/1RRL539-1.gif"/>} run the result as follows: 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/1RRI5J-31.png "/> first, we declare a QPainter object. Note: We have created an object in the stack space of this function, so we do not need to delete it. QPainter receives a QPaintDevice * type parameter. QPaintDevice has many sub-classes, such as QImage and QWidget. Remember, QPaintDevice can be understood as where to draw, and now we want to draw on this widget, so the input is the this pointer. QPainter has many functions starting with draw for drawing various graphics, such as drawLine, drawRect, and drawEllipse. For specific parameters, see the API documentation. An example of the QPainter draw function is provided. The figure below is from C ++ GUI Programming with Qt4, 2nd Edition. 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/1RRI409-32.png"/> well, come here today and we will continue with this paintEvent () function Description.

This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/235332

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.