Happy coder :Qt 自定义 Button 之美

✿正文   

        在不支持qml的Qt 4.6 ,很多时候,需要我们自定义控件。其实自定义一点也不难,并不是要从头开始写一个控件类,而是继承它,在其基础上,加些佐料,锦上添点花。

        曾经也有人对我的方案产生了质疑,说qss就可以解决,为什么要费劲自己写?

        我想说,第一qss不能解决全部,写一个功能函数无非是3、4行代码的事情,不费劲,同时所有特效都封装在一起,统一管理方便工程中任何一个widget的调用。第二,vim可以打开读文件,为什么还有cat的存在?一个道理,可有其利。第三,对于嵌入式终端的UI实现方案来说,越多越好,在比较下根据实际情况选取最优,因为嵌入式对文件系统大小,库的裁剪要求比较严格。

 

        以Button为例吧,为什么要以Button为例,因为在我看来,Button是万能的,只是看你会不会用。如果把Button属性设置为flat平滑且不可点击,那么它就是个label;如果把许多上述的label按一定的顺序和布局排成行或列,那它就是listview……加载不同背景图片、点击、悬浮、动画、click on槽灵活处理,等等把所有属性都打乱随机组合,没有不能拼凑出的效果和特效。

 

✿某phone 抽奖小例子

鼠标悬浮于按钮上时,加载诱惑iphone图片,离开加载苹果标,点击加载叹气背景…偷笑


 


✿其实还可以加载更多

 


✿My button list鼠标划过

 


✿随你拖

 


✿关于自定义Button和特效方法的封装

  
  1. Button::Button(QWidget *parent) : QPushButton(parent)  
  2. {  
  3.         //保存图片成员初始化  
  4.         buttonPicture = new QPixmap();  
  5.         pressPicture = new QPixmap();  
  6.         releasePicture = new QPixmap();  
  7.   
  8.         enterPicture = new QPixmap();  
  9.         leavePicture = new QPixmap();  
  10.   
  11.         //关闭按钮的默认显示  
  12.         this -> setFlat(true);  
  13.         this->setFocusPolicy(Qt::NoFocus);  
  14.   
  15.         //初始化flag  
  16.         flag=false;  
  17.   
  18.   
  19. }  
  20.   
  21. void Button::setButtonPicture(QPixmap pic)  
  22. {  
  23.         *buttonPicture = pic;  
  24.   
  25.         this -> setIcon(QIcon(*buttonPicture));  
  26. }  
  27.   
  28. void Button::setPressPicture(QPixmap pic)  
  29. {  
  30.         *pressPicture = pic;  
  31. }  
  32.   
  33. void Button::setReleasePicture(QPixmap pic)  
  34. {  
  35.         *releasePicture = pic;  
  36. }  
  37.   
  38. void Button::setEnterPicture(QPixmap pic)  
  39. {  
  40.     *enterPicture = pic;  
  41. }  
  42.   
  43. void Button::setLeavePicture(QPixmap pic)  
  44. {  
  45.     *leavePicture = pic;  
  46. }  
  47.   
  48. void Button::set_X_Y_width_height(int x, int y, int width, int height)  
  49. {  
  50.         this -> setIconSize(QSize(width, height));  
  51.         this -> setGeometry(x, y, width, height);  
  52. }  
  53.   
  54. void Button::mouseDoubleClickEvent(QMouseEvent *event)  
  55. {  
  56.         //null  
  57. }  
  58.   
  59. void Button::mousePressEvent (QMouseEvent *event)  
  60. {  
  61.         this -> setIcon (QIcon(*pressPicture));  
  62. }  
  63.   
  64. void Button::mouseMoveEvent(QMouseEvent *event)  
  65. {  
  66.         //null  
  67. }  
  68.   
  69.   
  70. void Button::mouseReleaseEvent (QMouseEvent *event)  
  71. {  
  72.         this -> setIcon(QIcon(*releasePicture));  
  73.         emit clicked();  
  74. }  
  75.   
  76. void Button::enterEvent(QEvent *)  
  77. {  
  78.     this->setIcon(QIcon(*enterPicture));  
  79.     flag=true;  
  80.   //  this->resizeit();  
  81.   
  82. }  
  83.   
  84. void Button::leaveEvent(QEvent *)  
  85. {  
  86.     this->setIcon(QIcon(*leavePicture));  
  87.     flag=false;  
  88. }  
  89.   
  90. void Button::resizeit(int w , int h)  
  91. {  
  92.     this->raise();  
  93.     this->resize(w,h);  
  94.     this ->setIconSize(QSize(w, h));  
  95.   
  96. }  

把这些方法按照自己的思路,配合paintEvent、Animation、qss等等,美化Qt 应用程序的UI 和动画效果基本没什么问题了。
一时之间想不出还有什么新鲜的需求来写个小demo,就这样吧,欢迎交流。


✿由Button想到的

        

           就一句话:你说什么控件不能自定义啊?


✿后记

         兴趣和快乐是进步的动力和源泉

                                             ——以渔为娱,以渔为乐,以渔为生活。

原文http://blog.csdn.net/yiyaaixuexi/article/details/6706729

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值