VButton.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "VButton.h"
  2. VButton::VButton(
  3. QWidget* parent,
  4. const QPoint& pos,
  5. const QSize& size,
  6. CONTROL_PROPERTY* pProperty
  7. )
  8. : QPushButton(parent)
  9. , VControlObject(pProperty)
  10. {
  11. // 设置默认属性(初始化值或者外部传入值)
  12. if (m_Property.m_strText.isEmpty())
  13. {
  14. m_Property.m_strText = "Button";
  15. }
  16. QPushButton::setText(m_Property.m_strText);
  17. QPushButton::setToolTip(m_Property.m_strTip);
  18. this->setTextColor(m_Property.m_clrText);
  19. this->setBgColor(m_Property.m_clrBg);
  20. QPushButton::setFont(m_Property.m_Font);
  21. QPushButton::setEnabled(m_Property.m_bEnable);
  22. // 设置尺寸
  23. if (size == DEFAULT_CONTROL_SIZE)
  24. {
  25. this->resize(DEFAULT_BUTTON_SIZE);
  26. }
  27. else
  28. {
  29. this->resize(size);
  30. }
  31. // 设置中心点坐标
  32. QPoint tempPos;
  33. tempPos.setX(pos.x() - width() / 2);
  34. tempPos.setY(pos.y() - height() / 2);
  35. // 设置位置
  36. this->move(tempPos);
  37. m_pWidget = this;
  38. m_Type = VALUE_TYPE::Control_Button;
  39. }
  40. VButton::~VButton()
  41. {
  42. }
  43. // 属性的实现函数
  44. void VButton::setText(const QString& title)
  45. {
  46. // qDebug() << m_Property.m_strText;
  47. if (this->m_Property.m_strText != title)
  48. {
  49. this->m_Property.m_strText = title;
  50. QPushButton::setText(title);
  51. }
  52. }
  53. void VButton::setTip(const QString& tip)
  54. {
  55. if (this->m_Property.m_strTip != tip)
  56. {
  57. this->m_Property.m_strTip = tip;
  58. QPushButton::setToolTip(tip);
  59. }
  60. }
  61. void VButton::setTextColor(const QColor& color)
  62. {
  63. QString qss = VControlObject::textColorQss(color);
  64. this->setStyleSheet(qss);
  65. }
  66. void VButton::setBgColor(const QColor& color)
  67. {
  68. QString qss = VControlObject::bgColorQss(color);
  69. this->setStyleSheet(qss);
  70. //// 或者用纯代码的方式实现
  71. ////QPalette palette = this->palette();
  72. ////palette.setColor(QPalette::Button, color);
  73. ////this->setPalette(palette);
  74. ////this->setAutoFillBackground(true);
  75. ////this->setFlat(true);
  76. }
  77. void VButton::setFont(const QFont& font)
  78. {
  79. if (this->m_Property.m_Font != font)
  80. {
  81. this->m_Property.m_Font = font;
  82. QPushButton::setFont(font);
  83. }
  84. }
  85. void VButton::setEnable(const bool enable)
  86. {
  87. if (this->m_Property.m_bEnable != enable)
  88. {
  89. this->m_Property.m_bEnable = enable;
  90. QPushButton::setEnabled(enable);
  91. }
  92. }