Qt5 Coordinate System

Source: Internet
Author: User

This time, there was basically no major error .. Based on the down-Down Function of the buttons, the up-down function is implemented, and the rotate () function is rotated.

Myitem. h

# Ifndef myitem_h # define myitem_h # include <qgraphicsitem> class myitem: Public qgraphicsitem {public: myitem (); qrectf boundingrect () const; void paint (qpainter * painter, const alert * option, qwidget * widget); void setcolor (const qcolor & color) {brushcolor = color;} void keypressevent (qkeyevent * event); void mousepressevent (qgraphicsscenemouseevent * event ); // press the mouse button to respond to void hoverenterevent (qgraphicsscenehoverevent * event); // void contextmenuevent (callback * event); Private: qcolor brushcolor ;};# endif // myitem_h

Myview. h

#ifndef MYVIEW_H#define MYVIEW_H#include <QGraphicsView>class MyView : public QGraphicsView{    Q_OBJECTpublic:    explicit MyView(QWidget *parent = 0);protected:    void keyPressEvent(QKeyEvent *event);signals:public slots:};#endif // MYVIEW_H

Main. cpp

# Include <qapplication> # include "myitem. H "# include" myview. H "# include" myitem. CPP "# include" myview. CPP "# include <qtime> int main (INT argc, char * argv []) {qapplication app (argc, argv); qsrand (qtime (0, 0, 0 ). secsto (qtime: currenttime (); qgraphicsscene scene; scene. setscenerect (-200,-150,400,300); For (INT I = 0; I <= 5; ++ I) {myitem * Item = new myitem; item-> setcolor (qcolor (qrand () % 256, qrand () % 256, qrand () % 256 )); // randomly generate the color item-> setpos (I * 50-90,-50); scene. additem (item);} myview view; view. setscene (& scene); view. setbackgroundbrush (qpixmap (".. /myview/11.jpg"); view. show (); Return app.exe C ();}

Myitem. cpp

# Include "myitem. H "# include <qpainter> # include <qcursor> # include <qkeyevent> # include <qgraphicsscenehoverevent> # include <qgraphicsscenecontextmenuevent> # include <qmenu> myitem: myitem () {brushcolor = QT: red; setflag (qgraphicsitem: itemisfocusable); setflag (Expiration: itemismovable); setaccepthoverevents (true);} qrectf myitem: boundingrect () const {qreal adjust = 0.5; return qrectf (-10-adjust,-10-adjust, 20 + adjust, 20 + adjust);} void myitem: paint (qpainter * painter, const qstyleoptiongraphicsitem * option, qwidget * widget) {If (hasfocus () {painter-> setpen (qpen (qcolor (255,255,255,200 )));} else {painter-> setpen (qpen (qcolor (100,100,100,100);} painter-> setbrush (brushcolor); painter-> drawrect (-10,-, 20 );} void myitem: mousepressevent (qgraphicsscenemouseevent * event) {setfocus (); setcursor (QT: closedhandcursor);} void myitem: keypressevent (qkeyevent * event) {If (Event-> key () = QT: key_down) // X, Y moves {moveBy ();} else if (Event-> key () = QT: key_up) {moveBy (0,-10) ;}} void myitem: hoverenterevent (qgraphicsscenehoverevent * event) {setcursor (QT: openhandcursor ); settooltip ("I am item");} void myitem: contextmenuevent (qgraphicsscenecontextmenuevent * event) {qmenu menu; qaction * moveaction = menu. addaction ("Move Back"); // right-click the menu to display qaction * selectedaction = menu.exe C (Event-> screenpos (); If (selectedaction = moveaction) // return to (0, 0) point {setpos (0, 0 );}}

Myview. cpp

# Include "myview. H "# include <qkeyevent> myview: myview (qwidget * parent): qgraphicsview (parent) {} void myview: keypressevent (qkeyevent * event) {Switch (Event-> key () {Case QT: key_plus: // + scale (1.2, 1.2); break; Case QT: key_minus: //-scale (1/1. 2, 1/1.2); break; Case QT: key_right: Rotate (30); break; Case QT: key_left: Rotate (-30); break;} qgraphicsview :: keypressevent (event );}


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.