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

相关文章

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.引言

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言