QT Implementation Picture Moving instance (graphics and text tutorial) _c language

Source: Internet
Author: User

This semester practice with MFC did a plane war, very boring things, always want to do with QT, but in school when more decadent, back to look at.

The first problem to be solved is the picture moving, how to say the plane ah bullets are moving, the picture of course to run up.

Gossip Hugh Floc, first with Qtcreator a new Qtgui program, named Paintwidget, casually up the name, experiment this is not.

Will generate these three files, where the UI does not have to tube, the experiment picture movement needs to use is the event, is not the signal slot, therefore the UI does not matter, puts that is.

The first step to draw out the picture, referring to the "QT Learning path of this code", it is not difficult to draw out, is to rewrite the PaintEvent method, with Qpainter object to paint.

Copy Code code as follows:

void Paintedwidget::p aintevent (qpaintevent *event)
{
qpainter painter (this);
Qpixmap pixmap ("Cat.png");
Qbitmap Bitmap ("Cat.png");
Painter.drawpixmap (Ten, 128, 128, PIXMAP);
Painter.drawpixmap (140, 128, 128, bitmap);
Qpixmap pixmap2 ("Cat2.png");
Qbitmap bitmap2 ("Cat2.png");
Painter.drawpixmap (ten, 140, 128, 128, PIXMAP2);
Painter.drawpixmap (140, 140, 128, 128, BITMAP2);
}

This is his result.

The problem is how to use picture resources:

In the "C + + GUI QT4 Programming (second Edition)" This book, there are examples, directly search the source code of the book, in Src\chap04 found that the original QT reference resources are used resource files this thing, is a xml,qtcreator very convenient management.

The first is to add files to the project and select the QT resource file:

Because there may be a lot of pictures in the future, so I created a new IMG folder in the Engineering directory to store pictures.

The generated resource file has a prefix appended to it, which is needed for referencing resources in Qt, and after that, add files without nagging.

To facilitate the explanation, first send the source code up

Mainwindow.h

Copy Code code as follows:

#ifndef Mainwindow_h
#define Mainwindow_h

#include <QtGui>

Namespace Ui {
Class MainWindow;
}

Class Mainwindow:public Qmainwindow
{
Q_object

Public
Explicit MainWindow (Qwidget *parent = 0);
~mainwindow ();
Protected
void PaintEvent (Qpaintevent *event);
void Mousemoveevent (Qmouseevent *event);
void Keypressevent (Qkeyevent *event);

Private
Ui::mainwindow *ui;
Qpixmap *catimg;
Qrect *catimgrect;

Protected
int shrinkmultiple;
int speed;
};

#endif//Mainwindow_h

Where the qpixmap*catimg is a pointer to a picture object, because my picture is a cat, used to have a name.

Qrect*catimgrect is the image of the rectangular information, the picture is moving when the image of the x,y coordinates of the value will change, the rectangular information will change, and the drawing function has a function based on the rectangle information and the picture pointer to draw a function

Painter.drawpixmap (*catimgrect,*catimg); 

For convenience, use Catimgrect to store this information.

and the int shrinkmultiple; This is the magnification of the picture, my cat's picture is a bit large, it narrows him down, of course, without

This int speed is the number of pixels that the picture moves

OK, here's mainwindow.cpp.

Copy Code code as follows:

#include "Mainwindow.h"
#include "Ui_mainwindow.h"

Mainwindow::mainwindow (Qwidget *parent):
Qmainwindow (parent),
UI (New Ui::mainwindow),
Shrinkmultiple (2), speed (10)
{
UI-&GT;SETUPUI (this);
catimg = new Qpixmap (":/img/cat.jpg");
int width = catimg->width ()/shrinkmultiple;
int height = catimg->height ()/shrinkmultiple;
Catimgrect = new Qrect (10,10,width,height);
Qrect rect = This->geometry ();
Rect.setwidth (WIDTH*4);
Rect.setheight (HEIGHT*4);
Rect.setx (20);
Rect.sety (50);
This->setgeometry (rect);
}
void MainWindow::p aintevent (qpaintevent *event)
{
qpainter painter (this);
Painter.drawpixmap (*CATIMGRECT,*CATIMG);
}
void Mainwindow::mousemoveevent (Qmouseevent *event)
{
}
void Mainwindow::keypressevent (Qkeyevent *event)
{
int width = catimgrect->width ();
int height = catimgrect->height ();
Switch (Event->key ())
{
Case Qt::key_left:
{

Catimgrect->setx (Catimgrect->x ()-speed);

Break
}
Case Qt::key_right:
{
Catimgrect->setx (Catimgrect->x () +speed);
Break
}
Case Qt::key_down:
{
Catimgrect->sety (Catimgrect->y () +speed);
Break
}
Case QT::KEY_UP:
{
Catimgrect->sety (Catimgrect->y ()-speed);
Break
}
}
Catimgrect->setheight (height);
Catimgrect->setwidth (width);
This->repaint ();

}
Mainwindow::~mainwindow ()
{
Delete catimg;
Delete Catimgrect;
Delete UI;
}

First look at the constructor, where
Copy Code code as follows:

Catimg=new Qpixmap (":/img/cat.jpg");

Here is a reference to a resource file:/img/cat.jpg, the first slash after the colon is the prefix that was just added.

The following lines are to shrink the picture of the cat and initialize the cat rectangle information, and the last line is to set the program window size 4 times times the size of the cat picture. The

Paint function is paintevent this function, nothing special, this function is invoked as soon as the window is redrawn.

The key move is to override the

Copy code code as follows:

Voidmainwindow::keypressevent (qkeyevent*event)

this letter Number, nothing special, out of the last line
Copy code code is as follows:

This->repaint ();

If the keyboard is pressed, redraw the window, and without this function, the cat rectangle does change, but the position of the picture does not change, unless the PaintEvent function is called when it is redrawn, and when it is redrawn, when this part of the window is obscured, Minimize, when the window is moving (and possibly), the function of the repaint is to redraw immediately, so the picture can be moved immediately.

Picture movement is no problem, the key is not very fluent, when using MFC is to use the timer to solve the problem, QT also has a timer, another day to study, but there may be problems, that is, window flashing problems, this time should be used to double buffer drawing technology.

Paste the final picture

-

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.