1.编写自定义按钮

MyButton.h

#ifndef
MYBUTTON_H

#define
MYBUTTON_H

#include
<QWidget>

/**

*
@brief
The
MyButton
class

*
如果想重写button,需要继承QWidget

*/

class
MyButton
:
public
QWidget

{

Q_OBJECT

public:

explicit
MyButton(QWidget
*parent
);

//重写MyButton的构造函数

MyButton(const
QString&
text,
QWidget*
parent =
);

//表示一个矩形

QRect
_rect;

//表示按钮中的文字

QString
_text;

//用来表示是否按下了的布尔值

bool
_pressed;

//表示鼠标按下的事件

void
mousePressEvent(QMouseEvent
*);

//表示鼠标释放的事件

void
mouseReleaseEvent(QMouseEvent
*);

void
paintEvent(QPaintEvent
*);

signals:

//信号,点击的信号

void
clicked();

public
slots:

};

#endif
//
MYBUTTON_H

MyButtonn.cpp

#include "MyButton.h"
#include <QPainter>
#include <QMouseEvent>
 
MyButton::MyButton(QWidget *parent) :
    QWidget(parent), _rect(0, 0, 100, 30), _text(QString())
{
    //通过这个标记按钮是否被按下了
    _pressed = false;
    this->setGeometry(_rect);
}
 
MyButton::MyButton(const QString &text, QWidget *parent):
    QWidget(parent),_text(text),_rect(0,0,100,30)
{
    _pressed = false;
    this->setGeometry(_rect);
}
 
void MyButton::mousePressEvent(QMouseEvent *ev)
{
    _pressed = true;
    update();
}
 
void MyButton::mouseReleaseEvent(QMouseEvent *ev)
{
    _pressed = false;
    update();
 
    if(_rect.contains(ev->pos()))
        emit clicked();
    // callback handler
}
 
void MyButton::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    if(_pressed)
        //如果是按下的,则显示的是黄色的
        p.setBrush(Qt::yellow);
    else
        //如果没有被按下了,回复称灰色的
        p.setBrush(Qt::darkGray);
    p.drawRect(_rect);
    p.drawText(_rect,_text,QTextOption(Qt::AlignCenter));
}

MyWidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H
 
#include <QWidget>
#include "MyButton.h"
 
class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent);
    MyButton* button;
 
    void paintEvent(QPaintEvent *);
 
    void mousePressEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
 
    QVector< QVector<QPoint> > _lines;
signals:
 
public slots:
    void slotButtonClicked();
};
 
#endif // MYWIDGET_H

MyWidget.cpp

#include "MyWidget.h"
#include <QPainter>
#include <QPixmap>
#include <QMouseEvent>
#include <QDebug>
#include <QApplication>
 
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
    button = new MyButton("MyButton", this);
    connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
 
    button->setGeometry(30, 30, 100, 30);
}
 
//槽函数
void MyWidget::slotButtonClicked()
{
    qDebug() << "button is clicked";
}
 
void MyWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    //通过下面的方式实现画多条线
    for(int i = 0; i < _lines.size();++i)
    {
        const QVector<QPoint> & line = _lines.at(i);
        for(int j = 0; j < line.size() - 1;++j)
        {
            p.drawLine(line.at(j),line.at(j + 1));
        }
    }
}
 
/**
 * @brief MyWidget::mouseMoveEvent 鼠标移动的事件
 * @param ev
 */
void MyWidget::mouseMoveEvent(QMouseEvent *ev)
{
    if(_lines.size() == 0)
    {
        QVector<QPoint> line;
        _lines.append(line);
    }
 
    QVector<QPoint>& lastLine = _lines.last();
    lastLine.append(ev->pos());
 
    update();
}
 
void MyWidget::mousePressEvent(QMouseEvent *ev)
{
    QVector<QPoint> line;
    _lines.append(line);
 
    QVector<QPoint>& lastLine = _lines.last();
    lastLine.append(ev->pos());
}
 
void MyWidget::mouseReleaseEvent(QMouseEvent *ev)
{
    QVector<QPoint>& lastLine = _lines.last();
    lastLine.append(ev->pos());
}
 
int main(int argc,char** argv)
{
    QApplication app(argc,argv);
 
    MyWidget w;
    w.show();
 
    return app.exec();
}

鼠标按下的运行结果:

5.Qt自定义Button按钮的实现-LMLPHP

鼠标离开的运行效果:

5.Qt自定义Button按钮的实现-LMLPHP

04-15 09:56