Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1755656
  • 博文数量: 198
  • 博客积分: 4088
  • 博客等级: 上校
  • 技术积分: 2391
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-15 16:29
个人简介

游戏开发,系统架构; 博客迁移到:http://www.jianshu.com/u/3ac0504b3b8c

文章分类

全部博文(198)

文章存档

2017年(1)

2016年(12)

2015年(1)

2014年(3)

2013年(13)

2012年(18)

2011年(150)

分类: C/C++

2011-07-20 14:13:45

 

  自定义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;  
}  
 
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

#ifndef WIDGET_H
#define WIDGET_H

#include

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  
#include  
#include  
#include   
 
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

阅读(34286) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~