经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
31.qt quick-使用SwipeView添加滑动视图-高仿微信V2版本
来源:cnblogs  作者:诺谦  时间:2021/6/21 10:12:51  对本文有异议

在上章我们学习了ListView,然后实现了: 28.qt quick-ListView高仿微信好友列表和聊天列表,本章我们来学习SwipeView滑动视图,并出高仿微信V2版本:


 

1.Container介绍

由于SwipeView继承于Container,而Container是一个抽象的容器类.所以我们先来学习下Container
Container类的子类有:DialogButtonBox(对话框按钮框), MenuBar(菜单栏), SwipeView(滑动视图),TabBar、如下图所示:

Properties

  • contentChildren : list<Item>,保存容器中的item所有项,与contentData不同,contentChildren只包括可视化的QML对象。当插入或移动项目时,它将重新排序。
  • contentData : list<Object>,保存容器中的所有数据列表,包含使用addItem()或者insertItem()方法动态添加或插入的项。与contentChildren不同,contentData包含了所有对象,并且插入或移动项目时,不会重新排序。
  • contentHeight : real,此属性保存内容高度。用于计算容器的总隐式高度。除非显式赋值它,否则内容高度将根据容器中项目的隐式高度自动计算。
  • contentWidth : real,和contentHeight一样,此属性保存内容高度
  • contentModel : model,只读属性,此属性保存item的内容模型。
  • count : int,只读属性,获取item数量
  • currentIndex : int,当前item索引号
  • currentItem : Item ,只读属性,当前item

Methods

  • void addItem(item),添加一个item
  • void decrementCurrentIndex(),递减容器的当前索引
  • void incrementCurrentIndex(),递增容器的当前索引
  • void insertItem(index, Item item),在index处插入一个item
  • Item itemAt(index),获取指定index处的item
  • void moveItem(from, int to),移动item从from索引到to索引位置
  • void removeItem(item),删除容器中指定的item
  • void setCurrentIndex(index),删除容器中指定索引的item
  • Item takeItem(index),删除并返回指定索引的item

 

2.SwipeView介绍

SwipeView(滑动视图)通过一组页面填充,每次只显示一个页面。用户可以通过横向滑动在页面之间导航,一般会将它与PageIndicator结合使用.

它的属性如下所示:

  • horizontal : bool,只读属性,此属性保存滑动视图是否水平方向的
  • interactive : bool,默认为true,表示用户可以滑动视图
  • orientation : enumeration,滑动视图方向,默认为Qt.Horizontal
  • vertical : bool ,只读属性,此属性保存滑动视图是否垂直方向的

它的Attached Properties附加属性如下所示:

  • index : int,此附加属性保存SwipeView中每个子项的索引。它附加到SwipeView的每个item项中。
  • isCurrentItem : bool,如果此子项是当前项,则此附加属性为true。它附加到SwipeView的每个item项中。
  • isNextItem : bool,如果此子项是下一项,则此附加属性为true。它附加到SwipeView的每个item项中。
  • isPreviousItem : bool如果此子项是上一个项目,则此附加属性为true。它附加到SwipeView的每个item项中。
  • view : SwipeView 此附加属性保存管理此子项的视图。它附加到SwipeView的每个item项。

使用SwipeView时,一般会将它与PageIndicator结合使用.PageIndicator用于标志在多个页面的容器时,当前活动的页面是哪个小圆圈.
示例代码如下所示:

  1. Window {
  2. id: window
  3. width: 400
  4. height: 400
  5. visible: true
  6. SwipeView {
  7. id: view
  8. currentIndex: 1
  9. anchors.fill: parent
  10. Rectangle {
  11. id: firstPage
  12. color: "red"
  13. }
  14. Rectangle {
  15. id: secondPage
  16. color: "yellow"
  17. }
  18. Rectangle {
  19. id: thirdPage
  20. color: "blue"
  21. }
  22. }
  23. PageIndicator {
  24. id: indicator
  25. count: view.count
  26. currentIndex: view.currentIndex
  27. anchors.bottom: view.bottom
  28. anchors.horizontalCenter: parent.horizontalCenter
  29. }
  30. }

效果如下所示所示:

 

3.SwipeView实战

为了方便后续更加方便添加模块,所以我们还需要重构之前V1版本整个文件目录结构,重构后的文件夹如下所示:

接下来我们就在我们之前v1版本里面添加一个Page.qml,通过SwipeView实现切换微信主界面每页列表, Page.qml如下所示:

  1. import QtQuick 2.12
  2. import Qt.labs.folderlistmodel 2.12
  3. import QtQuick.Layouts 1.12
  4. import QtQuick.Controls 2.12
  5. import "qrc:/bar" as Bars
  6. import "qrc:/recentFirend" as RencentFirend
  7. Rectangle {
  8. id: container
  9. anchors.fill: parent
  10. ListModel { // 最近好友列表
  11. id: recentFirendModel
  12. // 格式如下所示:
  13. // 'name' : 好友名称
  14. // 'headSrc' : 头像位置
  15. // 'recode'(聊天记录) : [date(时间), msgType(hint提示、hintDate时间提示、recv接受提示、send发送提示), msg(信息内容)]
  16. }
  17. ColumnLayout {
  18. id: pageList
  19. anchors.fill: parent
  20. spacing: 0
  21. SwipeView {
  22. id: view
  23. currentIndex: 0
  24. Layout.fillHeight: true
  25. Layout.fillWidth: true
  26. interactive: false
  27. RencentFirend.RecentFirendList {
  28. id: recentFirendList
  29. onNewFirendRequest: {
  30. addExample()
  31. }
  32. onEnterRequest: {
  33. var obj = newJumpWindow("qrc:/chat/ChatList.qml", recentFirendModel.get(index));
  34. console.log("",recentFirendModel.get(index).name)
  35. obj.show();
  36. }
  37. Component.onCompleted: {
  38. recentFirendList.setModel(recentFirendModel)
  39. }
  40. }
  41. Rectangle {
  42. Text {
  43. text: "Page2"
  44. anchors.centerIn: parent
  45. font.pixelSize: 24
  46. font.family: "Microsoft Yahei"
  47. }
  48. }
  49. Rectangle {
  50. Text {
  51. text: "Page3"
  52. anchors.centerIn: parent
  53. font.pixelSize: 24
  54. font.family: "Microsoft Yahei"
  55. }
  56. }
  57. Rectangle {
  58. Text {
  59. text: "Page4"
  60. anchors.centerIn: parent
  61. font.pixelSize: 24
  62. font.family: "Microsoft Yahei"
  63. }
  64. }
  65. onCurrentIndexChanged: {
  66. menuBar.swipeIndex(currentIndex)
  67. }
  68. }
  69. Bars.MenuBar {
  70. id: menuBar
  71. Layout.fillHeight: false
  72. Layout.fillWidth: true
  73. Layout.preferredHeight: 60
  74. onIndexClicked: {
  75. view.currentIndex = index
  76. }
  77. }
  78. }
  79. ... ...
  80. }

最终效果如下所示(支持自适应布局):

 

代码已上传群里, 未完待续,  后续出 微信V3版-添加通讯录列表控件(通过字典树实现中文转拼音排序)

 

原文链接:http://www.cnblogs.com/lifexy/p/14905303.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号