查看完整版本: [-- Qt编写导航按钮 --]

QTCN开发网 -> Qt 作品展 -> Qt编写导航按钮 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

<<   1   2   3   4   5  >>  Pages: ( 5 total )

liudianwu 2017-12-24 11:49

Qt编写导航按钮

做各种各样的界面的时候,经常需要做一排按钮用于切换到对应界面,俗称导航按钮或者导航菜单,参照过各种各样的主界面导航布局,特意编写导航按钮自定义控件,结合各种情况,继承自QPushButton。已集成在QUC自定义控件中。
作品已在另外一篇帖子开源,请见http://www.qtcn.org/bbs/read-htm-tid-66098.html
  1. /**
    * 导航按钮控件 作者:feiyangqingyun(QQ:517216493) 2017-12-19
    * 1:可设置文字的左侧+右侧+顶部+底部间隔
    * 2:可设置文字对齐方式
    * 3:可设置显示倒三角/倒三角边长/倒三角位置/倒三角颜色
    * 4:可设置显示图标/图标间隔/图标尺寸/正常状态图标/悬停状态图标/选中状态图标
    * 5:可设置显示边框线条/线条宽度/线条间隔/线条位置/线条颜色
    * 6:可设置正常背景颜色/悬停背景颜色/选中背景颜色
    * 7:可设置正常文字颜色/悬停文字颜色/选中文字颜色
    * 8:可设置背景颜色为画刷颜色
    */


[attachment=19090]
[attachment=19369]

[attachment=18353]
本人有代码洁癖症,写代码处处讲究对称完美。如下图所示。
[attachment=18354]
[attachment=18355]
主要代码:
  1. 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();
    }




微笑内敛 2017-12-24 14:44
    

liuchangyin 2017-12-25 08:52

liuchangyin 2017-12-25 08:53
漂亮的界面

一只小鱼 2017-12-25 09:19
  

liudianwu 2017-12-25 09:41
一只小鱼:[表情]  [表情]  (2017-12-25 09:19) 

好评者留下邮箱,发本控件完整demo源码。

fariel 2017-12-25 10:17
   大师越来越给力

vitty1984 2017-12-25 11:03
vitty2005@163.com,好东西啊

huige3528 2017-12-25 11:04
刘大神依旧很威武,来份代码瞧瞧   193323681@qq.com

vitty1984 2017-12-25 11:04
好东西,vitty2005@163.com

wvjy001 2017-12-25 14:45

zoemolly 2017-12-25 15:14
多年QT老菜鸟前来学习 solmars@126.com

uu1000 2017-12-25 15:31
,厉害,看看代码 uuvv1000@sohu.com

llwj0303 2017-12-25 16:33
479379320@qq.com

街舞show 2017-12-25 16:57
又是一波好控件啊。990882252@qq.com

第二航空队 2017-12-25 20:02
刘大师出品,必属精品。547650721@qq.com。谢谢了。

hlr159hlr 2017-12-26 08:52
刘大师出品,一直在学习。192842854@qq.com。谢谢了。

一只小鱼 2017-12-26 09:30
感谢了 929077213@qq.com

liudianwu 2017-12-26 13:37
vitty2005@163.com
193323681@qq.com
solmars@126.com
uuvv1000@sohu.com
479379320@qq.com
990882252@qq.com
547650721@qq.com
192842854@qq.com
929077213@qq.com
已发以上邮箱,还望记得给帖子评分!举手之劳!谢谢!
[attachment=18359]

good_123 2017-12-26 14:47
界面不错,3238363825@qq.com

renconan 2017-12-26 15:21
大师出品,必属精品.谢谢大师819237590@qq.com

xdh873939316 2017-12-26 19:45
感谢刘大师分享,,给5星好评。568470911@qq.com  刘大师出品,必属精品。。

rohgeo 2017-12-27 07:57
刘老师出品,必属精品,5星好评。redgex@163.com

gonxa 2017-12-27 09:32
精品必须支持!gonxa@163.com

lipanlin 2017-12-27 23:26
精品!!!!!  lipanlin@126.com。谢谢了。

wyy626562203 2017-12-28 08:35
感谢大神 1368299513@qq.com

thematic971 2017-12-28 09:05
支持精品好帖 跟您学了不少知识 83945185@qq.com

奔跑的程序员 2017-12-28 17:42
支持刘大师,很喜欢刘大师作品的风格
369495512@qq.com

zqxwce007 2017-12-29 08:38
优秀文章,支持!n神马都是浮云 zqxwce111@163.com

zengqingguo 2017-12-29 10:09
大师出品,必属精品.谢谢大师 155226440@qq.com

liudianwu 2017-12-29 10:31
155226440@qq.com
zqxwce111@163.com
369495512@qq.com
83945185@qq.com
1368299513@qq.com
gonxa@163.com
lipanlin@126.com
redgex@163.com
568470911@qq.com
819237590@qq.com
3238363825@qq.com
以上邮箱已发送请查收!

fariel 2017-12-29 16:02
前面忘了添加邮箱了,补个邮箱
fariel@126.com

lzh280 2017-12-29 17:13
优秀文章,支持!n神马都是浮云 。邮箱:247095354@qq.com

morg 2017-12-29 19:09
不错,谢谢   hz.qqmz@qq.com

qiji_2015 2017-12-29 22:46
刘大师威武!受教了!shiyuluo2010@126.com

crazy 2017-12-30 09:40
我顶

yyan_wwei8 2017-12-30 18:01
yyan_wwei8@126.com

chenjinb2008 2017-12-31 17:05
支持刘大师,很喜欢刘大师作品。
chenjinb2008@163.com

yzq189424658 2018-01-01 16:10
感谢分享, 向前辈学习.  yzq189424658@163.com 恳请发一份demo,感谢ing.

zhucc3 2018-01-01 17:03
感谢分享,向你学习,zhucc3@126.com,谢谢!

liudianwu 2018-01-02 08:33
fariel@126.com
247095354@qq.com
hz.qqmz@qq.com
shiyuluo2010@126.com
yyan_wwei8@126.com
chenjinb2008@163.com
yzq189424658@163.com
zhucc3@126.com
已发送请查收!

dogjin1 2018-01-02 10:58
向刘大师学习,425195305@qq.com 谢谢

花葬尘土 2018-01-02 11:20
QT菜鸟,向刘大师学习学习。1358214176@qq.com

doublelight 2018-01-02 19:37
304369331@qq.com

t1029901995 2018-01-02 22:15
刘大师 怎么才i算好评呢,这个算不算??   1144727618@qq.com

lsyzsl 2018-01-03 09:06
看你的代码犹如看一篇美文,一直在向你学习,写出诗一样的代码。 1024847801@qq.com

capdi 2018-01-03 09:45
         tangdrong@126.com

13419543466 2018-01-03 09:50
大师,请发邮箱13419543466@163.com

w642833823 2018-01-03 13:31
感谢楼主这些年做出的贡献,向作者学习,2873878546@qq.com

hc976865965 2018-01-03 15:39
刘大师好作品啊,崇拜,写得漂亮。。。3360501749@qq.com


查看完整版本: [-- Qt编写导航按钮 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled