[Qt/qml]程序之外显示的弹幕

/ 44,342评论 / 217973阅读 / 4点赞

前几天课设做个聊天系统,顺便实现了这个在程序外显示弹幕作为消息提示的功能,这里记录一下怎么搞。

效果

思路

实操

import QtQuick 2.3
import QtQuick.Controls 2.5
//窗口
ApplicationWindow  {
    id:root
    property string str: "我的洛天依"  //弹幕文本内容
    width: 300
    height: 40

    flags:  Qt.FramelessWindowHint  //去掉窗口标题栏
    color:"#00000000"   //设置背景透明

    Rectangle {     //使用矩形包裹Text是为了在Text外面可以增加圆角矩形边框效果
        anchors.fill: parent
        border.width: 1     //设置边框
        border.color: "#66ccff"
        color:"#00000000"
        radius: 20          //边框圆角
        clip: true          //超出矩形范围的内容隐藏

        Text {      //弹幕文本
            anchors.fill: parent
            text: str
            font.bold: true
            font.pointSize: 15
            color: "#66ccff"
            style: Text.Outline     //设置文本样式,在字体轮廓加一层styleColor指定的颜色边缘,防止背景导致文本不明显
            styleColor: "#fff"
            horizontalAlignment: Text.AlignHCenter  //居中
            verticalAlignment:   Text.AlignVCenter
            clip: true
        }
    }
}
import QtQuick
import QtQuick.Window 2.3
import QtQuick.Controls 2.5

Window {
    id:mainWin
    width: 700
    height: 500
    visible: true
    title: qsTr("MimicryChat")

    //添加弹幕
    function addBarrage(in_str) {
        var newCom = Qt.createComponent("Barrage.qml");   //创建弹幕窗口
        var newWin = newCom.createObject();
        newWin.x   = 200;
        newWin.y   = Math.random() * 900;   //随机竖直位置
        newWin.str = in_str;   //设置弹幕文本
        newWin.show();         //显示弹幕窗口

        //设置定时控制器
        let timer = Qt.createQmlObject("import QtQuick 2.14; Timer {}", mainWin);
        timer.interval = 20;    //执行间隔时间(单位ms)
        timer.repeat = true;    //重复执行
        timer.triggered.connect(function() {  //执行内容
            if(newWin.x < 1700) {  //如果没到右边缘就移动2个像素点
                newWin.x += 2;
            } else {
                timer.stop();      //到达右边缘,则停止定时控制器,并销毁弹幕
                newWin.close();
            }
        });
        timer.start();   //启动定时器
    }
    Rectangle {   //主窗口背景颜色
        anchors.fill: parent
        color: "#dde8fc"
        radius: 20
    }
    Button {
        width:100
        height: 40
        text:"发送弹幕"
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        //水平居中
        anchors.horizontalCenter: parent.horizontalCenter
        onClicked: {
            addBarrage("嘿嘿,我的洛天依");
        }
    }
}

