轉(zhuǎn)帖|使用教程|編輯:鮑佳佳|2021-06-23 11:06:01.683|閱讀 296 次
概述:Qt內(nèi)置基本的鼠標(biāo)樣式,使用函數(shù)QCursor(Qt::CursorShape shape)進行設(shè)置。對于不同操作系統(tǒng)來說,設(shè)置的Qt鼠標(biāo)樣式會被替換成當(dāng)前系統(tǒng)支持的鼠標(biāo)樣式效果。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Qt是一個跨平臺框架,通常用作圖形工具包,它不僅創(chuàng)建CLI應(yīng)用程序中非常有用。而且它也可以在三種主要的臺式機操作系統(tǒng)以及移動操作系統(tǒng)(如Symbian,Nokia Belle,Meego Harmattan,MeeGo或BB10)以及嵌入式設(shè)備,Android(Necessitas)和iOS的端口上運行?,F(xiàn)在我們?yōu)槟闾峁┝嗣赓M的試用版。
Qt組件推薦:
Qt內(nèi)置基本的鼠標(biāo)樣式,使用函數(shù)QCursor(Qt::CursorShape shape)進行設(shè)置。對于不同操作系統(tǒng)來說,設(shè)置的Qt鼠標(biāo)樣式會被替換成當(dāng)前系統(tǒng)支持的鼠標(biāo)樣式效果。
Qt內(nèi)置的鼠標(biāo)樣式(CursorShape)如下:
比如設(shè)置鼠標(biāo)樣式為Qt::PointingHandCursor:
CustomCursor::CustomCursor(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); setCursor(Qt::PointingHandCursor); //設(shè)置鼠標(biāo)樣式 }效果如下:
2、使用圖片自定義鼠標(biāo)樣式
使用函數(shù)QCursor::QCursor(const QBitmap & bitmap, const QBitmap & mask, int hotX = -1, int hotY = -1),需要準備自定義鼠標(biāo)樣式的圖片和自定義鼠標(biāo)樣式的掩碼圖片,hotX和hotY設(shè)置鼠標(biāo)熱點。甚至可以生成與背景具有反差效果的鼠標(biāo)樣式。該函數(shù)詳細使用說明如下:
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表示鼠標(biāo)熱點在中心點 setCursor(cursor); //設(shè)置自定義的鼠標(biāo)樣式 }
效果如下:
為方便理解,這里將顏色設(shè)為黑色RGB(0,0,0)表示為1像素值,將顏色設(shè)為白色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表示鼠標(biāo)熱點在中心點 setCursor(cursor); //設(shè)置自定義的鼠標(biāo)樣式 }
效果如下:
3、使用XPM生成鼠標(biāo)樣式
XPM用于創(chuàng)建位圖文件,可生成背景透明的圖片。使用函數(shù)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生成的圖標(biāo)文件 setCursor(cursor); //設(shè)置自定義的鼠標(biāo)樣式 }
版權(quán)聲明:本文為CSDN博主「有何不為」的原創(chuàng)文章.
原文鏈接://blog.csdn.net/Staranywhere/article/details/87895321
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: