欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

1.視圖模型介紹

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

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

本章首先來學(xué)習(xí)ListView.以微信好友列表為例:

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

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

2.demo實(shí)現(xiàn)(支持自適應(yīng))

好友列表如下圖所示:

聊天列表如下圖所示:

整個(gè)效果如下所示:

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

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

如果下載后學(xué)習(xí)有收獲,一定要來這里給我點(diǎn)個(gè)贊呀,都沒動(dòng)力更新文章了,贊的人太少了

3.重要組件-實(shí)現(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值,那么我們就會去馬上調(diào)用替換后對應(yīng)的氣泡js文件的function(),進(jìn)行初始化消息、重繪氣泡背景。這個(gè)組件實(shí)現(xiàn)后,我們?nèi)绻雽?shí)現(xiàn)其它的氣泡,也可以直接往里加就好了

4.重要組件-實(shí)現(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
        }
    }


    // 判斷消息時(shí)間,與當(dāng)前時(shí)間間隔多久,來動(dòng)態(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"類型,則調(diào)用getChatDate()來動(dòng)態(tài)獲取要如何顯示時(shí)間.
  • 如果消息類型是"recv"類型,則靠左顯示對方頭像,并加上氣泡消息
  • 如果消息類型是"send"類型,則靠又顯示自己頭像,并加上氣泡消息

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

相關(guān)文章

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

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

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

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

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

    Python類的動(dòng)態(tài)綁定實(shí)現(xiàn)原理

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

    淺談Python pygame繪制機(jī)制

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

    4種非常實(shí)用的python內(nèi)置數(shù)據(jù)結(jié)構(gòu)

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

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

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

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

    二叉樹查找算法,在開發(fā)實(shí)踐中,會經(jīng)常用到。按照慣例,對于這么一個(gè)常用的東西,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格式的代碼示例

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

    解決Python報(bào)錯(cuò):ValueError:operands?could?not?be?broadcast?t

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

最新評論