有很多朋友都抱怨,为什么自己使Qt做的小项目,UI那么土那么俗,方方框框,基本控件很丑,要不是Qt的跨平台,才不去学习它。呵呵,其实我想说,嵌入式系统中的图形界面,通通交给QT,绝对没问题!
简单说说自定义Button,QPushButton是常用组件之一,先看下效果。
当单击按钮后,效果如图:
实现代码:
view plaincopy to clipboardprint?
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();
}
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~
阅读(1099) | 评论(0) | 转发(0) |