自定义Qt按钮

  Qt自带的PushButton样式比较单一,在开发的时候往往按钮的形状各异,所以需要自定义Qt的按钮。其方法是做一张图片来作为按钮,如果需要动态效果的话,可以做两张图片进行替换。按钮的载体可以是QLabel、QPushButton,可以通过QStyle类来设计样式,如果对QStyle不太了解的话,可以用下面的方法来实现。

1. 使用QPushButton

    通过自定义一个按钮样式函数,在该函数中设置按钮的样式。(可以设计一个QPushButton的子类来完成设置)

实现代码:

view plaincopy to clipboardprint?
QPushButton *custButton(QString str,QString str1)  
{  
    QPushButton *pushButton= new QPushButton;  
 
    pushButton->setGeometry(10,10,200,200); //按钮的位置及大小  
    pushButton->clearMask();  
    pushButton->setBackgroundRole( QPalette::Base);  
 
    QPixmap mypixmap;   mypixmap.load(str);  
 
    pushButton->setFixedSize( mypixmap.width(), mypixmap.height() );  
    pushButton->setMask(mypixmap.createHeuristicMask());  
    pushButton->setIcon(mypixmap);  
    pushButton->setIconSize(QSize(mypixmap.width(),mypixmap.height()));  
    pushButton->setToolTip(str1);  
    return pushButton;  

QPushButton *custButton(QString str,QString str1)
{
    QPushButton *pushButton= new QPushButton;

    pushButton->setGeometry(10,10,200,200); //按钮的位置及大小
    pushButton->clearMask();
    pushButton->setBackgroundRole( QPalette::Base);

    QPixmap mypixmap;   mypixmap.load(str);

    pushButton->setFixedSize( mypixmap.width(), mypixmap.height() );
    pushButton->setMask(mypixmap.createHeuristicMask());
    pushButton->setIcon(mypixmap);
    pushButton->setIconSize(QSize(mypixmap.width(),mypixmap.height()));
    pushButton->setToolTip(str1);
    return pushButton;
}

调用代码:

view plaincopy to clipboardprint?
QPushButton *btn=custButton("../login.png", "LOGIN");  
 
connect(btn, SIGNAL(clicked()), this, SLOT(slotLogin())); 
    QPushButton *btn=custButton("../login.png", "LOGIN");

    connect(btn, SIGNAL(clicked()), this, SLOT(slotLogin()));

2. 通过QLabel

   我们可以把一个图片放在QLabel里面作为按钮,因为我没有找到QLabel是否有当点击后发出的信号,所以自定义了一个鼠标事件用来检测是否在QLabel上点击了鼠标。在自定义的鼠标事件中检测QLabel所在的区域,当在该区域发生鼠标点击事件后,发送信号。

   设计时通过Qt Creator在widget.ui中加入一个QLabel即可,不需要进行设置。

代码widget.h

view plaincopy to clipboardprint?
#ifndef WIDGET_H 
#define WIDGET_H 
 
#include <QWidget>  
 
namespace Ui {  
    class Widget;  
}  
 
class Widget : public QWidget {  
    Q_OBJECT  
public:  
    Widget(QWidget *parent = 0);  
    ~Widget();  
 
signals:  
    void clicked();  
 
protected:  
    void mousePressEvent(QMouseEvent *e);  
 
protected slots:  
    void slotClicked();  
 
private:  
    Ui::Widget *ui;  
}; 
 
#endif // WIDGET_H 
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
    class Widget;
}

class Widget : public QWidget {
    Q_OBJECT
public:
    Widget(QWidget *parent = 0);
    ~Widget();

signals:
    void clicked();

protected:
    void mousePressEvent(QMouseEvent *e);

protected slots:
    void slotClicked();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

代码widget.cpp

view plaincopy to clipboardprint?
#include "widget.h" 
#include "ui_widget.h" 
#include <QMouseEvent> 
#include <QMessageBox> 
#include <QPixmap> 
#include <QLabel>  
 
Widget::Widget(QWidget *parent) :  
    QWidget(parent),  
    ui(new Ui::Widget)  
{  
    ui->setupUi(this);  
    //使用label作为按钮,通过自定义鼠标事件,点击label所在区域实现鼠标单击  
    QPixmap pm;   pm.load("../logo.png");  
    ui->label->setGeometry(0,0,pm.width(), pm.height());  
    ui->label->setPixmap(pm);  
 
    connect( this, SIGNAL(clicked()), this, SLOT(slotClicked()));   //信号连接  
}  
 
Widget::~Widget()  
{  
    delete ui;  
}  
 
 
void Widget::mousePressEvent(QMouseEvent *e)  
{  
    int x = e->x();  
    int y = e->y();  
 
    //假如在QRect( 0, 0, 48, 48 )这个区域里(图片大小为48X48),就发出信号  
    if (x>0 && x<48 && y>0 && y<48){  
        emit clicked();  
    }  
}  
 
void Widget::slotClicked()  
{  
    QMessageBox::about( this, "Mouse Example", "You have pressed mouse, exit now!");  
    close();  
 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/starcloud_zxt/archive/2010/01/13/5185556.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值