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  在把部分透明化就好看, 比如下面的聖光普照。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM