Qt浅谈之四十五QSplitter实现自由伸缩滑动窗口

2024-03-11 10:18

本文主要是介绍Qt浅谈之四十五QSplitter实现自由伸缩滑动窗口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、简介

 

        最近看到一篇Qt实现伸缩滑动的窗口的文章,但其代码不完整。便在此重新书写了完整的代码,并开源出来。窗口的中央有滑动条可以动态改变子窗口的大小,隐藏的按钮可以快速伸缩子窗口。其效果图如下:

 

二、详解

1、代码

(1)slidingwindow.h

#ifndef SLIDINGWINDOW_H
#define SLIDINGWINDOW_H#include <QtGui>class SlidingWindow : public QWidget
{Q_OBJECTpublic:SlidingWindow(QWidget *parent = 0);~SlidingWindow();protected:void mousePressEvent ( QMouseEvent * event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void resizeEvent(QResizeEvent *event);bool eventFilter(QObject *obj, QEvent *event);private:void setBtnPos();void setBtnIcon();private slots:void slotClickedBtn();void slotSplitterMoved(int pos, int index);private:QPoint dragPosition;bool bPressFlag;QSplitter *splitter;QFrame *contentFrame;QFrame *listFrame;QPushButton *pushButton;QIcon leftIcon;QIcon rightIcon;
};#endif // SLIDINGWINDOW_H

(2)slidingwindow.cpp

#include "slidingwindow.h"SlidingWindow::SlidingWindow(QWidget *parent): QWidget(parent, Qt::FramelessWindowHint), bPressFlag(false)
{resize(660, 460);splitter = new QSplitter(Qt::Horizontal, this);splitter->setHandleWidth(1);splitter->setStyleSheet("QSplitter::handle{background:#FFFFFF}");contentFrame = new QFrame(splitter);contentFrame->setStyleSheet("background:#000000");contentFrame->resize(475, height());contentFrame->setMinimumWidth(405);listFrame = new QFrame(splitter);listFrame->setStyleSheet("background:#323232");listFrame->resize(660 - 475, height());listFrame->setMaximumWidth(660 - 405);connect(splitter, SIGNAL(splitterMoved(int,int)), this, SLOT(slotSplitterMoved(int,int)));leftIcon = QIcon(":/left.png");rightIcon = QIcon(":/right.png");pushButton = new QPushButton(this);pushButton->setFocusPolicy(Qt::NoFocus);pushButton->hide();pushButton->setFixedSize(13, 42);pushButton->setIconSize(pushButton->size());pushButton->setStyleSheet("border:none;");pushButton->setIcon(rightIcon);pushButton->move(contentFrame->width() - pushButton->width()-2, (contentFrame->height() - pushButton->height())/2);connect(pushButton,SIGNAL(clicked()),this,SLOT(slotClickedBtn()));contentFrame->setMouseTracking(true);listFrame->setMouseTracking(true);contentFrame->installEventFilter(this);listFrame->installEventFilter(this);
}SlidingWindow::~SlidingWindow()
{}void SlidingWindow::resizeEvent(QResizeEvent *event)
{splitter->setGeometry(0, 0, width(), height());move((QApplication::desktop()->width() - width())/2,  (QApplication::desktop()->height() - height())/2);QWidget::resizeEvent(event);
}bool SlidingWindow::eventFilter(QObject *obj, QEvent *event)
{if (event->type() == QEvent::MouseMove) {QMouseEvent *mouseMove = static_cast<QMouseEvent*>(event);QRect rect = pushButton->frameGeometry();if (rect.contains(mouseMove->pos())) {pushButton->show();}else {pushButton->hide();}}return QWidget::eventFilter(obj, event);
}void SlidingWindow::setBtnPos()
{pushButton->move(contentFrame->width() - pushButton->width(), (contentFrame->height() - pushButton->height())/2);
}void SlidingWindow::setBtnIcon()
{if (listFrame->width() != 0) {pushButton->setIcon(rightIcon);}else {pushButton->setIcon(leftIcon);}
}void SlidingWindow::slotClickedBtn()
{QList <int> sizeList;sizeList.clear();if (listFrame->width() != 0) {sizeList.append(660);sizeList.append(0);}else {sizeList.append(475);sizeList.append(660 - 475);}splitter->setSizes(sizeList);setBtnIcon();setBtnPos();pushButton->hide();
}void SlidingWindow::slotSplitterMoved(int pos, int index)
{Q_UNUSED(pos)Q_UNUSED(index)setBtnIcon();setBtnPos();
}void SlidingWindow::mousePressEvent ( QMouseEvent * event)
{bPressFlag = true;dragPosition = event->pos();QWidget::mousePressEvent(event);
}void SlidingWindow::mouseMoveEvent(QMouseEvent *event)
{if (bPressFlag) {QPoint relaPos(QCursor::pos() - dragPosition);move(relaPos);}QWidget::mouseMoveEvent(event);
}void SlidingWindow::mouseReleaseEvent(QMouseEvent *event)
{bPressFlag = false;QWidget::mouseReleaseEvent(event);
}

(3)main.cpp

#include "slidingwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);SlidingWindow w;w.show();return a.exec();
}

(4)编译运行

三、总结

(1)获取位置坐标可以尝试QPoint realPos(QCursor::pos() - this->pos());,这样不用类型转换。主要设置setMouseTracking(true);才能当鼠标不点击时移动鼠标进入mouseMoveEvent事件函数。
(2)源码已上传到csdn:http://download.csdn.net/detail/taiyang1987912/9439233。
(3)若有问题或建议,请留言,在此感谢!

这篇关于Qt浅谈之四十五QSplitter实现自由伸缩滑动窗口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法