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

Qt编写导航按钮

0
回复
5309
查看
[复制链接]
累计签到:7 天
连续签到:1 天
来源: 2017-12-24 12:07:52 显示全部楼层 |阅读模式
做各种各样的界面的时候,经常需要做一排按钮用于切换到对应界面,俗称导航按钮或者导航菜单,参照过各种各样的主界面导航布局,特意编写导航按钮自定义控件,结合各种情况,继承自QPushButton。已集成在QUC自定义控件中。
  1. /**
  2. * 导航按钮控件 作者:feiyangqingyun(QQ:517216493) 2017-12-19
  3. * 1:可设置文字的左侧+右侧+顶部+底部间隔
  4. * 2:可设置文字对齐方式
  5. * 3:可设置显示倒三角/倒三角边长/倒三角位置/倒三角颜色
  6. * 4:可设置显示图标/图标间隔/图标尺寸/正常状态图标/悬停状态图标/选中状态图标
  7. * 5:可设置显示边框线条/线条宽度/线条间隔/线条位置/线条颜色
  8. * 6:可设置正常背景颜色/悬停背景颜色/选中背景颜色
  9. * 7:可设置正常文字颜色/悬停文字颜色/选中文字颜色
  10. * 8:可设置背景颜色为画刷颜色
  11. */
复制代码



本人有代码洁癖症,写代码处处讲究对称完美。如下图所示。


