-
UID:110085
-
- 注册时间2010-12-21
- 最后登录2024-04-19
- 在线时间3537小时
-
- 发帖2740
- 搜Ta的帖子
- 精华2
- 金钱32703
- 威望3368
- 贡献值588
- 好评度3386
-
访问TA的空间加好友用道具
|
前言导航 按钮控件,主要用于各种漂亮精美的导航条,我们经常在web中看到导航条都非常精美,都是html+css+js实现的,还自带动画过度效果,Qt提供的qss其实也是无敌的,支持基本上所有的CSS2属性,配合QPainter这个无敌大法工具, 没有什么不能绘制的。这个控件总结了大部分的导航条样式,比如左侧+右侧+顶部+底部,线条指示器,倒三角指示器等。还可以在导航条前面加上图标等,就显得更加有特色。有了此控件,再也不用担心没有精美的导航了。 实现的功能 * 1:可设置文字的左侧+右侧+顶部+底部间隔 * 2:可设置文字对齐方式 * 3:可设置 显示倒三角/倒三角边长/倒三角位置/倒三角颜色 * 4:可设置显示图标/图标间隔/图标尺寸/正常状态图标/悬停状态图标/选中状态图标 * 5:可设置显示边框线条/线条宽度/线条间隔/线条位置/线条颜色 * 6:可设置正常背景颜色/悬停背景颜色/选中背景颜色 * 7:可设置正常文字颜色/悬停文字颜色/选中文字颜色 * 8:可设置背景颜色为画刷颜色 效果图头文件代码- #ifndef NAVBUTTON_H
- #define NAVBUTTON_H
- /**
- * 导航按钮控件 作者:feiyangqingyun(QQ:517216493) 2017-12-19
- * 1:可设置文字的左侧+右侧+顶部+底部间隔
- * 2:可设置文字对齐方式
- * 3:可设置显示倒三角/倒三角边长/倒三角位置/倒三角颜色
- * 4:可设置显示图标/图标间隔/图标尺寸/正常状态图标/悬停状态图标/选中状态图标
- * 5:可设置显示边框线条/线条宽度/线条间隔/线条位置/线条颜色
- * 6:可设置正常背景颜色/悬停背景颜色/选中背景颜色
- * 7:可设置正常文字颜色/悬停文字颜色/选中文字颜色
- * 8:可设置背景颜色为画刷颜色
- */
- #include <QPushButton>
- #ifdef quc
- #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
- #include <QtDesigner/QDesignerExportWidget>
- #else
- #include <QtUiPlugin/QDesignerExportWidget>
- #endif
- class QDESIGNER_WIDGET_EXPORT NavButton : public QPushButton
- #else
- class NavButton : public QPushButton
- #endif
- {
- Q_OBJECT
- Q_ENUMS(TextAlign)
- Q_ENUMS(TrianglePosition)
- Q_ENUMS(LinePosition)
- Q_ENUMS(IconPosition)
- Q_PROPERTY(int paddingLeft READ getPaddingLeft WRITE setPaddingLeft)
- Q_PROPERTY(int paddingRight READ getPaddingRight WRITE setPaddingRight)
- Q_PROPERTY(int paddingTop READ getPaddingTop WRITE setPaddingTop)
- Q_PROPERTY(int paddingBottom READ getPaddingBottom WRITE setPaddingBottom)
- Q_PROPERTY(TextAlign textAlign READ getTextAlign WRITE setTextAlign)
- Q_PROPERTY(bool showTriangle READ getShowTriangle WRITE setShowTriangle)
- Q_PROPERTY(int triangleLen READ getTriangleLen WRITE setTriangleLen)
- Q_PROPERTY(TrianglePosition trianglePosition READ getTrianglePosition WRITE setTrianglePosition)
- Q_PROPERTY(QColor triangleColor READ getTriangleColor WRITE setTriangleColor)
- Q_PROPERTY(bool showIcon READ getShowIcon WRITE setShowIcon)
- Q_PROPERTY(int iconSpace READ getIconSpace WRITE setIconSpace)
- Q_PROPERTY(QSize iconSize READ getIconSize WRITE setIconSize)
- Q_PROPERTY(QPixmap iconNormal READ getIconNormal WRITE setIconNormal)
- Q_PROPERTY(QPixmap iconHover READ getIconHover WRITE setIconHover)
- Q_PROPERTY(QPixmap iconCheck READ getIconCheck WRITE setIconCheck)
- Q_PROPERTY(bool showLine READ getShowLine WRITE setShowLine)
- Q_PROPERTY(int lineSpace READ getLineSpace WRITE setLineSpace)
- Q_PROPERTY(int lineWidth READ getLineWidth WRITE setLineWidth)
- Q_PROPERTY(LinePosition linePosition READ getLinePosition WRITE setLinePosition)
- Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor)
- Q_PROPERTY(QColor normalBgColor READ getNormalBgColor WRITE setNormalBgColor)
- Q_PROPERTY(QColor hoverBgColor READ getHoverBgColor WRITE setHoverBgColor)
- Q_PROPERTY(QColor checkBgColor READ getCheckBgColor WRITE setCheckBgColor)
- Q_PROPERTY(QColor normalTextColor READ getNormalTextColor WRITE setNormalTextColor)
- Q_PROPERTY(QColor hoverTextColor READ getHoverTextColor WRITE setHoverTextColor)
- Q_PROPERTY(QColor checkTextColor READ getCheckTextColor WRITE setCheckTextColor)
- public:
- enum TextAlign {
- TextAlign_Left = 0x0001, //左侧对齐
- TextAlign_Right = 0x0002, //右侧对齐
- TextAlign_Top = 0x0020, //顶部对齐
- TextAlign_Bottom = 0x0040, //底部对齐
- TextAlign_Center = 0x0004 //居中对齐
- };
- enum TrianglePosition {
- TrianglePosition_Left = 0, //左侧
- TrianglePosition_Right = 1, //右侧
- TrianglePosition_Top = 2, //顶部
- TrianglePosition_Bottom = 3 //底部
- };
- enum IconPosition {
- IconPosition_Left = 0, //左侧
- IconPosition_Right = 1, //右侧
- IconPosition_Top = 2, //顶部
- IconPosition_Bottom = 3 //底部
- };
- enum LinePosition {
- LinePosition_Left = 0, //左侧
- LinePosition_Right = 1, //右侧
- LinePosition_Top = 2, //顶部
- LinePosition_Bottom = 3 //底部
- };
- explicit NavButton(QWidget *parent = 0);
- protected:
- void enterEvent(QEvent *);
- void leaveEvent(QEvent *);
- void paintEvent(QPaintEvent *);
- void drawBg(QPainter *painter);
- void drawText(QPainter *painter);
- void drawIcon(QPainter *painter);
- void drawLine(QPainter *painter);
- void drawTriangle(QPainter *painter);
- private:
- int paddingLeft; //文字左侧间隔
- int paddingRight; //文字右侧间隔
- int paddingTop; //文字顶部间隔
- int paddingBottom; //文字底部间隔
- TextAlign textAlign; //文字对齐
- bool showTriangle; //显示倒三角
- int triangleLen; //倒三角边长
- TrianglePosition trianglePosition; //倒三角位置
- QColor triangleColor; //倒三角颜色
- bool showIcon; //显示图标
- int iconSpace; //图标间隔
- QSize iconSize; //图标尺寸
- QPixmap iconNormal; //正常图标
- QPixmap iconHover; //悬停图标
- QPixmap iconCheck; //选中图标
- bool showLine; //显示线条
- int lineSpace; //线条间隔
- int lineWidth; //线条宽度
- LinePosition linePosition; //线条位置
- QColor lineColor; //线条颜色
- QColor normalBgColor; //正常背景颜色
- QColor hoverBgColor; //悬停背景颜色
- QColor checkBgColor; //选中背景颜色
- QColor normalTextColor; //正常文字颜色
- QColor hoverTextColor; //悬停文字颜色
- QColor checkTextColor; //选中文字颜色
- QBrush normalBgBrush; //正常背景画刷
- QBrush hoverBgBrush; //悬停背景画刷
- QBrush checkBgBrush; //选中背景画刷
- bool hover; //悬停标志位
- public:
- int getPaddingLeft() const;
- int getPaddingRight() const;
- int getPaddingTop() const;
- int getPaddingBottom() const;
- TextAlign getTextAlign() const;
- bool getShowTriangle() const;
- int getTriangleLen() const;
- TrianglePosition getTrianglePosition()const;
- QColor getTriangleColor() const;
- bool getShowIcon() const;
- int getIconSpace() const;
- QSize getIconSize() const;
- QPixmap getIconNormal() const;
- QPixmap getIconHover() const;
- QPixmap getIconCheck() const;
- bool getShowLine() const;
- int getLineSpace() const;
- int getLineWidth() const;
- LinePosition getLinePosition() const;
- QColor getLineColor() const;
- QColor getNormalBgColor() const;
- QColor getHoverBgColor() const;
- QColor getCheckBgColor() const;
- QColor getNormalTextColor() const;
- QColor getHoverTextColor() const;
- QColor getCheckTextColor() const;
- QSize sizeHint() const;
- QSize minimumSizeHint() const;
- public Q_SLOTS:
- //设置文字间隔
- void setPaddingLeft(int paddingLeft);
- void setPaddingRight(int paddingRight);
- void setPaddingTop(int paddingTop);
- void setPaddingBottom(int paddingBottom);
- void setPadding(int padding);
- void setPadding(int paddingLeft, int paddingRight, int paddingTop, int paddingBottom);
- //设置文字对齐
- void setTextAlign(const TextAlign &textAlign);
- //设置显示倒三角
- void setShowTriangle(bool showTriangle);
- //设置倒三角边长
- void setTriangleLen(int triangleLen);
- //设置倒三角位置
- void setTrianglePosition(const TrianglePosition &trianglePosition);
- //设置倒三角颜色
- void setTriangleColor(const QColor &triangleColor);
- //设置显示图标
- void setShowIcon(bool showIcon);
- //设置图标间隔
- void setIconSpace(int iconSpace);
- //设置图标尺寸
- void setIconSize(const QSize &iconSize);
- //设置正常图标
- void setIconNormal(const QPixmap &iconNormal);
- //设置悬停图标
- void setIconHover(const QPixmap &iconHover);
- //设置按下图标
- void setIconCheck(const QPixmap &iconCheck);
- //设置显示线条
- void setShowLine(bool showLine);
- //设置线条间隔
- void setLineSpace(int lineSpace);
- //设置线条宽度
- void setLineWidth(int lineWidth);
- //设置线条位置
- void setLinePosition(const LinePosition &linePosition);
- //设置线条颜色
- void setLineColor(const QColor &lineColor);
- //设置正常背景颜色
- void setNormalBgColor(const QColor &normalBgColor);
- //设置悬停背景颜色
- void setHoverBgColor(const QColor &hoverBgColor);
- //设置选中背景颜色
- void setCheckBgColor(const QColor &checkBgColor);
- //设置正常文字颜色
- void setNormalTextColor(const QColor &normalTextColor);
- //设置悬停文字颜色
- void setHoverTextColor(const QColor &hoverTextColor);
- //设置选中文字颜色
- void setCheckTextColor(const QColor &checkTextColor);
- //设置正常背景画刷
- void setNormalBgBrush(const QBrush &normalBgBrush);
- //设置悬停背景画刷
- void setHoverBgBrush(const QBrush &hoverBgBrush);
- //设置选中背景画刷
- void setCheckBgBrush(const QBrush &checkBgBrush);
- };
- #endif // NAVBUTTON_H
核心代码- void NavButton::paintEvent(QPaintEvent *)
- {
- //绘制准备工作,启用反锯齿
- QPainter painter(this);
- painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
- //绘制背景
- drawBg(&painter);
- //绘制文字
- drawText(&painter);
- //绘制图标
- drawIcon(&painter);
- //绘制边框线条
- drawLine(&painter);
- //绘制倒三角
- drawTriangle(&painter);
- }
- void NavButton::drawBg(QPainter *painter)
- {
- painter->save();
- painter->setPen(Qt::NoPen);
- int width = this->width();
- int height = this->height();
- QRect bgRect;
- if (linePosition == LinePosition_Left) {
- bgRect = QRect(lineSpace, 0, width - lineSpace, height);
- } else if (linePosition == LinePosition_Right) {
- bgRect = QRect(0, 0, width - lineSpace, height);
- } else if (linePosition == LinePosition_Top) {
- bgRect = QRect(0, lineSpace, width, height - lineSpace);
- } else if (linePosition == LinePosition_Bottom) {
- bgRect = QRect(0, 0, width, height - lineSpace);
- }
- //如果画刷存在则取画刷
- QBrush bgBrush;
- if (isChecked()) {
- bgBrush = checkBgBrush;
- } else if (hover) {
- bgBrush = hoverBgBrush;
- } else {
- bgBrush = normalBgBrush;
- }
- if (bgBrush != Qt::NoBrush) {
- painter->setBrush(bgBrush);
- } else {
- //根据当前状态选择对应颜色
- QColor bgColor;
- if (isChecked()) {
- bgColor = checkBgColor;
- } else if (hover) {
- bgColor = hoverBgColor;
- } else {
- bgColor = normalBgColor;
- }
- painter->setBrush(bgColor);
- }
- painter->drawRect(bgRect);
- painter->restore();
- }
- void NavButton::drawText(QPainter *painter)
- {
- painter->save();
- painter->setBrush(Qt::NoBrush);
- //根据当前状态选择对应颜色
- QColor textColor;
- if (isChecked()) {
- textColor = checkTextColor;
- } else if (hover) {
- textColor = hoverTextColor;
- } else {
- textColor = normalTextColor;
- }
- QRect textRect = QRect(paddingLeft, paddingTop, width() - paddingLeft - paddingRight, height() - paddingTop - paddingBottom);
- painter->setPen(textColor);
- painter->drawText(textRect, textAlign | Qt::AlignVCenter, text());
- painter->restore();
- }
- void NavButton::drawIcon(QPainter *painter)
- {
- if (!showIcon) {
- return;
- }
- painter->save();
- QPixmap pix;
- if (isChecked()) {
- pix = iconCheck;
- } else if (hover) {
- pix = iconHover;
- } else {
- pix = iconNormal;
- }
- if (!pix.isNull()) {
- //等比例平滑缩放图标
- pix = pix.scaled(iconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
- painter->drawPixmap(iconSpace, (height() - iconSize.height()) / 2, pix);
- }
- painter->restore();
- }
- void NavButton::drawLine(QPainter *painter)
- {
- if (!showLine) {
- return;
- }
- if (!isChecked()) {
- return;
- }
- painter->save();
- QPen pen;
- pen.setWidth(lineWidth);
- pen.setColor(lineColor);
- painter->setPen(pen);
- //根据线条位置设置线条坐标
- QPoint pointStart, pointEnd;
- if (linePosition == LinePosition_Left) {
- pointStart = QPoint(0, 0);
- pointEnd = QPoint(0, height());
- } else if (linePosition == LinePosition_Right) {
- pointStart = QPoint(width(), 0);
- pointEnd = QPoint(width(), height());
- } else if (linePosition == LinePosition_Top) {
- pointStart = QPoint(0, 0);
- pointEnd = QPoint(width(), 0);
- } else if (linePosition == LinePosition_Bottom) {
- pointStart = QPoint(0, height());
- pointEnd = QPoint(width(), height());
- }
- painter->drawLine(pointStart, pointEnd);
- painter->restore();
- }
- void NavButton::drawTriangle(QPainter *painter)
- {
- if (!showTriangle) {
- return;
- }
- //选中或者悬停显示
- if (!hover && !isChecked()) {
- return;
- }
- painter->save();
- painter->setPen(Qt::NoPen);
- painter->setBrush(triangleColor);
- //绘制在右侧中间,根据设定的倒三角的边长设定三个点位置
- int width = this->width();
- int height = this->height();
- int midWidth = width / 2;
- int midHeight = height / 2;
- QPolygon pts;
- if (trianglePosition == TrianglePosition_Left) {
- pts.setPoints(3, triangleLen, midHeight, 0, midHeight - triangleLen, 0, midHeight + triangleLen);
- } else if (trianglePosition == TrianglePosition_Right) {
- pts.setPoints(3, width - triangleLen, midHeight, width, midHeight - triangleLen, width, midHeight + triangleLen);
- } else if (trianglePosition == TrianglePosition_Top) {
- pts.setPoints(3, midWidth, triangleLen, midWidth - triangleLen, 0, midWidth + triangleLen, 0);
- } else if (trianglePosition == TrianglePosition_Bottom) {
- pts.setPoints(3, midWidth, height - triangleLen, midWidth - triangleLen, height, midWidth + triangleLen, height);
- }
- painter->drawPolygon(pts);
- painter->restore();
- }
控件介绍 1. 超过140个精美控件,涵盖了各种仪表盘、进度条、进度球、指南针、曲线图、标尺、温度计、导航条、导航栏,flatui、高亮按钮、滑动选择器、农历等。远超qwt集成的控件数量。 2. 每个类都可以独立成一个单独的控件,零耦合,每个控件一个头文件和一个实现文件,不依赖其他文件,方便单个控件以源码形式集成到项目中,较少代码量。qwt的控件类环环相扣,高度耦合,想要使用其中一个控件,必须包含所有的代码。 3. 全部纯Qt编写,QWidget+QPainter绘制,支持Qt4.6到 Qt5.12的任何Qt版本,支持mingw、msvc、gcc等 编译器,不乱码,可直接集成到Qt Creator中,和自带的控件一样使用,大部分效果只要设置几个属性即可,极为方便。 4. 每个控件都有一个对应的单独的包含该控件源码的DEMO,方便参考使用。同时还提供一个所有控件使用的集成的DEMO。 5. 每个控件的源代码都有详细中文注释,都按照统一设计规范编写,方便学习自定义控件的编写。 6. 每个控件默认配色和demo对应的配色都非常精美。 7. 超过120个可见控件,6个不可见控件。 8. 部分控件提供多种样式风格选择,多种指示器样式选择。 9. 所有控件自适应窗体拉伸变化。 10. 集成自定义控件属性设计器,支持拖曳设计,所见即所得,支持导入导出xml格式。 11. 自带activex控件demo,所有控件可以直接运行在ie浏览器中。 12. 集成fontawesome图形字体+阿里巴巴iconfont收藏的几百个图形字体,享受图形字体带来的乐趣。 13. 所有控件最后生成一个dll动态库文件,可以直接集成到qtcreator中拖曳设计使用。 SDK下载 - SDK下载链接: https://pan.baidu.com/s/1tD9v1YPfE2fgYoK6lqUr1Q 提取码:lyhk - 自定义控件+属性设计器欣赏: https://pan.baidu.com/s/1l6L3rKSiLu_uYi7lnL3ibQ 提取码:tmvl - 下载链接中包含了各个版本的动态库文件,所有控件的头文件,使用demo。 - 自定义控件 插件开放动态库dll使用(永久免费),无任何后门和限制,请放心使用。 - 目前已提供25个版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。 - 不定期增加控件和完善控件,不定期更新SDK,欢迎各位提出建议,谢谢! - widget版本(QQ:517216493) qml版本(QQ:373955953)三峰驼(QQ:278969898)。
|