Use of qwt to draw scientific charts based on QT

Source: Internet
Author: User
Tags ranges qt designer

Article 1

Qwt is an open-source project based on the lgpl copyright Protocol. Its goal is to provide a set of 2D form libraries to display data in the technical field. The data source is provided in floating point groups or ranges, the output can be curves, Slider, dials, and compasses. The tool library is developed based on QT, so it also inherits the cross-platform features of QT, according to the original document, the project in the QT-Win/Qt-x11/Qt-embedded (qvfb environment) have been tested and run normally. Project home page at: http://qwt.sourceforge.net/

I searched the internet and found that there are very few Chinese documents about qwt. Basically, there are only articles about compilation and installation. In fact, through the practice of the author, the compilation and running of qwt is really not good, qmake; make can be done, there is nothing to do with Memo, so this article will focus on literacy and introduce the feature of qwt.

Of course, in the normal order, we will start from compilation and installation. Download the latest code from the svn server: SVN Co https://qwt.svn.sourceforge.net/svnroot/qwt/trunk/qwt
Go to the qwt directory, run qmake corresponding to qt4 on your computer, and then run make compilation. For example, in my environment
$ CD qwt
$ Export Path =/usr/local/trolltech/Qt-4.5.1/bin/: $ path
$ Qmake
$ Make

Compilation takes several minutes. After successful execution, the libqwt. So * file is generated in lib and examples is also used for compilation. The generated binary file is in examples/bin. We can run these examples to check the qwt function.

$ Export LD_LIBRARY_PATH = $ PWD/lib
$ CD examples/bin
$./Simple

