Graphics view framework of QT (III)

Source: Internet
Author: User
Key features scaling and Rotation

Qgraphicsview uses qgraphicsview: setmatrix () to support the same settings as qpainter. By applying transformations to a view, you can easily support common navigation features such as scaling and rotation. The following is an example:

class View:;public QGraphicsView{Q_OBJECT//.....public slots:void zoomIn() {scale(1.2,1.2);}void zoomOut() {scale(1/1.2,1/1.2);}void rotateLeft() {rotate(-10);}void rotateRight() {rotate(10);}};

These slots should be connected to qtoolbuttons and make autorepeat valid. When a view is changed, qgraphicsview corrects the View Center.

Drag

Because qgraphicsview inherits from qwidget, it also provides drag and drop functions like qwidget. In addition, for convenience, graphics view Cabinet also provides drag and drop support for each item in scenarios. When the view receives a drag event, it can be translated into qgraphicsscenedragdropevent and then sent to the scenario. The scenario takes over this event and sends it to the first item under the cursor to be dragged.
When dragging from an item, create a qdrag object and pass the pointer of the widget that begins to drag. Items can be viewed by multiple views at the same time, but only one view can be dragged. In most cases, dragging starts when you press or move the mouse. Therefore, in mousepressevent () or mousemoveevent (), you can obtain the original widget pointer from the event, for example:

void CustomItem::mousePressEvent(QGraphicsSceneMouseEvent *event){QMimeData *data=new QMimeData;data->setColor(Qt::green);QDrag *drag=new QDrag(event->widget());drag->setMimeData(data);drag->start();}

To load drag and drop events in the scene, you should re-implement qgraphicsscene: dragenterevent () and any event processor required for your specific scenario in the qgraphicsitem subclass. Items can also get drag support by calling qgraphicsitem: setacceptdrops (). to process the drag, you need to re-implement qgraphicsitem: dragenterevent (), qgraphicsitem: dragmoveevent (), qgraphicsitem: dragleaveevent () and qgraphicsitem: dropevent ().

Cursor and tooltip

Like qwidget, qgraphicsitem also supports cursor (qgraphicsitem: setcursor) and tooltip (qgraphicsitem: settooltip ()). When the cursor enters the item area, the cursor and tooltip are activated by qgraphicsview (detected by calling qgraphicsitem: Contains ). You can also set a default cursor (qgraphicsview: setcursor) directly on The View ).

Animation

Graphics view supports several levels of animation. You can easily associate qgraphicsitemanimatoin with your item.
Assemble the animation path, which allows you to control the animation with a timeline and operate at a stable rate on all platforms. Qgraphicsitemanimation allows you to generate a path for the position, rotation, scaling, cutting, and transformation of an item. You can use qslider to control the animation or use qtimeline more commonly.
The other is inherited from qobject and qgraphicsitem. Item can set its own timer to control the animation by adding a step in qobject: timeevent.
The third is to call qgraphicsscene: Advance () to promote the scenario, and it calls qgraphicsitem: Advance () in turn ().

OpenGL Rendering

To use OpenGL for rendering, you need to set a new qglwidget as the qgraphicsview's viewport: qgraphicsview: setviewport (). If you want OpenGL to provide the anti-sawtooth function, you need OpenGL sampling buffer.
Qgraphicsview view (& scene );
View. setviewport (New qglwidget (qglformat (qgl: samplebuffers )));

Item Group

By using one item as the child of another item, you can get most of the essential features of the item group: these items will move together, all transformations
It is passed from parent to child. Qgraphicsitem can also handle all the events for its children, so that the father can represent all its children and can effectively regard all items as a whole.
In addition, qgraphicsitemgroup is a special item that processes child events and has an interface to add and delete items from a group. Add an item
Qgraphicsitemgroup still retains the original position and transformation of the item, while re-specifying the parent item for an item will allow the item to be relocated based on its new parent. You can use qgraphicsscene: createitemgroup () to create a group.

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.