主要代码如下:
  1. void NavButton::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.     drawIcon(&painter);
  12.     //绘制边框线条
  13.     drawLine(&painter);
  14.     //绘制右侧倒三角
  15.     drawTriangle(&painter);
  16. }

  17. void NavButton::drawBg(QPainter *painter)
  18. {
  19.     painter->save();
  20.     painter->setPen(Qt::NoPen);

  21.     int width = this->width();
  22.     int height = this->height();

  23.     QRect bgRect;
  24.     if (linePosition == LinePosition_Left) {
  25.         bgRect = QRect(lineSpace, 0, width - lineSpace, height);
  26.     } else if (linePosition == LinePosition_Right) {
  27.         bgRect = QRect(0, 0, width - lineSpace, height);
  28.     } else if (linePosition == LinePosition_Top) {
  29.         bgRect = QRect(0, lineSpace, width, height - lineSpace);
  30.     } else if (linePosition == LinePosition_Bottom) {
  31.         bgRect = QRect(0, 0, width, height - lineSpace);
  32.     }

  33.     //如果画刷存在则取画刷
  34.     QBrush bgBrush;
  35.     if (isChecked()) {
  36.         bgBrush = checkBgBrush;
  37.     } else if (hover) {
  38.         bgBrush = hoverBgBrush;
  39.     } else {
  40.         bgBrush = normalBgBrush;
  41.     }

  42.     if (bgBrush != Qt::NoBrush) {
  43.         painter->setBrush(bgBrush);
  44.     } else {
  45.         //根据当前状态选择对应颜色
  46.         QColor bgColor;
  47.         if (isChecked()) {
  48.             bgColor = checkBgColor;
  49.         } else if (hover) {
  50.             bgColor = hoverBgColor;
  51.         } else {
  52.             bgColor = normalBgColor;
  53.         }

  54.         painter->setBrush(bgColor);
  55.     }

  56.     painter->drawRect(bgRect);

  57.     painter->restore();
  58. }

  59. void NavButton::drawText(QPainter *painter)
  60. {
  61.     painter->save();
  62.     painter->setBrush(Qt::NoBrush);

  63.     //根据当前状态选择对应颜色
  64.     QColor textColor;
  65.     if (isChecked()) {
  66.         textColor = checkTextColor;
  67.     } else if (hover) {
  68.         textColor = hoverTextColor;
  69.     } else {
  70.         textColor = normalTextColor;
  71.     }

  72.     QRect textRect = QRect(paddingLeft, paddingTop, width() - paddingLeft - paddingRight, height() - paddingTop - paddingBottom);
  73.     painter->setPen(textColor);
  74.     painter->drawText(textRect, textAlign | Qt::AlignVCenter, text());

  75.     painter->restore();
  76. }

  77. void NavButton::drawIcon(QPainter *painter)
  78. {
  79.     if (!showIcon) {
  80.         return;
  81.     }

  82.     painter->save();

  83.     QPixmap pix;
  84.     if (isChecked()) {
  85.         pix = iconCheck;
  86.     } else if (hover) {
  87.         pix = iconHover;
  88.     } else {
  89.         pix = iconNormal;
  90.     }

  91.     if (!pix.isNull()) {
  92.         //等比例平滑缩放图标
  93.         pix = pix.scaled(iconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  94.         painter->drawPixmap(iconSpace, (height() - iconSize.height()) / 2, pix);
  95.     }

  96.     painter->restore();
  97. }

  98. void NavButton::drawLine(QPainter *painter)
  99. {
  100.     if (!showLine) {
  101.         return;
  102.     }

  103.     if (!isChecked()) {
  104.         return;
  105.     }

  106.     painter->save();

  107.     QPen pen;
  108.     pen.setWidth(lineWidth);
  109.     pen.setColor(lineColor);
  110.     painter->setPen(pen);

  111.     //根据线条位置设置线条坐标
  112.     QPoint pointStart, pointEnd;
  113.     if (linePosition == LinePosition_Left) {
  114.         pointStart = QPoint(0, 0);
  115.         pointEnd = QPoint(0, height());
  116.     } else if (linePosition == LinePosition_Right) {
  117.         pointStart = QPoint(width(), 0);
  118.         pointEnd = QPoint(width(), height());
  119.     } else if (linePosition == LinePosition_Top) {
  120.         pointStart = QPoint(0, 0);
  121.         pointEnd = QPoint(width(), 0);
  122.     } else if (linePosition == LinePosition_Bottom) {
  123.         pointStart = QPoint(0, height());
  124.         pointEnd = QPoint(width(), height());
  125.     }

  126.     painter->drawLine(pointStart, pointEnd);

  127.     painter->restore();
  128. }

  129. void NavButton::drawTriangle(QPainter *painter)
  130. {
  131.     if (!showTriangle) {
  132.         return;
  133.     }

  134.     //选中或者悬停显示
  135.     if (!hover && !isChecked()) {
  136.         return;
  137.     }

  138.     painter->save();
  139.     painter->setPen(Qt::NoPen);
  140.     painter->setBrush(triangleColor);

  141.     //绘制在右侧中间,根据设定的倒三角的边长设定三个点位置
  142.     int width = this->width();
  143.     int height = this->height();
  144.     int midWidth = width / 2;
  145.     int midHeight = height / 2;

  146.     QPolygon pts;
  147.     if (trianglePosition == TrianglePosition_Left) {
  148.         pts.setPoints(3, triangleLen, midHeight, 0, midHeight - triangleLen, 0, midHeight + triangleLen);
  149.     } else if (trianglePosition == TrianglePosition_Right) {
  150.         pts.setPoints(3, width - triangleLen, midHeight, width, midHeight - triangleLen, width, midHeight + triangleLen);
  151.     } else if (trianglePosition == TrianglePosition_Top) {
  152.         pts.setPoints(3, midWidth, triangleLen, midWidth - triangleLen, 0, midWidth + triangleLen, 0);
  153.     } else if (trianglePosition == TrianglePosition_Bottom) {
  154.         pts.setPoints(3, midWidth, height - triangleLen, midWidth - triangleLen, height, midWidth + triangleLen, height);
  155.     }

  156.     painter->drawPolygon(pts);

  157.     painter->restore();
  158. }
复制代码


本帖子中包含更多资源

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

x
回复

使用道具 举报

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

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