Simple is the simplest example of qwt. It only contains one hundred lines of code and implements the sine function (sin () and cosine function (COS () curves in mathematics. For example:

There are four core classes used in this example (the following content is the author's understanding, and there may be something wrong, please read as appropriate ):
Qwtplot
Similar to a 2D drawing container, other qwtplotitem derived class objects can be placed, such as qwtplotmarker used in this example. (Similar to graphicsview in QT .) This class controls the generation and transmission of drawing data and draws an axis.
Qwtplotmarker
Scale class, used to draw a dial line.
Qwtplotcurve
Curve class, used to draw various curves.
Qwtsyntheticpointdata
It is strange that this is an unreceivented class. It is estimated that this class is accidentally omitted by the author. The description of this class is not provided in the document, and the answer can only be found from the source code. This is depressing. By looking at the comments in the Code and the example code, the author understands that this class is responsible for generating data. Its function is to provide a fixed number of floating point points, and the number of points is passed in when constructing the class. When obtaining data, the qwtplotcurve class calls the Y () method of the class to obtain the ordinate.

The basic process of the program is:
1. initialize the drawing container and set the axis Parameters
// Set Axes
Setaxistitle (xbottom, "X-> ");
Setaxisscale (xbottom,
0.0, 10.0); // the x-axis ranges from 0 to 10. xbottom indicates that the X-axis is directed from bottom to top.

Setaxistitle (yleft, "Y-> ");
Setaxisscale (yleft,
-1.0, 1.0); // y coordinates-1 to 1, yleft indicates the direction of Y coordinates from left to right

2. Add a sine cosine curve
// Insert new curves
Qwtplotcurve * Csin = new qwtplotcurve ("Y = sin (x )");
# If qt_version> = 0 ×040000
Csin-> setrenderhint (qwtplotitem: renderantialiased );
# Endif
Csin-> setpen (qpen (QT: Red); // red curve
Csin-> attach (this );

Qwtplotcurve * ccos = new qwtplotcurve ("y = cos (x )");
# If qt_version> = 0 ×040000
Ccos-> setrenderhint (qwtplotitem: renderantialiased );
# Endif
Ccos-> setpen (qpen (QT: Blue); // blue curve
Ccos-> attach (this );

3. Set the data content of the curve
// Create sin and cos data
Csin-> setdata (functiondata (: sin ));
Ccos-> setdata (functiondata (: COS ));

4. Add the horizontal and vertical ruler lines as the reference coordinates.
// Insert markers
//... A horizontal line at y = 0...
Qwtplotmarker * My = new qwtplotmarker ();
My-> setlabel (qstring: fromlatin1 ("Y = 0 ″));
My-> setlabelalignment (QT: alignright | QT: aligntop );
My-> setlinestyle (qwtplotmarker: hline); // line direction
My-> setyvalue (0.0); // The ruler line is drawn at the position Y = 0.
My-> attach (this );

//... A vertical line at x = 2 * pi
Qwtplotmarker * MX = new qwtplotmarker ();
Mx-> setlabel (qstring: fromlatin1 ("x = 2 pi "));
Mx-> setlabelalignment (QT: alignleft | QT: alignbottom );
Mx-> setlabelorientation (QT: vertical );
Mx-> setlinestyle (qwtplotmarker: vline );
Mx-> setlinepen (qpen (QT: Black, 0, QT: dashdotline ));
Mx-> setxvalue (2.0 * m_pi); // draw the line at the position X = 2PI.
Mx-> attach (this );

If you use QT to write the same function, you need to write a large amount of drawing code, which is troublesome. However, the qwt encapsulated library code is more Oo and easier to understand and maintain. In this example, the advantages of using qwt may not be obvious, and more complex charts can reflect the superiority of qwt. Here are several examples:

 

Article 2

Now we see the third version of this article. The reason for writing this blog post is that this dish is recently prepared to use qwt to develop some functional components, and the company basically uses Win XP for development, to facilitate future team development, we studied how to integrate the qwt library and qtcreator. However, due to her lack of rigorous attitude, I wrote a blog post easily after the experiment was successful and I felt that integration was okay. Thanks to shiroki, and even serious errors may mislead others. Through the unremitting efforts and research on the weekend, we finally achieved the experiment results and confirmed the ideas of this dish. In addition, we have a better understanding of QT and supplemented the previous knowledge points we lack, it is also truly achieved. Draw scientific charts based on QT
-- During the qwt trial, shiroki introduced how to install qwt in Linux and use qwt. This dish has been continuously tried in the WINXP operating system, it is found that QT creator can be used for compilation quickly, while QT creator can also be used for program development. Therefore, we will introduce it in detail. First, make sure that you are using the WINXP operating system and have installed qt sdk for Windows. If not, you can download the installation package from the official QT website. The installation process is very simple, there is no big difference with the general windows EXE installation package. After specifying the path, you can install the package one by one. After the installation is complete, you can use it to compile the QT program without setting environment variables. After installation, you can use qtcreator, and then download the source code of qwt, the current version is the qwt-5.2.0, in the previous article provided the project home address, on the project homepage, you can download the tar source code package for Linux or the zip source code package for Windows. We naturally choose the zip package. After the download is complete, decompress the directory in the English path. With QT
The compiling process of SDK and qwt naturally becomes very simple. environment variables, QT lib libraries, and everything has the help of qt sdk, this is why I want to say that qwt is installed in a silly way. Compared with previous compilation, installation and setting of environment variables, QT creator can help simplify the process. Open the QT creator and open the qwt source code directory. PRO project file, and then click the run button in the lower left, qwt starts the automatic compilation process: the compilation process takes 5 to 10 minutes, you can sit down to something else, or have a cup of coffee or something

 

In the previous article, we introduced how to use qtcreator to compile and install qwt. Now we will introduce how to use qtcreator to compile the QT program using the qwt library. To simplify the process, this dish directly copies the example code in qwt, creates a project using qtcreator, and pastes the code. At this time, if you mistakenly thought that you could run the program by clicking run, it would be a big mistake. The following error was made at the beginning of the course. Unfortunately, the example in the qwt directory is used directly, but the PRO project file in it is set up, as long as the library file DLL and libxx are generated. A files can naturally run. This is how this dish is blinded, resulting in the illusion that the qwt library has been set up.
If you click Run, the following error occurs: Simply put, qtcreator cannot find the declarations of various types in the qwt library. The solution is also very simple. Just include the header file of qwt. To decompress the qwt directory, there is a src directory below, which contains all the qwt source code files, replace. copy the H header file and go to the installation directory of the qt sdk. Create a qwt file in the include directory of the following QT subdirectory (for example, F: \ QT \ 2009.03 \ QT \ include, the copied. paste the H file. After all, qwt is a third-party library. It is expected that qtcreator can recognize it automatically. You also need to set libs and includepath in the. Pro file.
, These belong to the scope of qmake knowledge, interested friends can go to see the document qmake Manual Part: http://qt.nokia.com/doc/4.5/qmake-manual.html. set libs + =-L "F:/QT/2009.03/QT/lib"-lqwt5 includepath + = F: /QT/2009.03/QT/include/qwt each has different interests. You can set them based on your needs. The above steps are very simple to concentrate, but it takes a lot of time to try this dish without knowing anything about it, it can be said that it is a step by step of hard work. I'm afraid I 've been thinking a lot about it. Now I can use qwt normally. Well, I think so too, but the result is:
It was a very depressing mistake, and it took a lot of time to study it. The reason is actually very simple. In WINXP, QT cannot mix libraries. Under qtcreator, the debug mode is used by default, and the release library compiled in this dish is not enough. When qtcreator is set to the release mode, the program can be run. But what should I do for debugging? Of course, as long as you compile qwt to generate the debug library file, you can naturally compile and use the debug mode in qtcreator. Here is the source code directory of qwt. Open the qwtconfig. PRI file, you can use Notepad, or use a software tool similar to ultraedit. Find This section in it:
Win32 {# On Windows you can't mix release and debug libraries. # The designer is built in release mode. if you like to use it # You need a release version. for your own application development you # might need a debug version.
# Enable debug_and_release + [...]

 

In and in the middle part, through some simple column configurations, we can easily compile and install the qwt library on WINXP and make few modifications to the Pro file, you can use the qwt library to develop programs smoothly. This step is sufficient for developers who are used to coding, but it is far from enough for developers who require fast application development or who are used to QT designer. Careful people may think that the previous copy of the library file should have been integrated into the QT designer. You can say that you don't know much about qt sdk. qt sdk is composed of two parts: QT creator and QT library. Without QT creator, you can still use the QT library to write programs, without the QT library, QT
Creator cannot do anything. In the above process, I tightly integrated the qwt library into the QT library. If I started the QT library designer, I would naturally see the qwt component that can be selected next to it, in the qtcreator designer, qwt components are not displayed. Similarly, the solution is to copy the qwt plug-in file to the bin directory of qtcreator, for example, F: \ QT \ 2009.03 \ bin \ designer. If this is a one-time success, it would be a good thing. The reality is cruel. The designer in qtcreator shows that the plug-in cannot be loaded, and the error message shows: "The plugin uses incompatible
Qt library. Expected build key "Windows msvc relase full-config", to "Windows mingw release full-config". Why does the msvc compiler appear when the QT library is compiled with mingw? So I carefully searched the relevant information and found the following passage: the version of QT creator shipped with the QT SDK open source version for Windows is build using Visual
Studio, whereas the SDK is build with and supports g ++ (mingw). This [...]

 

Article 3

) Win32/mingw qt4
============================

C1) Windows Shell

Start a Windows Shell, where qt4 is initialized. (F. E.
"Programs-> QT by trolltech...-> QT 4. x. x command prompt ").

Qmake qwt. Pro
Make

If you didn't enable autobuilding of the examples in qwtconfig. PRI
You have to build the examples this way:

CD examples
Qmake examples. Pro
Make
Make install

 

Article 4

CD path
Qmake qwt. Pro
Mingw32-make
Install mingw32-make

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.