Combined Development of QT and VTK (2)

Source: Internet
Author: User

The last time I introduced how to display the VTK image in the QT widget, but there was still a problem that could not be solved, that is, when I dragged the image with the left mouse button for rotation, in addition, the original method also uses the underlying winevent () function to pick out mouse events from all messages, as a result, this encapsulation is not beautiful. Secondly, the winevent () function can only be used on the Windows platform, thus losing the cross-platform nature.

When the default mouse operation method (interactorstyle under VTK) is modified, a simple solution is found, which perfectly solves the above problems.

First, I used vtkinteractorstyletrackballcamera to replace the default interaction method in the vtkinteractor. What are the differences between the two interaction methods? It is also difficult for me to describe in languages, in short, trackball seems to be more in line with my usage habits. The code to replace the default interaction mode of vtkinteractor is:

Minteractorstyle = vtkinteractorstyletrackballcamera: New ();
Minteractor-> setinteractorstyle (minteractorstyle );

Simple: The vtkinteractor is called the interactive device. It can be regarded as taking over all the window messages of the vtkwindow and then processing it. The vtkwindow itself is only responsible for display, in fact, the display and message processing functions of common Windows are divided into two classes for management. The specific content of this section will be explained later.

As we mentioned earlier, vtkinteractor is actually just a proxy class. It is responsible for getting mouse and keyboard messages. How can we reflect the images on the form based on these messages, this is determined by a member of its vtkinteractorstyle, and this class has many different extensions (implementations), so that you only need to replace different derived objects, you can easily change the operation function, which is what the above minteractor-> setinteractorstyle () function does.

After modifying the above code, I found that my program did not update the mouse operation method as expected. What happened? I used vkthandlemessage2 () the function does not use any interactor, but processes messages by itself. It seems that the program written by mfcsample was originally using an extremely informal method.

After carefully checking the help of VTK, I accidentally found several public functions under vtkinteractorstyle, such as onleftbuttondown (), onrightbuttondown (), and so on. It seems that this function can be used, so I will immediately modify the code, it overwrites several functions of QT, such as mousepressevent (), mousemoveevent (), and mousereleaseevent (), and then adds the mouse operation function in interactorstyle to run the program .... Failed. Why is it still not reflected .. At this time, the advantages of opensource are shown. The document is not good. I can check the source directly and check it with onleftbuttondown (). It turns out this is the case: interactorstyle is easy to get the current position of the mouse operation, but it is not obtained by itself. Instead, it is obtained based on the geteventposition () function of the interactor, before Various Mouse times, call the minteractor-> seteventposition () function to tell the current mouse position. Then, you can use the mouse to operate the mouse. However, with a click, the left mouse button is pressed, the left and right sides are moved, and the image is rotated to the right. It is moving up and down, and the direction of the image is the opposite. What is the problem? After reading the document again, there was a function named seteventpositionflipy (). It seems that the developer knows that I will encounter this problem. I will try it again. Hey, it's totally normal! Later I realized that the original mouse origin in Windows is the upper left corner of the screen, while in OpenGL graphics systems, the origin is the lower left corner, so of course I need to reverse the Y axis.

Finally, paste the relevant code:

Void enstmapwidget: mousepressevent (qmouseevent * E)
{
Minteractor-> seteventpositionflipy (e-> X (), E-> Y ());
Switch (e-> button ()){
Case QT: leftbutton:
Minteractorstyle-> onleftbuttondown ();
Break;
Case QT: rightbutton:
Minteractorstyle-> onrightbuttondown ();
Break;
Case QT: midbutton:
Minteractorstyle-> onmiddlebuttondown ();
Break;
}
}

Void enstmapwidget: mousemoveevent (qmouseevent * E)
{
Minteractor-> seteventpositionflipy (e-> X (), E-> Y ());
Minteractorstyle-> onmousemove ();
}

Void enstmapwidget: mousereleaseevent (qmouseevent * E)
{
Minteractor-> seteventpositionflipy (e-> X (), E-> Y ());
Switch (e-> button ()){
Case QT: leftbutton:
Minteractorstyle-> onleftbuttonup ();
Break;
Case QT: rightbutton:
Minteractorstyle-> onrightbuttonup ();
Break;
Case QT: midbutton:
Minteractorstyle-> onmiddlebuttonup ();
Break;
}
}

In addition, we recently used VTK as a display area to display it on the main program interface. Let's take a look at the effect:

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.