"QT5 Development and examples" 32, the implementation of keyboard monitoring operation

Source: Internet
Author: User

Keyevent.h

/*** Book: "QT5 Development and examples" * Function: Implement keyboard control * file: keyevent.h* time: February 8, 2015 11:48:47* Author: cutter_point*/#ifndef keyevent_h#define Keyevent_h#include <QWidget> #include <QKeyEvent>    /keyboard # include <QPaintEvent>//    Painting Events # Include <qpainter>class keyevent:public qwidget{  q_objectpublic:  keyevent (Qwidget *parent = 0);  ~keyevent ();  void Drawpix ();   Painting image  void Keypressevent (Qkeyevent *event);    Keyboard corresponding  void PaintEvent (qpaintevent *);p rivate:  qpixmap *pix;   Buffered images  qimage image;    Picture  int StartX;  int starty;  int width;  int height;  int step;   Size of each step}; #endif//Keyevent_h

Keyevent.cpp

/*** Book: "QT5 Development and examples" * Function: Implement keyboard control * file: keyevent.cpp* time: February 8, 2015 11:48:47* Author: cutter_point*/#include "keyevent.h" keyevent  :: KeyEvent (Qwidget *parent): Qwidget (parent) {Setwindowtitle (TR ("keyboard event"));  This->setautofillbackground (TRUE);   Qpalette palette = This->palette ();    Get Palette Palette.setcolor (Qpalette::window, qt::white);    Sets the background default color This->setpalette (palette);  Set a good palette, using the palette this->setminimumsize (512, 256);  This->setmaximumsize (512, 256);  width = this->size (). width ();   Height = this->size (). height ();     Get the corresponding long and high pix = new Qpixmap (width, height);  The size of the buffered graphic Pix->fill (qt::white);   Image.load (":/image.png");  Load picture StartX = 100;  Starty = 100;  Step = 20;    Drawpix (); Drawing Resize (512, 256);}   void Drawpix ();  Painting image void KeyEvent::d Rawpix () {Pix->fill (qt::white);   Qpainter *painter = new Qpainter;      Painting Tool Qpen Pen (Qt::D otline);    Dashed for (int i = step; i < width; i = i + Step)//Draw vertical Line {painter->begin (pix);   Set drawing objects   Painter->setpen (pen);   Brush Painter->drawline (Qpoint (i, 0), Qpoint (i, height));    Draw from one point to another point painter->end ();      }//Draw horizontal for (int j = Step; j < height; j = j + Step) {painter->begin (PIX);      Painter->setpen (pen);      Painter->drawline (Qpoint (0, J), Qpoint (width, j));    Painter->end ();  } painter->begin (PIX);    Painter->drawimage (Qpoint (StartX, starty), image); Picture drawing Painter->end ();}    Keypressevent (Qkeyevent *event);       keyboard corresponding void Keyevent::keypressevent (Qkeyevent *event) {if (event->modifiers () = = Qt::controlmodifier)//whether there is a hold down CTRL {        if (event->key () = = Qt::key_left)//left {StartX = (startX-1 < 0)? startx:startx-1; } if (Event->key () = = qt::key_right)//Right {StartX = (StartX + image.width () > width)? s        Tartx:startx + 1; } if (Event->key () = = qt::key_up)//on {Starty = (startY-1 < 0)? starty:starty-1; } if (Event->key () = = Qt::key_down)//under {Starty = (Starty + image.height () > height)? start        Y:starty + 1;      }} else {StartX = startx-startx% Step;      As a multiple of step, the back good judgment starty = starty-starty% Step;        if (event->key () = = Qt::key_left)//left {StartX = (Startx-step < 0)? Startx:startx-step; } if (Event->key () = = qt::key_right)//Right {StartX = (StartX + step + image.width () > Wid TH)?        Startx:startx + step; } if (Event->key () = = qt::key_up)//on {Starty = (Starty-step < 0)? starty:starty-step        ; } if (Event->key () = = Qt::key_down)//under {Starty = (Starty + image.height () + Step > height) ?        Starty:starty + step;          } if (Event->key () = = Qt::key_home)//starting point {StartX = 0;        Starty = 0; } if (Event->key () = = Qt::key_end)//End point {StartX = Width-image.width ();        Starty = Height-image.height ();  }} Drawpix (); This->update ();}  void PaintEvent (qpaintevent *); void KeyEvent::p aintevent (qpaintevent *) {qpainter painter;  Painter.begin (this);   Painter.drawpixmap (qpoint (0, 0), *pix); Draw the image into space and show Painter.end ();} Keyevent::~keyevent () {}

Resource Picture:


Run:





"QT5 Development and examples" 32, the implementation of keyboard monitoring operation

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.