问题

  1. StephenLoulk说道:

    Будівельний портал https://apis-togo.org Все про будівництво та ремонт від А до Я.

  2. NelsonSaIlk说道:

    mexico drug stores pharmacies: mexican pharmaceuticals online – buying prescription drugs in mexico

  3. Diplomi_cwol说道:

    Добрый день!
    Приобрести документ института вы можете в нашей компании.
    ast-diplomy.com/kupit-diplom-chelyabinsk

  4. Trefoot说道:

    Привет, друзья!
    Быстрая покупка диплома старого образца: возможные риски
    kabtaferplus.com/купить-диплом-советы-и-рекомендации
    Будем рады вам помочь!.

  5. IrvinTof说道:

    The legendary Turkish club Konyaspor https://turkey.superligsitesi.net is the pride of Konya and many champions of Turkey, regularly participating in European cups.

  6. AlfredHunse说道:

    F.Bahce https://istanbul.fenerbahce-haberleri.com is much more than a football club. It is an integral part of Istanbul’s cultural heritage, uniting millions of fans around the world.

  7. Arnoldscava说道:

    mexican border pharmacies shipping to usa medication from mexico pharmacy mexican drugstore online

  8. Lazrmna说道:

    Добрый день!
    Мы готовы предложить дипломы любой профессии по разумным тарифам.
    2dommedical.com/2024/06/30/купить-главу-диплома

  9. Waynetar说道:

    mexican mail order pharmacies: mexican mail order pharmacies – best online pharmacies in mexico

  10. Lazrbkr说道:

    Добрый день!
    Мы предлагаем дипломы любой профессии по разумным тарифам.
    phongthuygiaphat.com/купить-заполненный-диплом

  11. Waynetar说道:

    п»їbest mexican online pharmacies: п»їbest mexican online pharmacies – best online pharmacies in mexico

  12. DominicHOr说道:

    best online pharmacies in mexico: buying from online mexican pharmacy – mexican border pharmacies shipping to usa

  13. NelsonSaIlk说道:

    mexico pharmacies prescription drugs: mexican pharmaceuticals online – purple pharmacy mexico price list

  14. DominicHOr说道:

    buying prescription drugs in mexico: mexican drugstore online – purple pharmacy mexico price list

  15. Arnoldscava说道:

    medication from mexico pharmacy buying from online mexican pharmacy mexico pharmacies prescription drugs

  16. NelsonSaIlk说道:

    mexican mail order pharmacies: mexico drug stores pharmacies – mexican drugstore online

  17. Arnoldscava说道:

    pharmacies in mexico that ship to usa [url=http://mexicandeliverypharma.com/#]mexican drugstore online[/url] buying from online mexican pharmacy

  18. Lazrjcd说道:

    Здравствуйте!
    Заказать диплом университета.
    zdravamir.ru/index.php?option=com_myblog&show=n-n-n-n-on-n-n-n-n-n-&Itemid=

  19. Arnoldscava说道:

    mexico drug stores pharmacies medicine in mexico pharmacies mexican pharmaceuticals online

  20. Arnoldscava说道:

    buying prescription drugs in mexico п»їbest mexican online pharmacies mexico pharmacies prescription drugs

  21. Waynetar说道:

    mexican border pharmacies shipping to usa: mexico pharmacies prescription drugs – buying from online mexican pharmacy

  22. DominicHOr说道:

    mexico drug stores pharmacies: mexican online pharmacies prescription drugs – mexican drugstore online

  23. Arnoldscava说道:

    medication from mexico pharmacy best online pharmacies in mexico buying prescription drugs in mexico

  24. Elmeressek说道:

    Galatasaray’s https://istanbul.galata-saray.net path from ups and downs to unconditional dominance in Istanbul. The story of one of Turkey’s strongest clubs returning to the top.

  25. LarryUnlow说道:

    An impressive story https://turkey.nba-tr.com of how Turkey became a leading power in the NBA and produced many world-class star players.

  26. NelsonSaIlk说道:

    mexican border pharmacies shipping to usa: medication from mexico pharmacy – buying from online mexican pharmacy

  27. Trefjwq说道:

    Здравствуйте!
    Как приобрести аттестат о среднем образовании в Москве и других городах
    protectorakanaan.com/купить-диплом-проверенные-способы
    Окажем помощь!.

  28. JameslEt说道:

    The fascinating story of Turkey’s https://turkey.oyunlar-espor.com rise to the heights of eSports glory, from amateur tournaments to victories at world championships.

  29. Stevensar说道:

    Kylian Mbappe https://real-madrid.kylianmbappe-tr.net one of the most talented young football players, will join Real Madrid in 2024. He joined. Find out how he quickly rose to the top of world football.

  30. Diplomi_ryol说道:

    Привет, друзья!
    Заказать документ о получении высшего образования можно в нашем сервисе.
    ast-diploms.com/kupit-diplom-s-registraciej

  31. Waynetar说道:

    buying prescription drugs in mexico online: purple pharmacy mexico price list – medicine in mexico pharmacies

  32. ErnestoBat说道:

    The gripping story of Stephen Curry’s https://golden-state-warriors.stephencurry-fr.com difficult journey to becoming an NBA superstar and the leader of the undefeated Golden State Warriors dynasty.

  33. Lazrkaq说道:

    Привет, друзья!
    Мы изготавливаем дипломы любых профессий по приятным ценам.
    thmyan1.pgdthapmuoidt.edu.vn/купить-диплом-сварщика-5-разряд

  34. DominicHOr说道:

    buying prescription drugs in mexico: mexico drug stores pharmacies – mexican pharmaceuticals online

  35. Onwin说道:

    Thank you for content. Area rugs and online home decor store. Hello Administ . Seo Paketi Skype: By_uMuT@KRaLBenim.Com -_- live:by_umut

  36. Arnoldscava说道:

    mexican border pharmacies shipping to usa purple pharmacy mexico price list п»їbest mexican online pharmacies

  37. Waynetar说道:

    mexico drug stores pharmacies: purple pharmacy mexico price list – mexican online pharmacies prescription drugs

  38. Lazrdkn说道:

    Добрый день!
    Купить диплом о высшем образовании.
    poobshchaemsya.ru/gallery/image/kak-nayti-proverennyy-internet-magazin-s-shirokim-katalogom-diplomov-353

  39. Arnoldscava说道:

    pharmacies in mexico that ship to usa buying prescription drugs in mexico mexican rx online

  40. Manrijd说道:

    Здравствуйте!
    Купить документ о получении высшего образования
    diploms-x24.ru/kupit-diplom-moskva

  41. Xazrmug说道:

    Привет!
    Покупка диплома о среднем полном образовании: как избежать мошенничества?
    arusak-diploms-srednee.ru/kupit-diplom-kandidata-nauk

  42. Arnoldscava说道:

    buying from online mexican pharmacy mexico pharmacies prescription drugs medicine in mexico pharmacies

  43. Mazrffd说道:

    Здравствуйте!
    Процесс получения диплома стоматолога: реально ли это сделать быстро?
    mans-diplomyxx.ru

  44. Trefbpi说道:

    Привет, друзья!
    Процесс получения диплома стоматолога: реально ли это сделать быстро?
    theplaygamepicks.com/диплом-техникума-где-и-как-купить
    Рады помочь!.

  45. NelsonSaIlk说道:

    medication from mexico pharmacy: buying from online mexican pharmacy – pharmacies in mexico that ship to usa

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注