Qt qcustomplot plot (1) configuration and the first example

Source: Internet
Author: User

Recently, a project developed with QT needs to draw coordinate curves. I used the qcustomplot plug-in under the guidance of a teacher. It is easy to use and has good functions.

However, I found a lot of materials and blogs on the Internet, but I only demonstrated the examples provided by the official team. I didn't have comprehensive learning materials in the system, therefore, I decided to share some development instances and experiences I had gained during the exploration process and share them with you for further study and exchange. Please kindly advise.

I. qcustomplot Installation

Http://www.qcustomplot.com/index.php/download

We recommend that you download the first link, which contains many official examples for reference.

 

Decompress the downloaded file to any folder without a Chinese path.

Open QT and create a new form project

Copy and paste the header file qcustomplot. h and source file qcustomplot. cpp in the decompressed folder to the project folder.

In QT, right-click the project name, add existing files, and add the header file qcustomplot. h and source file qcustomplot. cpp to the project.

Add printsupport at the end of line 2 of the Pro file of the project, as shown in figure

Open the interface file, enter the graphical design interface, add a widget area to the main window, right-click the added widget area, and select the "upgrade to" button.

Enter "qcustomplot" for the promotion class name and click "add.

Select qcustomplot in the following interface and click the Upgrade button. The created widget is upgraded to the qcustomplot class.

Now let's run the program and a simple coordinate system will appear.

Congratulations! The basic configuration of qcustomplot is ready.

Ii. Example 1

First, explain the principle:

We can understand that qcustomplot is a drawing board class, which inherits from widgets, And the widget class in the interface can be upgraded to qcustomplot for plotting.

Every curve in qcustomplot is a graph object. Any curve related to displaying data is operated on graph or the method provided by Graph object is called.

There are four axes in a qcustomplot, xaxis and yaxis are the X and Y axes we see, and the two axes xaxis1 and yaxis1 are the X and Y coordinates of the top and right sides, which are hidden by default, it can be displayed through program design.

Steps for drawing:

First, we will rename the object on the container interface promoted to the qcustomplot class above to qcustomplot.

In this case, you only need to add the following code to the main class widget constructor to draw a standard y = x ^ 3 curve. The code meaning Code contains comments.

1 Widget: widget (qwidget * parent): 2 qwidget (parent), 3 UI (new UI: widget) 4 {5 UI-> setupui (this ); 6 7 // define two variable arrays to store the plot coordinate data 8 qvector <double> X (101), y (101); // respectively store the X and Y coordinate data, 101 is the data length 9 // Add data. Here we demonstrate y = x ^ 3. for positive and negative symmetry, we use X from-10 to + 1010 for (INT I = 0; I <101; I ++) 11 {12 x [I] = I/5-10; 13 y [I] = x [I] * X [I] * X [I]; 14} 15 16 // to the drawing area qcustomplot (upgraded from the widget) add a curve 17 UI-> qcustomplot-> addgraph (); 18 // Add data 19 UI-> qcustomplot-> graph (0)-> setdata (x, y ); 20 21 // set the axis label name 22 UI-> qcustomplot-> xaxis-> setlabel ("X "); 23 UI-> qcustomplot-> yaxis-> setlabel ("Y"); 24 25 // set the axis display range, otherwise, we can only see the default range 26 UI-> qcustomplot-> xaxis-> setrange (-11, 11); 27 UI-> qcustomplot-> yaxis-> setrange (-1100,1100 ); 28 29 // re-paint. This is not required. In the official example, the setdata function is automatically re-painted 30 // I think it should be used for dynamic display or dynamic display after changing the coordinate axis range, we will explore 31 // UI-> qcustomplot-> replot (); 32 33}

In this way, we can draw a three-power curve.

 

Qt qcustomplot plot (1) configuration and the first example

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.