#include "VButton.h" VButton::VButton( QWidget* parent, const QPoint& pos, const QSize& size, CONTROL_PROPERTY* pProperty ) : QPushButton(parent) , VControlObject(pProperty) { // 设置默认属性(初始化值或者外部传入值) if (m_Property.m_strText.isEmpty()) { m_Property.m_strText = "Button"; } QPushButton::setText(m_Property.m_strText); QPushButton::setToolTip(m_Property.m_strTip); this->setTextColor(m_Property.m_clrText); this->setBgColor(m_Property.m_clrBg); QPushButton::setFont(m_Property.m_Font); QPushButton::setEnabled(m_Property.m_bEnable); // 设置尺寸 if (size == DEFAULT_CONTROL_SIZE) { this->resize(DEFAULT_BUTTON_SIZE); } else { this->resize(size); } // 设置中心点坐标 QPoint tempPos; tempPos.setX(pos.x() - width() / 2); tempPos.setY(pos.y() - height() / 2); // 设置位置 this->move(tempPos); m_pWidget = this; m_Type = VALUE_TYPE::Control_Button; } VButton::~VButton() { } // 属性的实现函数 void VButton::setText(const QString& title) { // qDebug() << m_Property.m_strText; if (this->m_Property.m_strText != title) { this->m_Property.m_strText = title; QPushButton::setText(title); } } void VButton::setTip(const QString& tip) { if (this->m_Property.m_strTip != tip) { this->m_Property.m_strTip = tip; QPushButton::setToolTip(tip); } } void VButton::setTextColor(const QColor& color) { QString qss = VControlObject::textColorQss(color); this->setStyleSheet(qss); } void VButton::setBgColor(const QColor& color) { QString qss = VControlObject::bgColorQss(color); this->setStyleSheet(qss); //// 或者用纯代码的方式实现 ////QPalette palette = this->palette(); ////palette.setColor(QPalette::Button, color); ////this->setPalette(palette); ////this->setAutoFillBackground(true); ////this->setFlat(true); } void VButton::setFont(const QFont& font) { if (this->m_Property.m_Font != font) { this->m_Property.m_Font = font; QPushButton::setFont(font); } } void VButton::setEnable(const bool enable) { if (this->m_Property.m_bEnable != enable) { this->m_Property.m_bEnable = enable; QPushButton::setEnabled(enable); } }