Qt Learning (3): Mouse icon change

Source: Internet
Author: User

Qt Learning (3): Mouse icon change

When you enter a wonderful QT software scenario, such as a game, the black and white icons of the computer will make the program inferior,

1. Add the cursor header file to the icon,

2 load the optical map,

3. Set and change the cursor.

1. Add the # include <qtgui> // cursor class parent class to the header file.
// Declare the void function in the Public member.Keypressevent(Qkeyevent * k );
// Declare the function of button for graph change
        
. H file-pay attention to header files and declarations

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
# Include <qtgui> // The parent class of the cursor class
namespace Ui {
    class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
VoidKeypressevent(Qkeyevent * k); // declare the function of changing the graph by pressing the button
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
 
 
 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~

Main. cpp file --- add and select the font Function

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QTextCodec>
int main(int argc, char *argv[])
{
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
 
 
 
2. Sometimes you need to prompt the user to display Chinese characters, but Chinese characters must be processed by strings and converted.

Qt provides the qbytearray and qstring classes for byte streams and strings respectively (and other classes such as qlatin1string, but these two are the most important ).

When I/O is involved, such as reading and writing files, reading and writing network socket, console (terminal) input and output, reading and writing serial ports... all operations are byte streams,

If the operation content we need at this time is a string, the conversion between the two is required.

Select the font library

3. qcursor can call qpixmap to load images

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~

Mainwindow. cpp file --- pay attention to set and restore in event

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::keyPressEvent(QKeyEvent *k)
{
    if(k->key() == Qt::Key_F8)
    {
       QCursor my(QPixmap("C:/Users/Administrator/Desktop/zhi.png");
// Last learned Loading
Qapplication: setoverridecursor (my); // use the member function to set the icon.
Qapplication: restoreoverridecursor (); // deletes a loaded graph.
    }
}
4. Set the icon using the member function

Void qapplication: setoverridecursor (const qcursor &Cursor)[Static]

Sets the application override cursor to cursor.

Application override cursors are intended for showing the user that the application is in a special State, for example during an operation that might takes some time.

This cursor will be displayed in all the application's widgets until restoreoverridecursor () or another setoverridecursor () is called.

Application cursors are stored on an internal stack. setoverridecursor () pushes the cursor onto the stack, and restoreoverridecursor () pops the active cursor off the stack. changeoverridecursor () changes the curently active application override cursor.

Every setoverridecursor () must eventually be followed by a corresponding restoreoverridecursor (), otherwise the stack will never be emptied.

Set the application to overwrite the cursor.

The application overwrite cursor is used to show the application to the user in a special State, and the operation may take some time in the instance. This cursor will appear in all parts of the application until restoreoverridecursor () or another setoverridecursor () is called. The application cursor is stored in an internal stack. Setoverridecursor () moves the cursor to the stack, and the active cursor of restoreoverridecursor () pops up from the stack. Changeoverridecursor () changes the current active application to overwrite the cursor. Each setoverridecursor () must follow the corresponding restoreoverridecursor (). Otherwise, the stack will not be cleared.

Example:

Qapplication: Setoverridecursor (Qcursor(Qt: Waitcursor ));

Calculatehugemandelbrot (); // lunch time...

5

Delete stack chart after useQapplication: Restoreoverridecursor ();

 

6: Result

 

If you want to see a better image, you can convert it to a. PNG image. If you want to make some of the images more transparent, you can see the image as follows.

 

 

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.