提要
继承QPushButton后的按钮,设置其样式。
实现
在自定义的控件类中添加Q_OBJECT宏
在自定义的控件的类中重写paintevent函数
自定义的控件在使用时,必须指明其父对象
使用样式表设置样式,将类名改为自定义的类名
例子
直接上代码
myButton.h
#ifndef MYBUTTON_H
#define MYBUTTON_H
#include <QWidget>
#include <QPushButton>
class myButton : public QPushButton
{
Q_OBJECT
public:
myButton(int index,QWidget *parent = nullptr);
~myButton();
int getIndex();//获取按钮的下标
void setSceneBtnId(int &id);//设置场景按钮的id
void setSceneBtnName(QString &name);//设置场景按钮的名字
void setSceneBtnNameId(int id,QString name);//设置场景按钮id
int getSceneBtnId();//获取场景按钮id
QString getScenBtnName();//获取场景按钮的名称
void initBtnStyle();//设置按钮样式
void getAdjustScreenRatio();//获取屏幕分辨率
public slots:
void slot_setSceneBtnName(QString strName);//设置场景按钮的名称
void slot_setSceneBtnId(int id);//设置场景按钮id
protected:
void paintEvent(QPaintEvent *e);
void mousePressEvent(QMouseEvent *e);
private:
int m_index;//按钮的下标,用于区别点击的是哪个按钮
int m_id;//按钮的id
QString m_strName;//按钮的名称
};
#endif // MYBUTTON_H
myButton.cpp
#include "mybutton.h"
//#include "screenresolution.h"
#include <QMouseEvent>
#include <QStyleOption>
#include <QBitmap>
#include <QPixmap>
#include <QPainter>
myButton::myButton(int index,QWidget *parent)
:QPushButton(parent),m_index(index)
{
initBtnStyle();
}
myButton::~myButton()
{
}
int myButton::getIndex()
{
return m_index;
}
void myButton::setSceneBtnId(int &id)
{
m_id = id;
}
void myButton::setSceneBtnName(QString &name)
{
m_strName = name;
}
void myButton::setSceneBtnNameId(int id, QString name)
{
m_id = id;
m_strName = name;
}
int myButton::getSceneBtnId()
{
return m_id;
}
QString myButton::getScenBtnName()
{
return m_strName;
}
void myButton::getAdjustScreenRatio()
{
}
void myButton::initBtnStyle()
{
QPixmap pixmap;
QBitmap bit;
pixmap.load(":/new/prefix1/images/sceneBtn.png");
pixmap = pixmap.scaled(QSize(181 ,74),Qt::KeepAspectRatio);
setFixedSize(pixmap.size());
bit = pixmap.mask();
setMask(bit);
this->setStyleSheet("myButton{background-color:transparent;"
"border-image: url(:/new/prefix1/images/sceneBtn.png);"
"border: 1px solid #0D79FF;opacity: 0.7;"
"font-size: 18px;font-family: Microsoft YaHei;"
"font-weight: 400;color: #FFFFFF;}"
"myButton:pressed{border-image: url(:/new/prefix1/images/sysbtnPressed.png);"
"font-size: 18px;font-family: Microsoft YaHei;"
"font-weight: 400;color: rgba(255, 255, 255, 0.8);line-height: 18px;}"
);
}
void myButton::slot_setSceneBtnName(QString strName)
{
m_strName = strName;
}
void myButton::slot_setSceneBtnId(int id)
{
m_id = id;
}
void myButton::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
QPushButton::paintEvent(e);
}
void myButton::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::RightButton)
{
//创建编辑框,若已经创建则直接显示,绑定编辑框与按钮来存储修改的信息
}
QPushButton::mousePressEvent(e);
}
以上就是自定义控件的类,其使用如下,附上关键代码:
myButton *btn = new myButton(0, this);//指出父类
btn->setText("自定义类");
btn->setGeometry(0,0,200,200);
以上就是自定义控件样式的设置。由于我重写鼠标点击事件的时候,忘记了父类的鼠标点击事件导致事件没有传递下去,从而没有出现点击后的效果,纠结了许久,终得解决。