Qt应用程序Cursor样式
在X11上,Qt支持Xcursor库,可以随意更改全彩色的Cursor主题。但是在嵌入式Qt开发中,没有Xcursor的支持,效果就没这么乐观。
总结一下,我们可以通过以下三类方式修改Qt应用程序的Cursor样式:
效果图:
总结一下,我们可以通过以下三类方式修改Qt应用程序的Cursor样式:
1. Qt::CursorShape
Qt提供以下CursorShape,如下图:![](http://hi.csdn.net/attachment/201112/29/6157840_1325147073H3kN.png)
代码:
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- setCursor(QCursor(Qt::OpenHandCursor));
- }
效果图:
![](http://hi.csdn.net/attachment/201112/30/6157840_1325236463M4JJ.gif)
对于桌面应用程序开发,系统主题可替代Qt::CursorShape的Cursor样式,比如,当我使用Qt::WaitCursor,程序运行在UBuntu时的样式为:
![](http://hi.csdn.net/attachment/201112/30/6157840_1325236840j3tY.gif)
对于常用嵌入式开发组合Qt-embedded+linux+arm来说,正如Qt::CursorShape原样。
2.QPixmap or QBitmap
代码:- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- QCursor cursor ;
- QPixmap pixmap("cursor.png") ;
- cursor = QCursor(pixmap,-1,-1);
- setCursor(cursor) ;
- }
对于桌面应用程序开发,可实现全彩色的自定义Cursor,如图:
![](http://hi.csdn.net/attachment/201112/30/6157840_1325237246LU3H.gif)
对于常用嵌入式开发组合Qt-embedded+linux+arm来说,如果使用QWS来启动应用程序,则好好的
![](http://hi.csdn.net/attachment/201110/19/6157840_1319016005TY39.png)
![](http://hi.csdn.net/attachment/201110/19/6157840_1319016005EWq8.png)
3. x pixmap (xpm)
代码:
- static const char *const cursor_xpm[] = {
- "15 15 3 1",
- " c None",
- ". c #0000aa", //.的颜色
- "* c #aa0000", //*的颜色
- " ..... ",
- " ..*****.. ",
- " . *** . ",
- " . *** . ",
- " . *** . ",
- ". *** .",
- ". ***** .",
- ".*************.",
- ". ***** .",
- ". *** .",
- " . *** . ",
- " . *** . ",
- " . *** . ",
- " ..*****.. ",
- " ..... "
- };
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- QCursor myCursor(cursor_xpm);
- setCursor(myCursor);
- }
从cursor_xpm[]形状可以清晰的看到样式效果,如图:
![](http://hi.csdn.net/attachment/201112/30/6157840_1325239072HRZ3.gif)
其实xpm的方式和pixmap原理相同,Linux下我们可以轻松通过命令转换图片格式,如png转xpm:
convert 1.png xpm:2.xpm
Windows下也有不少转换软件和工具,可以完成一键转换。
也许你会有这样一个疑问,为何不直接用QPixmap貼原图片,何必转成xpm?我想说,多知道一些途径不是坏事,对你不实用可以不用,当然,xpm绝对有它的用武之地,和它不可磨灭的意义……