轉帖|使用教程|編輯:鮑佳佳|2021-06-23 11:06:01.683|閱讀 296 次
概述:Qt內置基本的鼠標樣式,使用函數QCursor(Qt::CursorShape shape)進行設置。對于不同操作系統來說,設置的Qt鼠標樣式會被替換成當前系統支持的鼠標樣式效果。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Qt是一個跨平臺框架,通常用作圖形工具包,它不僅創建CLI應用程序中非常有用。而且它也可以在三種主要的臺式機操作系統以及移動操作系統(如Symbian,Nokia Belle,Meego Harmattan,MeeGo或BB10)以及嵌入式設備,Android(Necessitas)和iOS的端口上運行。現在我們為你提供了免費的試用版。
Qt組件推薦:
Qt內置基本的鼠標樣式,使用函數QCursor(Qt::CursorShape shape)進行設置。對于不同操作系統來說,設置的Qt鼠標樣式會被替換成當前系統支持的鼠標樣式效果。
Qt內置的鼠標樣式(CursorShape)如下:
比如設置鼠標樣式為Qt::PointingHandCursor:
CustomCursor::CustomCursor(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); setCursor(Qt::PointingHandCursor); //設置鼠標樣式 }效果如下:
2、使用圖片自定義鼠標樣式
使用函數QCursor::QCursor(const QBitmap & bitmap, const QBitmap & mask, int hotX = -1, int hotY = -1),需要準備自定義鼠標樣式的圖片和自定義鼠標樣式的掩碼圖片,hotX和hotY設置鼠標熱點。甚至可以生成與背景具有反差效果的鼠標樣式。該函數詳細使用說明如下:
CustomCursor::CustomCursor(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); QBitmap bitmap(32, 32); //生成32x32大小的bitmap圖片 bitmap.fill(Qt::color1); //填充1像素值 QBitmap bitmap_mask(32, 32); //生成32x32大小的bitmap_mask圖片 bitmap_mask.fill(Qt::color0); //填充0像素值 QCursor cursor(bitmap, bitmap_mask, -1, -1); //-1 -1表示鼠標熱點在中心點 setCursor(cursor); //設置自定義的鼠標樣式 }
效果如下:
為方便理解,這里將顏色設為黑色RGB(0,0,0)表示為1像素值,將顏色設為白色RGB(255,255,255)表示為0像素值。比如生成的bitmap圖片:
生成的bitmap_mask圖片:
CustomCursor::CustomCursor(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); QBitmap bitmap("bitmap.png"); //背景透明的png格式圖片 QBitmap bitmap_mask("bitmap_mask.png"); QCursor cursor(bitmap, bitmap_mask, -1, -1); //-1 -1表示鼠標熱點在中心點 setCursor(cursor); //設置自定義的鼠標樣式 }
效果如下:
3、使用XPM生成鼠標樣式
XPM用于創建位圖文件,可生成背景透明的圖片。使用函數QPixmap(const char * const xpm[])加載xpm。
static const char* const xpmCursor[] = { // columns rows colors chars-per-pixel "20 20 3 1", " c None", "= c #FF796D", //=的顏色 "* c #FFE6B2", //*的顏色 " ", " ============= ", " ============= ", " ==*********== ", " ==*********== ", " ==*********== ", " ==*********== ", " ==*********== ", " ==*********== ", " ============= ", " ============= ", " ==*********== ", " ==*********== ", " ==*********== ", " ==*********== ", " ==*********== ", " ==*********== ", " ============= ", " ============= ", " ", }; CustomCursor::CustomCursor(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); QPixmap pixmap(xpmCursor); QCursor cursor(pixmap); //加載xpm生成的圖標文件 setCursor(cursor); //設置自定義的鼠標樣式 }
版權聲明:本文為CSDN博主「有何不為」的原創文章.
原文鏈接://blog.csdn.net/Staranywhere/article/details/87895321
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: