qt学习(三):鼠标图标改变

qt学习 (三):鼠标图标改变

当你进入一个美好的qt软件场景,比如游戏,电脑的黑白图标会让程序逊色不少,

1改图标要加光标的头文件,

2 载入光标图,

3 再设置改光标就可以了

1在头文件中加 #include <QtGui>  //光标类的父类
//再在public成员中声明换的函数void keyPressEvent(QKeyEvent *k);
//声明按键换图的函数
        
.h文件    --注意头文件和声明

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

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>  //光标类的父类
namespace Ui {
    class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    void keyPressEvent(QKeyEvent *k);//声明按键换图的函数
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
 
 
 

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

                      Main.cpp文件   ---注意加,选字库函数

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

#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有时候要提示用户,要显示中文, 但中文需要字符串处理,要转换, 所以用字库

Qt 为字节流和字符串分别提供了 QByteArray 和 QString 两个类(还有QLatin1String等其他类,但这两个是最主要的)。

当我们涉及到IO时,比如读写文件、读写网络socket、控制台(终端)输入输出、读写串口... 操作的都是字节流,

如果我们此时需要的操作的内容是字符串,则需要二者之间的相互转换。

即选择字库

3 QCursor能调用Qpixmap装载图片

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

              MainWindow.cpp文件   ---注意event中的set和restore

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

#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");
//上一次学的装载
       QApplication::setOverrideCursor(my);//用成员函数设置图标
       QApplication::restoreOverrideCursor();//删除载入的图
    }
}
4用成员函数设置图标

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 take 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.

设置应用程序覆盖光标光标。

应用覆盖游标用于向用户显示的应用是在一个特殊的状态,动手术,可能需要一些时间,在实例。这种光标将显示在应用程序的所有部件,直到restoreoverridecursor()或另一个setoverridecursor()叫做。应用游标储存于一个内部堆栈。setoverridecursor()将光标移动到堆栈,和restoreoverridecursor()主动光标从堆栈中弹出。changeoverridecursor()改变目前活跃的应用覆盖光标。每一setoverridecursor()最终都必须遵循相应的restoreoverridecursor(),否则不会被清空栈。

Example:

QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));

calculateHugeMandelbrot(); // lunch time...

5

用完删掉栈图 QApplication::restoreOverrideCursor();

6:结果

clip_image003

   如果想要更好看的, 可以吧好看的图片改成.png  在把部分透明化就好看, 比如下面的圣光普照。

原文地址:https://www.cnblogs.com/mayplestory/p/3898411.html