#include "qcustombutton.h"
#include <QVBoxLayout>
QCustomButton::QCustomButton(QWidget *parent) : QPushButton(parent)
{
imageLbl=new QLabel(this);
imageLbl->setObjectName("imageLbl");//QCustomButton imageLbl
imageLbl->setFixedSize(55,55);
imageLbl->setStyleSheet(QString("QLabel{background-color:rgba(243,62,32,0.5);}"));//QCustomButton
textLbl=new QLabel;
textLbl->setObjectName("textLbl");
textLbl->setStyleSheet("QLabel{background-color:rgba(243,62,32,1);}");
QVBoxLayout *mainLayout=new QVBoxLayout;
mainLayout->addWidget(imageLbl,0,Qt::AlignCenter);
mainLayout->addWidget(textLbl,0,Qt::AlignCenter);
mainLayout->setSpacing(10);
this->setLayout(mainLayout);
}
void QCustomButton::setImageLbl(const QPixmap &pixmap)
{
imageLbl->setPixmap(pixmap);
imageLbl->setScaledContents(true);
}
void QCustomButton::setNormalImagePath(const QPixmap &pixmap)
{
normalImage=pixmap;
}
void QCustomButton::setHoverImagePath(const QPixmap &pixmap)
{
hoverImage=pixmap;
}
void QCustomButton::setPressedImagePath(const QPixmap &pixmap)
{
pressedImage=pixmap;
}
void QCustomButton::setTextLbl(QString text)
{
textLbl->setText(text);
}
QLabel *QCustomButton::getImageHandle()
{
return imageLbl;
}
QLabel *QCustomButton::getTextHandle()
{
return textLbl;
}
bool QCustomButton::event(QEvent *e)
{
if(this->isChecked())
return QWidget::event(e);
switch (e->type()) {
case QEvent::Enter:
{//鼠标进入事件
setImageLbl(hoverImage);
return true;
break;
}
case QEvent::MouseButtonPress:
{//鼠标摁下事件
setImageLbl(pressedImage);
this->setChecked(true);
return true;
break;
}
case QEvent::Leave:
{//鼠标离开事件
setImageLbl(normalImage);
return true;
break;
}
default:
break;
}
return QWidget::event(e);
}