你还在用QPushButton吗?
来源:互联网 发布:奇迹暖暖 网络连接失败 编辑:程序博客网 时间:2024/05/17 06:22
有很多朋友都抱怨,为什么自己使Qt做的小项目,UI那么土那么俗,方方框框,基本控件很丑,要不是Qt的跨平台,才不去学习它。呵呵,其实我想说,嵌入式系统中的图形界面,通通交给QT,绝对没问题!
简单说说自定义Button,QPushButton是常用组件之一,先看下效果。
当单击按钮后,效果如图:
实现代码:
- Button::Button(QWidget *parent) : QPushButton(parent)
- {
- //保存图片成员初始化
- buttonPicture = new QPixmap();
- pressPicture = new QPixmap();
- releasePicture = new QPixmap();
- //关闭按钮的默认显示
- this -> setFlat(true);
- }
- void Button::setButtonPicture(QPixmap pic)
- {
- *buttonPicture = pic;
- this -> setIcon(QIcon(*buttonPicture));
- }
- void Button::setPressPicture(QPixmap pic)
- {
- *pressPicture = pic;
- }
- void Button::setReleasePicture(QPixmap pic)
- {
- *releasePicture = pic;
- }
- void Button::set_X_Y_width_height(int x, int y, int width, int height)
- {
- this -> setIconSize(QSize(width, height));
- this -> setGeometry(x, y, width, height);
- }
- void Button::mouseDoubleClickEvent(QMouseEvent *event)
- {
- //null
- }
- void Button::mousePressEvent (QMouseEvent *event)
- {
- this -> setIcon (QIcon(*pressPicture));
- }
- void Button::mouseMoveEvent(QMouseEvent *event)
- {
- //null
- }
- void Button::mouseReleaseEvent (QMouseEvent *event)
- {
- this -> setIcon(QIcon(*releasePicture));
- emit clicked();
- }
这下明白喽?我们只是需要在mousePressEvent和mouseReleaseEvent中,添加setIcon(QIcon(*buttonPicture))的处理,就让Button动起来了o(∩_∩)o ...
顺便说下icon的资源处理,你不要告诉我你不会哦,即使不会,咱可是可以写PS的无所不能的程序员,学一下怎么使用应该不难吧?
将图片抠出所选区域,设置为透明png格式,这样不会覆盖背景。建议大家把文字也做进图片里,而不是在QT里文本,因为不同分辨率不同大小的嵌入式设备屏幕,显示效果不好控制。制作静态(同释放后)与点击状态两种按钮图标。
就是这样!简单吧?
这里预留了mouseDoubleClickEvent(QMouseEvent *event),mouseMoveEvent(QMouseEvent *event)的响应,热爱Qt的朋友不如自己动动手,美化自己的button~
0 0
- 你还在用QPushButton吗?
- 你还在用QPushButton吗?
- 你还在用QPushButton吗?
- 你还在用QPushButton吗?
- 你还在用QPushButton吗?
- 你还在用if else吗
- 你还在用if else吗?
- 你还在用if else吗?
- 你还在用if else吗
- 你还在用if else吗?
- 你还在用if else吗?
- 你还在用if else吗?
- 你还在用if else吗
- 你还在用if else吗?
- 你还在用if else吗?
- 你还在用if else吗?
- 你还在用if else吗?
- 你还在用if else吗?
- android 使用shape 实现圆角图片__不用.9了
- 两个进程共享数据的方法
- Unity3D渲染优化技巧
- URAL 1375. Bill Clevers
- 利用数组建立栈
- 你还在用QPushButton吗?
- Validate Binary Search Tree
- 安卓 图片和文字分享
- 微信公众号下载媒体文件到个人服务器
- C++ 函数中如何接收数量不定的函数参数
- Qt 无边框、透明、可移动、的个性窗体案例详解
- C++对C加强专题总结:namespace,register,bool,三目运算符,类型检测,struct
- 安卓线程相关 HandlerThread Handler Thread Looper Message Runnable
- 单例模式