Qt放Element网页滑动菜单栏

2024-09-06 06:12

本文主要是介绍Qt放Element网页滑动菜单栏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

基于QTabWidget实现菜单

tabwidget.h

#ifndef TAB_WIDGET_H
#define TAB_WIDGET_H#include <QTabWidget>
#include <QVariantAnimation>
#include "customcomponent_global.h"class TabBarAnimation;class TabWidget : public QTabWidget
{Q_OBJECTpublic:InoTabWidget(QWidget *parent = 0);~InoTabWidget();void setAnimationCurrentValue(int value);protected:void paintEvent(QPaintEvent *);bool eventFilter(QObject *o, QEvent *e);private:void startAnimation(int beginX, int endX, int duration);private:TabBarAnimation *m_animation;int m_animationX;
};#endif

tabwidget.cpp

#include "tabwidget.h"
#include <QStyleOptionTabWidgetFrame>
#include <QStylePainter>
#include <QMouseEvent>const int AnimateBarWidth = 64;
const int AnimateBarHeight = 2;
const int AnimateBarXOffset = 30;const int LogoWidth = 140;
const int LogoHeight = 20;class TabBarAnimation : public QVariantAnimation
{
public:TabBarAnimation(InoTabWidget *t) :tabs(t){setEasingCurve(QEasingCurve::InOutQuad);}void updateCurrentValue(const QVariant &current) Q_DECL_OVERRIDE;private:InoTabWidget *tabs;
};void TabBarAnimation::updateCurrentValue(const QVariant &current)
{if (tabs) {tabs->setAnimationCurrentValue(current.toInt());}
}TabWidget::TabWidget(QWidget *parent) :QTabWidget(parent),m_animation(nullptr),m_animationX(-1)
{tabBar()->installEventFilter(this);tabBar()->setFixedHeight(40);
}TabWidget::~TabWidget()
{if (m_animation) {delete m_animation;m_animation = nullptr;}
}bool TabWidget::eventFilter(QObject *obj, QEvent *event)
{if (obj == tabBar() && event->type() == QEvent::MouseButtonPress) {QMouseEvent *pMouseEvent = (QMouseEvent *)event;if (pMouseEvent->button() == Qt::LeftButton) {const QPoint pos = pMouseEvent->pos();int index = tabBar()->tabAt(pos);if (index >= 0) {int curIndex = tabBar()->currentIndex();if (index != curIndex) {const QPoint tabBarPos = tabBar()->mapToGlobal(tabBar()->rect().topLeft());startAnimation(tabBarPos.x() + tabBar()->tabRect(curIndex).x() + tabBar()->tabRect(curIndex).width() / 2 - AnimateBarXOffset,tabBarPos.x() + tabBar()->tabRect(index).x() + tabBar()->tabRect(index).width() / 2 - AnimateBarXOffset, 250);}}}}return false;
}void TabWidget::startAnimation(int beginX, int endX, int duration)
{if (!m_animation) {m_animation = new TabBarAnimation(this);}m_animation->setStartValue(beginX);m_animation->setEndValue(endX);m_animation->setDuration(duration);m_animation->start();
}void TabWidget::setAnimationCurrentValue(int value)
{m_animationX = value;update();
}// 绘制背景下划线和当前Index的下划线
void TabWidget::paintEvent(QPaintEvent *event)
{Q_UNUSED(event);int index = tabBar()->currentIndex();QRect rect = tabBar()->tabRect(index);const QPoint tabBarPos = tabBar()->mapToGlobal(tabBar()->rect().topLeft());QStyleOptionTabWidgetFrame option;initStyleOption(&option);option.lineWidth = 0;QStylePainter p(this);option.rect = style()->subElementRect(QStyle::SE_TabWidgetTabPane, &option, this);p.drawPrimitive(QStyle::PE_FrameTabWidget, option);p.fillRect(QRect(option.rect.x(), rect.y(), option.rect.width(),rect.y() + rect.height() + AnimateBarHeight * 2),QColor(41, 90, 176));int x = (m_animation && m_animation->state() == QAbstractAnimation::Running) ?m_animationX :(tabBarPos.x() + rect.x() + rect.width() / 2 - AnimateBarXOffset);p.fillRect(QRect(x, rect.y() + rect.height(),AnimateBarWidth, AnimateBarHeight),QColor(255, 255, 255));
}

这篇关于Qt放Element网页滑动菜单栏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1141224

相关文章

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

Qt中实现多线程导出数据功能的四种方式小结

《Qt中实现多线程导出数据功能的四种方式小结》在以往的项目开发中,在很多地方用到了多线程,本文将记录下在Qt开发中用到的多线程技术实现方法,以导出指定范围的数字到txt文件为例,展示多线程不同的实现方... 目录前言导出文件的示例工具类QThreadQObject的moveToThread方法实现多线程QC

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

Qt QCustomPlot库简介(最新推荐)

《QtQCustomPlot库简介(最新推荐)》QCustomPlot是一款基于Qt的高性能C++绘图库,专为二维数据可视化设计,它具有轻量级、实时处理百万级数据和多图层支持等特点,适用于科学计算、... 目录核心特性概览核心组件解析1.绘图核心 (QCustomPlot类)2.数据容器 (QCPDataC

Qt如何实现文本编辑器光标高亮技术

《Qt如何实现文本编辑器光标高亮技术》这篇文章主要为大家详细介绍了Qt如何实现文本编辑器光标高亮技术,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录实现代码函数作用概述代码详解 + 注释使用 QTextEdit 的高亮技术(重点)总结用到的关键技术点应用场景举例示例优化建议

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

使用Python实现网页表格转换为markdown

《使用Python实现网页表格转换为markdown》在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,本文将使用Python编写一个网页表格转Markdown工具,需... 在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,以便在文档、邮件或

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言