Qt设置鼠标样式QCursor
2019-02-23 19:43
3651 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Staranywhere/article/details/87895321
3、使用XPM生成鼠标样式
Qt 设置鼠标样式
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); //设置自定义的鼠标样式 }
效果如下:
稍加整理方便大家参考,如有错误请指正,谢谢!
相关文章推荐
- Css cursor设置鼠标指针显示样式
- QT怎么设置字体轮廓、字体位置、字体样式、字体间距、窗口背景色大小、隐藏鼠标图标
- css cursor鼠标指针光标样式default pointer hand url
- Qt利用setStyleSheet设置样式
- CSS中cursor属性的鼠标样式明细
- 移动鼠标获取相应的样式(eg:手型、问号、箭头等多种样式)css的style="cursor:*"属性
- css cursor鼠标指针光标样式
- Qt使用教程之指定代码样式设置
- Qt 背景透明、无边框标题栏、鼠标拖动、定时器、获取屏幕分辨率,设置初始位置
- QT 鼠标形状设置
- Qt|Qt之鼠标样式
- qt 设置程序控件样式
- 设置 IMapControl2.MouseIcon 鼠标样式
- css cursor 的可选值(鼠标的各种样式)
- QT QPushButton 通过stylesheet 设置样式
- 网页中自定义鼠标样式,cursor
- QT——绘图事件、鼠标事件、QPainter、键盘事件、光标样式、登录框密码回显模式、移除字符串前后的空白、对话框accept()
- css鼠标样式cursor介绍(鼠标手型)
- 比较全的CSS cursor(鼠标样式)
- Flex中利用stretchCursor样式设置自定义的拉伸光标图标的例子