QT: custom button looking like an url , neat and cool

Let’s check the result first:

button like url

code snippets:
/************************** head file *********************************/

#ifndef BUTTONLIKEURL_H
#define BUTTONLIKEURL_H

#include 
#include 
#include 
class ButtonLikeUrl : public QWidget
{
    Q_OBJECT
public:
    explicit ButtonLikeUrl(QWidget *parent = nullptr);
    void setText(const QString & str);
    void setFontColor(const QColor & color);
    void setFontSize(int );
    void setHoverColor(const QColor & color);
    void setPressColor(const QColor & color);
    void setLeftGap(int);
protected:
    void paintEvent(QPaintEvent *event) ;
    void enterEvent(QEvent *event) ;
    void leaveEvent(QEvent *event) ;
    void mousePressEvent(QMouseEvent * e);
    void mouseReleaseEvent(QMouseEvent *event) ;

private:
    int textLen();
    QString str;
    QColor fontColor;
    QColor hoverColor;
    QColor pressColor;
    bool hovering;
    bool pressing;
    int leftGap;
    int upperGap;//upside gap or downside gap
signals:
    void sigClicked();
};

#endif // BUTTONLIKEURL_H

/****************************** cpp file ****************************/

#include "buttonlikeurl.h"
#include "style.h"
ButtonLikeUrl::ButtonLikeUrl(QWidget *parent) : QWidget(parent)
{
    str = "confirm";
    fontColor = QColor("black");
    hoverColor = skinList[SkinIndex].hover;
    pressColor = skinList[SkinIndex].pressed;
    hovering = false;
    pressing = false;
    upperGap = 6;
    leftGap = 2;
    setFixedSize(50,32);
    QFont f;
    f.setPixelSize(20);
    setFont(f);
}
void ButtonLikeUrl::setFontSize(int h){
    QFont f;
    f.setPixelSize(h);
    setFont(f);
    QFontMetrics m(f);
    int txth = m.height();
    if(txth > height() - upperGap * 2){
        this->setFixedHeight(txth + upperGap * 2);
    }
}
void ButtonLikeUrl::setFontColor(const QColor & color){
    fontColor = color;
    update();
}
void ButtonLikeUrl::setHoverColor(const QColor & color){
    hoverColor = color;
    update();
}
void ButtonLikeUrl::setPressColor(const QColor & color){
    pressColor = color;
    update();
}
void ButtonLikeUrl::setText(const QString & _str){
    str = _str;
    int len = textLen();
    if(len > width() - leftGap * 2){
        setFixedWidth(len + 2 * leftGap);
    }
}
int ButtonLikeUrl::textLen(){
    QFont f = font();
    QFontMetrics m(f);
    int w = m.horizontalAdvance(str);
    return w;
}
void ButtonLikeUrl::setLeftGap(int gap){
    leftGap = gap;
}
void ButtonLikeUrl::paintEvent(QPaintEvent *event) {
    QPainter p(this);
    p.setRenderHints(QPainter::Antialiasing);
    QPen pen;
    if(pressing){
        pen = QPen(pressColor);
    }
    else if(hovering){
        pen = QPen(hoverColor);
    }
    else{
        pen = QPen(fontColor);
    }
    p.setPen(pen);
    int y = height();

    p.drawText(leftGap,y-upperGap - 3,str);
    if(pressing | hovering){
        //draw an underline
        p.drawLine(leftGap,y-upperGap,leftGap + textLen(),y-upperGap);
    }
}
void ButtonLikeUrl::enterEvent(QEvent *event) {
    hovering = true;
    QCursor cur = cursor();
    cur.setShape(Qt::PointingHandCursor);
    setCursor(cur);
    update();
}
void ButtonLikeUrl::leaveEvent(QEvent *event) {
    hovering = false;
    QCursor cur = cursor();
    cur.setShape(Qt::ArrowCursor);
    setCursor(cur);
    update();
}
void ButtonLikeUrl::mousePressEvent(QMouseEvent * e){
    pressing = true;
    update();
}
void ButtonLikeUrl::mouseReleaseEvent(QMouseEvent *event) {
    pressing = false;
    emit sigClicked();
    update();
}

你可能感兴趣的:(QT,qt,c++,java)