找回密码
 立即注册
收起左侧

Qt编写超级多态按钮WKButton

0
回复
9078
查看
[复制链接]
累计签到:7 天
连续签到:1 天
来源: 2018-3-16 14:15:47 显示全部楼层 |阅读模式
一个朋友需要定制一个按钮部分需求如下:
1.可以设置圆角(Radius)值。
2.可以设置边框的粗细(BorderSize)和颜色(BorderColor)
3.背景颜色有两种颜色RColor和PColor。
4.背景颜色显示模式有3种:
a)松开状态的颜色为RColor;按下状态的颜色为PColor。
b)松开状态的颜色为按键上半部分是RColor,按键下半部分为PColor;按下状态的颜色为按键上半部分是PColor,按键下半部分为RColor。
c)松开状态的颜色为RColor到PColor的渐变色;按下状态的颜色为按键上半部分是PColor到RColor的渐变色。
5.按键内部可以显示3行文字(Text[3]),并对每行文字设置字体、对齐、大小、颜色。
6.可以处理鼠标按下、松开、移动、拖拽的事件
7.可以显示图片,当显示图片时自动覆盖背景颜色和文字,但保留边框和圆角。
实现效果:

集成在QUC自定义控件中



核心代码:
  1. void WKButton::paintEvent(QPaintEvent *)
  2. {
  3.     //绘制准备工作,启用反锯齿
  4.     QPainter painter(this);
  5.     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

  6.     //绘制背景
  7.     drawBg(&painter);
  8.     //绘制文字
  9.     drawText(&painter);
  10. }

  11. void WKButton::drawBg(QPainter *painter)
  12. {
  13.     painter->save();

  14.     //设置边框颜色及宽度
  15.     QPen pen;
  16.     pen.setColor(borderColor);
  17.     pen.setWidthF(borderWidth);
  18.     painter->setPen(pen);

  19.     //绘制区域要减去边框宽度
  20.     QRect rect;
  21.     rect.setX(borderWidth);
  22.     rect.setY(borderWidth);
  23.     rect.setWidth(width() - borderWidth * 2);
  24.     rect.setHeight(height() - borderWidth * 2);

  25.     //如果背景图片存在则显示背景图片,否则显示背景色
  26.     if (!bgImage.isNull()) {
  27.         //等比例缩放绘制
  28.         QPixmap img = bgImage.scaled(rect.width(), rect.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
  29.         painter->drawPixmap((this->rect().width() - img.width()) / 2, (this->rect().height() - img.height()) / 2, img);
  30.     } else {
  31.         if (colorMode == ColorMode_Normal) {
  32.             if (isPressed) {
  33.                 painter->setBrush(QBrush(pressedColor));
  34.             } else {
  35.                 painter->setBrush(QBrush(normalColor));
  36.             }
  37.         } else if (colorMode == ColorMode_Replace) {
  38.             QLinearGradient gradient(QPoint(0, 0), QPoint(0, height()));

  39.             if (isPressed) {
  40.                 gradient.setColorAt(0.0, pressedColor);
  41.                 gradient.setColorAt(0.49, pressedColor);
  42.                 gradient.setColorAt(0.50, normalColor);
  43.                 gradient.setColorAt(1.0, normalColor);
  44.             } else {
  45.                 gradient.setColorAt(0.0, normalColor);
  46.                 gradient.setColorAt(0.49, normalColor);
  47.                 gradient.setColorAt(0.50, pressedColor);
  48.                 gradient.setColorAt(1.0, pressedColor);
  49.             }

  50.             painter->setBrush(gradient);
  51.         } else if (colorMode == ColorMode_Shade) {
  52.             QLinearGradient gradient(QPoint(0, 0), QPoint(0, height()));

  53.             if (isPressed) {
  54.                 gradient.setColorAt(0.0, pressedColor);
  55.                 gradient.setColorAt(1.0, normalColor);
  56.             } else {
  57.                 gradient.setColorAt(0.0, normalColor);
  58.                 gradient.setColorAt(1.0, pressedColor);
  59.             }

  60.             painter->setBrush(gradient);
  61.         }

  62.         painter->drawRoundedRect(rect, borderRadius, borderRadius);
  63.     }

  64.     painter->restore();
  65. }

  66. void WKButton::drawText(QPainter *painter)
  67. {
  68.     if (!bgImage.isNull()) {
  69.         return;
  70.     }

  71.     painter->save();

  72.     //如果要显示角标,则重新计算显示文字的区域
  73.     if (showSuperText) {
  74.         int offset = 3;
  75.         QRect rect;
  76.         rect.setX(borderWidth * offset);
  77.         rect.setY(borderWidth);
  78.         rect.setWidth(width() - borderWidth * offset * 2);
  79.         rect.setHeight(height() - borderWidth * 2);

  80.         Qt::Alignment alignment = Qt::AlignCenter;
  81.         if (superTextAlign == TextAlign_Top_Left) {
  82.             alignment = Qt::AlignTop | Qt::AlignLeft;
  83.         } else if (superTextAlign == TextAlign_Top_Center) {
  84.             alignment = Qt::AlignTop | Qt::AlignHCenter;
  85.         } else if (superTextAlign == TextAlign_Top_Right) {
  86.             alignment = Qt::AlignTop | Qt::AlignRight;
  87.         } else if (superTextAlign == TextAlign_Center_Left) {
  88.             alignment = Qt::AlignLeft | Qt::AlignVCenter;
  89.         } else if (superTextAlign == TextAlign_Center_Center) {
  90.             alignment = Qt::AlignHCenter | Qt::AlignVCenter;
  91.         } else if (superTextAlign == TextAlign_Center_Right) {
  92.             alignment = Qt::AlignRight | Qt::AlignVCenter;
  93.         } else if (superTextAlign == TextAlign_Bottom_Left) {
  94.             alignment = Qt::AlignBottom | Qt::AlignLeft;
  95.         } else if (superTextAlign == TextAlign_Bottom_Center) {
  96.             alignment = Qt::AlignBottom | Qt::AlignHCenter;
  97.         } else if (superTextAlign == TextAlign_Bottom_Right) {
  98.             alignment = Qt::AlignBottom | Qt::AlignRight;
  99.         }

  100.         //绘制角标
  101.         painter->setPen(superTextColor);
  102.         painter->setFont(superTextFont);
  103.         painter->drawText(rect, alignment, superText);
  104.     }

  105.     int offset = 5;
  106.     QRect rect;
  107.     rect.setX(borderWidth * offset);
  108.     rect.setY(borderWidth);
  109.     rect.setWidth(width() - borderWidth * offset * 2);
  110.     rect.setHeight(height() - borderWidth * 2);

  111.     Qt::Alignment alignment = Qt::AlignCenter;
  112.     if (textAlign == TextAlign_Top_Left) {
  113.         alignment = Qt::AlignTop | Qt::AlignLeft;
  114.     } else if (textAlign == TextAlign_Top_Center) {
  115.         alignment = Qt::AlignTop | Qt::AlignHCenter;
  116.     } else if (textAlign == TextAlign_Top_Right) {
  117.         alignment = Qt::AlignTop | Qt::AlignRight;
  118.     } else if (textAlign == TextAlign_Center_Left) {
  119.         alignment = Qt::AlignLeft | Qt::AlignVCenter;
  120.     } else if (textAlign == TextAlign_Center_Center) {
  121.         alignment = Qt::AlignHCenter | Qt::AlignVCenter;
  122.     } else if (textAlign == TextAlign_Center_Right) {
  123.         alignment = Qt::AlignRight | Qt::AlignVCenter;
  124.     } else if (textAlign == TextAlign_Bottom_Left) {
  125.         alignment = Qt::AlignBottom | Qt::AlignLeft;
  126.     } else if (textAlign == TextAlign_Bottom_Center) {
  127.         alignment = Qt::AlignBottom | Qt::AlignHCenter;
  128.     } else if (textAlign == TextAlign_Bottom_Right) {
  129.         alignment = Qt::AlignBottom | Qt::AlignRight;
  130.     }

  131.     painter->setPen(textColor);
  132.     painter->setFont(textFont);
  133.     painter->drawText(rect, alignment, text);

  134.     painter->restore();
  135. }
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了