Qt内置基本的鼠标样式,使用函数QCursor(Qt::CursorShape shape)进行设置。对于不同操作系统来说,设置的Qt鼠标样式会被替换成当前系统支持的鼠标样式效果。
Qt组件推荐:
- QtitanRibbon| 下载试用: 遵循Microsoft Ribbon UI Paradigm for Qt技术的Ribbon UI组件,致力于为Windows、Linux和Mac OS X提供功能完整的Ribbon组件。
- QtitanChart | 下载试用 :是一个C ++库,代表一组控件,这些控件使您可以快速地为应用程序提供漂亮而丰富的图表。并且支持所有主要的桌面
1、使用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); //设置自定义的鼠标样式}
效果如下:

标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!