您的位置:首页 > 编程语言 > Qt开发

Qt圆角功能和状态组合按钮,可以显示颜色或者图片

2016-06-18 16:23 661 查看
使用两个按钮和一个Label封装了一个功能和状态圆角组合按钮,Label用来显示颜色或者图片。

实现的效果如下:


显示图片:



显示红色:



其中颜色或者图片是通过函数设置进去的。

两个按钮:前一个是状态按钮,可以Check,表示使用此项功能;后一个按钮是功能按钮,可以Check,表示跳转到此功能对应的选项。两个按钮都有信号,可以通过信号进行连接你需要的槽函数。

具体实现代码:

#include <QPushButton>
#include <QLabel>

class QStateFunctionButton : public QWidget
{
Q_OBJECT

public:
QStateFunctionButton(QWidget *parent = 0);
~QStateFunctionButton();

void setColor(const QColor &color);
void setImage(const QImage &image);
void setText(const QString &text);

signals:
void stateButtonClicked(bool bClicked);
void functionButtonClicked(bool bClicked);

private:
QPushButton *m_stateButton;//状态按钮
QPushButton *m_functionButton;//功能按钮
QLabel *m_colorImageLabel;//显示图片或者颜色
};
#include "QStateFunctionButton.h"
#include <QHBoxLayout>
#include <QPainter>

QStateFunctionButton::QStateFunctionButton(QWidget *parent)
: QWidget(parent)
{
m_stateButton = new QPushButton(this);
m_functionButton = new QPushButton(this);
m_colorImageLabel = new QLabel(this);

m_stateButton->setCheckable(true);
m_stateButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
m_stateButton->setStyleSheet("QPushButton{border:2px groove gray; border-top-left-radius:16px;border-bottom-left-radius:16px;border-style: outset;}"
"QPushButton:checked{background-color:rgb(85, 170, 255);}");
m_functionButton->setCheckable(true);
m_functionButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
m_functionButton->setStyleSheet("QPushButton{border:2px groove gray; border-top-right-radius:16px;border-bottom-right-radius:16px;border-style: outset;}"
"QPushButton:checked{background-color:rgb(85, 170, 255);}");
m_colorImageLabel->setScaledContents(true);
m_colorImageLabel->setFrameShadow(QFrame::Sunken);
m_colorImageLabel->setFrameShape(QFrame::Shape::Panel);

QHBoxLayout *pHBox = new QHBoxLayout(this);
pHBox->setSpacing(0);
pHBox->setContentsMargins(0, 0, 0, 0);

pHBox->addWidget(m_stateButton, 1);
pHBox->addWidget(m_functionButton, 3);
pHBox->addWidget(m_colorImageLabel, 1);

connect(m_stateButton, &QPushButton::clicked, this, &QStateFunctionButton::stateButtonClicked);
connect(m_functionButton, &QPushButton::clicked, this, &QStateFunctionButton::functionButtonClicked);

setColor(QColor(0, 0, 0, 255));
}

QStateFunctionButton::~QStateFunctionButton()
{

}

void QStateFunctionButton::setColor(const QColor &color)
{
m_colorImageLabel->setAutoFillBackground(true);
QPalette pal = m_colorImageLabel->palette();
pal.setColor(QPalette::Background, color);
m_colorImageLabel->setPalette(pal);
}

void QStateFunctionButton::setImage(const QImage &image)
{
QPixmap pixmap = QPixmap::fromImage(image).scaled(m_colorImageLabel->width(),
m_colorImageLabel->height(),
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
m_colorImageLabel->setPixmap(pixmap);
}

void QStateFunctionButton::setText(const QString &text)
{
m_functionButton->setText(text);
}
交流qq:1245178753
本文地址:http://blog.csdn.net/u011417605/article/details/51706166

源码下载:http://download.csdn.net/detail/u011417605/9552226
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: