在X11上,Qt支持Xcursor库,可以随意更改全彩色的Cursor主题。但是在嵌入式Qt开发中,没有Xcursor的支持,效果就没这么乐观。
总结一下,我们可以通过以下三类方式修改Qt应用程序的Cursor样式:
1. Qt::CursorShape
Qt提供以下CursorShape,如下图:
代码:
[cpp]
view plain
copy
print
?
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- setCursor(QCursor(Qt::OpenHandCursor));
- }
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); setCursor(QCursor(Qt::OpenHandCursor)); }
效果:
对于桌面应用程序开发,系统主题可替代Qt::CursorShape的Cursor样式,比如,当我使用Qt::WaitCursor,程序运行在UBuntu时的样式为:
对于常用嵌入式开发组合Qt-embedded+linux+arm来说,正如Qt::CursorShape原样。
2.QPixmap or QBitmap
代码:
[cpp]
view plain
copy
print
?
- 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) ;
- }
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,如图:
对于常用嵌入式开发组合Qt-embedded+linux+arm来说,如果使用QWS来启动应用程序,则好好的会变成了。只认识黑白2值,没办法……色盲……
3. x pixmap (xpm)
代码:
[cpp]
view plain
copy
print
?
- 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);
- }
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[]形状可以清晰的看到样式效果,如图:
其实xpm的方式和pixmap原理相同,Linux下我们可以轻松通过命令转换图片格式,如png转xpm:
convert 1.png xpm:2.xpm
Windows下也有不少转换软件和工具,可以完成一键转换。