Qt滑块图片验证码

Qt滑块图片验证码

@[toc]

(一)、控件介绍

  1. 自定义随机图片
  2. 滑动条滑动验证
  3. 重定义验证图片
  4. 抠图位置随机

(二)、效果图

1.gif
2.gif
void PuzzleWidget::setPixmap(const QString& pixmap)
{
    m_pixmap = pixmap;
    QTimer::singleShot(10, this, SLOT(onUpdatePixmap()));
}

void PuzzleWidget::onUpdatePixmap()
{
    m_offsetPoint.rx() = qBound(0, qrand() % this->width() + squarewidth + squareradius, this->width() - squarewidth - squareradius);
    m_offsetPoint.ry() = qBound(0, qrand() % this->height() + squarewidth + squareradius, this->height() - squarewidth - squareradius);
    update();
}

void PuzzleWidget::setValue(int value)
{
    m_value = qBound(0, value, this->width() - squarewidth - squareradius + m_offsetPoint.x());
    update();
}

void PuzzleWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing);
    QPainterPath clippath;
    clippath.addRoundedRect(this->rect(), 4, 4);
    painter.setClipPath(clippath);
    const QPixmap& pixmap = QPixmap(m_pixmap).scaled(this->width(), this->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    painter.drawPixmap(0, 0, this->width(), this->height(), pixmap);

    QPainterPath cutoutpath;
    cutoutpath.setFillRule(Qt::WindingFill);
    QRect rect(m_offsetPoint, QSize(squarewidth, squarewidth));
    cutoutpath.addRoundedRect(rect, 2, 2);
    cutoutpath.addEllipse(rect.center().x() - squareradius / 2, rect.top() - squareradius + 6, squareradius, squareradius);
    QPainterPath subellipseparh;
    subellipseparh.addEllipse(rect.right() - squareradius + 6, rect.center().y() - squareradius / 2, squareradius, squareradius);
    cutoutpath -= subellipseparh;

    painter.setPen(QPen(QColor(80, 80, 80), 1));
    painter.setBrush(QColor(100, 100, 100, 220));
    painter.drawPath(cutoutpath);

    QPixmap puzzlePixmap(this->size());
    puzzlePixmap.fill(Qt::transparent);
    QPainter puzzlePainter(&puzzlePixmap);
    puzzlePainter.setRenderHints(QPainter::Antialiasing);
    puzzlePainter.setClipPath(cutoutpath);
    puzzlePainter.setPen(QPen(QColor(80, 80, 80), 2));
    puzzlePainter.setBrush(QColor(200, 200, 200, 100));
    puzzlePainter.drawPixmap(0, 0, this->width(), this->height(), pixmap);
    puzzlePainter.drawPath(cutoutpath);

    painter.drawPixmap(-m_offsetPoint.x() + m_value, 0, this->width(), this->height(), puzzlePixmap);
}

bool PuzzleWidget::isOverlap()
{
    return qAbs(-m_offsetPoint.x() + m_value) < 5;
}

工程文件

Qt交流大会(收费群) 853086607
QQ 3246214072


在这里插入图片描述

结尾

不定期上传新作品,解答群中作品相关问题。相关外,能解答则解答。欢迎大家一起探索Qt世界

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布转载请标明出处: http://www.jian...
    张旭童阅读 12,062评论 22 98
  • 作者简介 原创微信公众号郭霖 WeChat ID: guolin_blog 本篇来自 老老司机(第六篇了)张旭童的...
    木木00阅读 3,158评论 0 15
  • 使用Java生成图片滑动验证码 目前接到了一个新的小需求,要在登录时进行滑动图片验证。搜了一下网上的demo,没有...
    沧海千寻阅读 14,182评论 8 57
  • 我们的身体仿佛一件尘世的器皿 每夜的沉睡是我们的更新 伴随太阳的来临 我们的器皿每一天都是新的 从上帝而造 尘世之...
    彩虹雪阅读 277评论 5 7
  • 在困境里,我们唯一能做也是唯一值得做的就是相信,我们都是注定会得救的,无论是以什么方式还是用多长时间。
    猫腻儿和瘪犊子阅读 301评论 0 2