用Qt设计一个属于自己的图标按钮

原文地址::http://blog.csdn.net/u010101067/article/details/41578315


 学c++以来,用qt做过一些界面,个人感觉软件界面还是比较漂亮的,但是如果界面想嵌入一些漂亮的图标做按钮,利用qt本身已有的控件实现就有点困难了,当然可以通过为QPushButton类添加背景图标来实现,但是自我感觉效果不是很好,所以写了这篇博客,想实现一下用纯图片来做按钮控件,通过改变图片的观感,比如图片的形状大小,来表示按钮的选中或者未选中,同时为这图片按钮添加鼠标事件,从而模拟一个按键出来,这样的好处主要是它能为界面增色,不同的图片,形状,大小以及按钮选中和未选中所展现给用户的不同观感将大大提高界面整体的美化程度。

设计思路主要通过重载Qt的几个事件实现,

void mousePressEvent(QMouseEvent *event);

    void paintEvent(QPaintEvent *event);

    void enterEvent(QEvent *event);

void leaveEvent(QEvent *event);

同时还需要为图标按钮添加一个信号函数,void isclicked(),由该函数发送鼠标按下的信号,供其他类使用。

代码简要分析:

/*IconButton类的构造函数*/

IconButton::IconButton(QString iconStr1,QString iconStr2,QWidget *parent)

    : QWidget(parent, Qt::FramelessWindowHint)

{

 

    //加载图片作为程序的界面

    m_Pixmap.load(iconStr1);

    m_Pixmap1.load(iconStr2);

    if(m_Pixmap1.size().width()<m_Pixmap.size().width())

    {

        m_Pixmap.load(iconStr2);

        m_Pixmap1.load(iconStr1);

    }

    leave=true; //初始化图标按钮为未选中状态

    this->x=0;  //初始化图标按钮的位置

    this->y=0;

}

/*重载mousePressEvent函数*/

void IconButton::mousePressEvent(QMouseEvent *event)

{

    //按下左键则发送按下信号

    if(event->button() == Qt::LeftButton)

    {

        emit isclicked();

    }

}

/*重载paintEvent函数*/

void IconButton::paintEvent(QPaintEvent *event)

{

    QPainter painter(this);

//分两种情况来显示不同的外观,未选中时画m_Pixmap,选中时画m_Pixmap1

    if(leave)

    {

        resize( m_Pixmap.size() );

        painter.drawPixmap(0,0, m_Pixmap);

        move(x+(m_Pixmap1.size().width()-m_Pixmap.size().width())/2,y+(m_Pixmap1.size().width()-m_Pixmap.size().width())/2);

    }

   else

    {

        resize( m_Pixmap1.size() );

        painter.drawPixmap(0, 0, m_Pixmap1);

        move(x,y);

        this->setToolTip(iconTip);

    }

}

/*重载:leaveEvent函数*/

void IconButton::leaveEvent(QEvent *event)

{

    //鼠标离开窗口时是普通的指针

    setCursor(Qt::ArrowCursor);

    leave=true;

    update(); //调用paintevent函数进行更新

}

/*重载:enterEvent函数*/

void IconButton::enterEvent(QEvent *event)

{

    //鼠标留在窗口上时是一个手指,表示可以读取按键事件

    setCursor(Qt::PointingHandCursor);

    leave=false;

    update();//调用paintevent函数进行更新

}

//下面是供给用户使用的两个基本接口,分别是设置图标按钮的位置以及需要显示的提示信息,这里只简单的写了两个功能,还可以增加其他功能,使这个IconButton类更加完善

void IconButton::setPosition(int x,int y)

{

    this->x=x;

    this->y=y;

}

void IconButton::setIconTip(QString iconTip)

{

    this->iconTip=iconTip;

}

 

以下是实现该类的源码:

iconButton.h

[cpp]  view plain  copy
  1. #ifndef ICONBUTTON_H  
  2. #define ICONBUTTON_H  
  3.   
  4. #include <QtGui/QWidget>  
  5. class IconButton : public QWidget  
  6. {  
  7.     Q_OBJECT  
  8. public:  
  9.     IconButton(QString iconStr1,QString iconStr2,QWidget *parent = 0);  
  10.     void setPosition(int x,int y);  
  11.     void setIconTip(QString iconTip);  
  12.   
  13. protected:  
  14.     void mousePressEvent(QMouseEvent *event);  
  15.     void paintEvent(QPaintEvent *event);  
  16.     void enterEvent(QEvent *event);  
  17.     void leaveEvent(QEvent *event);  
  18. signals:  
  19.     void isclicked();  
  20. private:  
  21.     QPixmap m_Pixmap,m_Pixmap1;  
  22.     int x,y;  
  23.     QString iconTip;  
  24.     bool leave;  
  25. };  
  26.   
  27.   
  28. #endif // ICONBUTTON_H  
[cpp]  view plain  copy
  1.    
[cpp]  view plain  copy
  1. iconButton.cpp  
[cpp]  view plain  copy
  1.    
[cpp]  view plain  copy
  1. <pre class="cpp" name="code">#include "iconButton.h"  
  2. #include <QtGui>  
  3.   
  4. IconButton::IconButton(QString iconStr1,QString iconStr2,QWidget *parent)  
  5.     : QWidget(parent, Qt::FramelessWindowHint)  
  6. {  
  7.   
  8.     //加载一幅有部分区域是透明的图片作为程序的界面  
  9.     m_Pixmap.load(iconStr1);  
  10.     m_Pixmap1.load(iconStr2);  
  11.     if(m_Pixmap1.size().width()<m_Pixmap.size().width())  
  12.     {  
  13.         m_Pixmap.load(iconStr2);  
  14.         m_Pixmap1.load(iconStr1);  
  15.     }  
  16.     //setStyleSheet("background-color:lightblue");  
  17.     leave=true;  
  18.     this->x=0;  
  19.     this->y=0;  
  20.   
  21. }  
  22.   
  23. void IconButton::mousePressEvent(QMouseEvent *event)  
  24. {  
  25.     //按住左键关闭程序  
  26.     if(event->button() == Qt::LeftButton)  
  27.     {  
  28.         emit isclicked();  
  29.     }  
  30. }  
  31. void IconButton::setPosition(int x,int y)  
  32. {  
  33.     this->x=x;  
  34.     this->y=y;  
  35. }  
  36. void IconButton::setIconTip(QString iconTip)  
  37. {  
  38.     this->iconTip=iconTip;  
  39. }  
  40.   
  41. void IconButton::paintEvent(QPaintEvent *event)  
  42. {  
  43.     qDebug("paint");  
  44.     QPainter painter(this);  
  45.     if(leave)  
  46.     {  
  47.         resize( m_Pixmap.size() );  
  48.         //setMask( m_Pixmap.mask() );  
  49.         painter.drawPixmap(0,0, m_Pixmap);  
  50.         move(x+(m_Pixmap1.size().width()-m_Pixmap.size().width())/2,y+(m_Pixmap1.size().width()-m_Pixmap.size().width())/2);  
  51.     }  
  52.    else  
  53.     {  
  54.         resize( m_Pixmap1.size() );  
  55.         //setMask( m_Pixmap1.mask() );  
  56.         painter.drawPixmap(0, 0, m_Pixmap1);  
  57.         move(x,y);  
  58.         this->setToolTip(iconTip);  
  59.     }  
  60. }  
  61.   
  62. void IconButton::leaveEvent(QEvent *event)  
  63. {  
  64.     //鼠标离开窗口时是普通的指针  
  65.     setCursor(Qt::ArrowCursor);  
  66.     leave=true;  
  67.     update();  
  68. }  
  69.   
  70. void IconButton::enterEvent(QEvent *event)  
  71. {  
  72.     //鼠标留在窗口上时是一个手指  
  73.     setCursor(Qt::PointingHandCursor);  
  74.     leave=false;  
  75.     update();  
  76. }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值