亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用qt quick-ListView仿微信好友列表和聊天列表的示例代碼

 更新時間:2021年06月13日 08:54:01   作者:諾謙  
本文以微信好友列表為例給大家學習listview的相關知識,通過實例demo給大家詳解qt quick-ListView仿微信好友列表和聊天列表的實現(xiàn)方法,需要的朋友參考下吧

1.視圖模型介紹

在Qml中、常見的View視圖有:

  • ListView: 列表視圖,視圖中數(shù)據(jù)來自ListModel、XmlListModel或c++中繼承自QAbstractItemModel或QAbstractListModel的自定義模型類
  • TableView: 和excel類似的視圖
  • GridView: 網(wǎng)格視圖,類似于home菜單那樣,排列著一個個app小圖標
  • PathView: 路徑視圖,可以根據(jù)用戶自定義的path路徑來顯示不一樣的視圖效果
  • SwipeView: 滑動視圖,使用一組頁面填充。每次只顯示一個頁面。用戶可以通過橫向滑動在頁面之間導航,一般會將它與PageIndicator結合使用

本章首先來學習ListView.以微信好友列表為例:

里面的每個好友就是由一個個 item 組成的,存在視圖中的model里,然后寫一個delegate組件,即可通過ListView顯示出來.

由于時間不是很多,所以本章實現(xiàn)的微信好友列表和聊天列表(v1版本)是通過模擬數(shù)據(jù)實現(xiàn)的,等后面有時間后,再來實現(xiàn)個一個真正的內網(wǎng)聊天工具.

2.demo實現(xiàn)(支持自適應)

好友列表如下圖所示:

聊天列表如下圖所示:

整個效果如下所示:

覺得GIF模糊的話,可以轉彎去bilibilihttps://www.bilibili.com/video/BV1Z64y1R7kL/

由于代碼上傳CSDN,會導致有些同學可能沒積分無法下載,所以已經(jīng)上傳群里了.

如果下載后學習有收獲,一定要來這里給我點個贊呀,都沒動力更新文章了,贊的人太少了

3.重要組件-實現(xiàn)氣泡組件源碼

import QtQuick 2.0
import "BubbleNormal.js" as BubbleNormal
import "BubbleBlue.js" as BubbleBlue
import "BubbleBlack.js" as BubbleBlack


Item {
    id: container
    property var bubbleIndex: 0
    property string msgText: ""
    property bool isSend: true
    property int iconHeight: 40
    property int maxWidth: 100

    Canvas {
        id: canvas
        anchors.fill: parent

        onPaint: {
            bubble().drawBubble(getContext('2d'));
        }
    }

    Text {
        id: text
        text: msgText
        font.pixelSize: 17
        font.family: "Microsoft Yahei"
        wrapMode: Text.WrapAnywhere

        horizontalAlignment:  Text.AlignLeft
        verticalAlignment: Text.AlignVCenter
        anchors.fill: parent
    }

    Component.onCompleted: {
        bubble().initText();
        bubble().reUpdateSize();
        canvas.requestPaint();
    }

    onBubbleIndexChanged: {
        bubble().initText();
        bubble().reUpdateSize();
        canvas.requestPaint();
    }
    function bubble() {
        switch (bubbleIndex) {
            case 0 :  return BubbleNormal
            case 1 :  return BubbleBlue
            case 2 :  return BubbleBlack
            default: return BubbleNormal
        }
    }

}

代碼如上所示,只要用戶更改了bubbleIndex值,那么我們就會去馬上調用替換后對應的氣泡js文件的function(),進行初始化消息、重繪氣泡背景。這個組件實現(xiàn)后,我們如果想實現(xiàn)其它的氣泡,也可以直接往里加就好了

4.重要組件-實現(xiàn)聊天列表委托源碼

/****************************************************************************
**  聊天列表委托
** Author   : 諾謙 https://www.cnblogs.com/lifexy/
** Create   : 2021-6-12
****************************************************************************/

import QtQuick 2.12
import QtGraphicalEffects 1.12
import "./bubble" as Bubble
import "qrc:/Common.js" as Common

Item {
    id: container
    property var headSrc
    property var myHeadSrc : "qrc:/head/myhead.jpg"
    property var bubbleIndex : 0

    height: _layout.height + 10
    width: ListView.view.width
    state: msgType
    states: [
        State {
              name: "hint"
              AnchorChanges { target: _layout;
                  anchors.horizontalCenter: container.horizontalCenter;
                  anchors.verticalCenter: container.verticalCenter; }
        },
        State {
              name: "hintDate"
              AnchorChanges { target: _layout;
                  anchors.horizontalCenter: container.horizontalCenter;
                  anchors.verticalCenter: container.verticalCenter; }
        },
        State {
              name: "recv"
              AnchorChanges { target: _layout;
                  anchors.left: container.left;
                  anchors.verticalCenter: container.verticalCenter; }
        },
        State {
              name: "send"
              AnchorChanges { target: _layout;
                  anchors.right: container.right;
                  anchors.verticalCenter: container.verticalCenter; }
        }
    ]

    Row {
        id: _layout
        anchors.leftMargin: 20
        anchors.rightMargin: 20
        spacing: 4
        layoutDirection : msgType == "send" ? Qt.RightToLeft : Qt.LeftToRight
        HeadImage {
            id: _head
            width : 50
            height : 50
            headUrl: msgType == "recv" ? headSrc : myHeadSrc
            visible: msgType == "recv" || msgType == "send"
        }

        Text {
            id: _hint
            visible: msgType == "hintDate" || msgType == "hint"
            text: msgType == "hintDate" ? getChatDate() : msg
            color: "#B0B0B0"
            font.pixelSize: 14
            font.family: "Microsoft Yahei"
            wrapMode: Text.WrapAnywhere
            elide: Text.ElideRight
            width: container.width - 40
            height: 30
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }

        Bubble.ChatBubble {
            id: _msg
            visible: msgType == "recv" || msgType == "send"
            msgText:  msgType == "recv" || msgType == "send" ?  msg : ""
            isSend: msgType == "send" ? true : false
            iconHeight: _head.height
            maxWidth: container.width - _layout.anchors.leftMargin * 2 - _head.width * 2 - _layout.spacing * 2
            bubbleIndex: container.bubbleIndex
        }
    }


    // 判斷消息時間,與當前時間間隔多久,來動態(tài)顯示
    function getChatDate () {
        var total = new Date() - date;
        if (total < (1000*60*60*24)) {
            return date.toLocaleTimeString(Qt.locale(), "hh:mm");
        } else if (total < (1000*60*60*24) * 2) {
            return "昨天 "+date.toLocaleTimeString(Qt.locale(), "hh:mm");
        } else if (total < (1000*60*60*24) * 3) {
            return "前天 "+date.toLocaleTimeString(Qt.locale(), "hh:mm");
        } else {
            return date.toLocaleString(Qt.locale(), "yyyy年M月d日 hh:mm");
        }
    } 
}

代碼如上所示,我們會去判斷消息類型:

  • 如果消息類型是"hint"類型,就直接居中顯示。
  • 如果消息類型是"hintDate"類型,則調用getChatDate()來動態(tài)獲取要如何顯示時間.
  • 如果消息類型是"recv"類型,則靠左顯示對方頭像,并加上氣泡消息
  • 如果消息類型是"send"類型,則靠又顯示自己頭像,并加上氣泡消息

以上就是qt quick-ListView高仿微信好友列表和聊天列表的詳細內容,更多關于qt 微信好友列表和聊天列表的資料請關注腳本之家其它相關文章!

相關文章

  • 基于Python實現(xiàn)DIT-FFT算法

    基于Python實現(xiàn)DIT-FFT算法

    FFT(Fast Fourier Transformation)是離散傅氏變換(DFT)的快速算法。即為快速傅氏變換。本文將用Python語言實現(xiàn)DIT-FFT算法,感興趣的可以了解一下
    2022-10-10
  • python中的生成器、迭代器、裝飾器詳解

    python中的生成器、迭代器、裝飾器詳解

    本文詳細講解了python中的生成器、迭代器、裝飾器,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • Python類的動態(tài)綁定實現(xiàn)原理

    Python類的動態(tài)綁定實現(xiàn)原理

    這篇文章主要介紹了Python類的動態(tài)綁定實現(xiàn)原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • 淺談Python pygame繪制機制

    淺談Python pygame繪制機制

    今天給大家?guī)淼氖顷P于Python的相關知識,文章圍繞著Python pygame繪制機制展開,文中有非常詳細的介紹及圖文示例,需要的朋友可以參考下
    2021-06-06
  • 4種非常實用的python內置數(shù)據(jù)結構

    4種非常實用的python內置數(shù)據(jù)結構

    這篇文章主要介紹了4種非常實用的python內置數(shù)據(jù)結構,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下
    2021-04-04
  • opencv之顏色過濾只留下圖片中的紅色區(qū)域操作

    opencv之顏色過濾只留下圖片中的紅色區(qū)域操作

    這篇文章主要介紹了opencv之顏色過濾只留下圖片中的紅色區(qū)域操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python中的二叉樹查找算法模塊使用指南

    Python中的二叉樹查找算法模塊使用指南

    二叉樹查找算法,在開發(fā)實踐中,會經(jīng)常用到。按照慣例,對于這么一個常用的東西,Python一定會提供輪子的。是的,python就是這樣,一定會讓開發(fā)者省心,降低開發(fā)者的工作壓力。
    2014-07-07
  • Python入門教程(四十一)Python的NumPy數(shù)組索引

    Python入門教程(四十一)Python的NumPy數(shù)組索引

    這篇文章主要介紹了Python入門教程(四十一)Python的NumPy數(shù)組索引,數(shù)組索引是指使用方括號([])來索引數(shù)組值,numpy提供了比常規(guī)的python序列更多的索引工具,除了按整數(shù)和切片索引之外,數(shù)組可以由整數(shù)數(shù)組索引、布爾索引及花式索引,需要的朋友可以參考下
    2023-05-05
  • Python爬取數(shù)據(jù)保存為Json格式的代碼示例

    Python爬取數(shù)據(jù)保存為Json格式的代碼示例

    今天小編就為大家分享一篇關于Python爬取數(shù)據(jù)保存為Json格式的代碼示例,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • 解決Python報錯:ValueError:operands?could?not?be?broadcast?together?with?shapes

    解決Python報錯:ValueError:operands?could?not?be?broadcast?t

    這篇文章主要給大家介紹了關于解決Python報錯:ValueError:operands?could?not?be?broadcast?together?with?shapes的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02

最新評論