首页 > 其他分享 >一个简单的QML滚动字幕实现

一个简单的QML滚动字幕实现

时间:2023-12-22 15:34:59浏览次数:27  
标签:滚动 settings int common 字幕 QML include configParam QString

一个简单的QML滚动字幕实现

目录结构

configparam.h

#ifndef CONFIGPARAM_H
#define CONFIGPARAM_H

#include <QObject>
#include <QTypeInfo>

class configParam : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString backGroundColor READ getBackGroundColor NOTIFY backGroundColorChanged)
    Q_PROPERTY(QString text READ getText NOTIFY textChanged)
    Q_PROPERTY(QString textColor READ getTextColor NOTIFY textColorChanged)
    Q_PROPERTY(int textSize READ getTextSize NOTIFY textSizeChanged)
    Q_PROPERTY(int textWeight READ getTextWeight NOTIFY textWeightChanged)
    Q_PROPERTY(int screenWidth READ getScreenWidth NOTIFY screenWidthChanged)
    Q_PROPERTY(int screenHeight READ getScreenHeight NOTIFY screenHeightChanged)
    Q_PROPERTY(double wordSpacing READ getWordSpacing NOTIFY wordSpacingChanged)
    Q_PROPERTY(bool runEnabled READ getRunEnabled NOTIFY runEnabledChanged)
    Q_PROPERTY(int duration READ getDuration NOTIFY durationChanged)

signals:
    void backGroundColorChanged();
    void textChanged();
    void textColorChanged();
    void textSizeChanged();
    void textWeightChanged();
    void screenWidthChanged();
    void screenHeightChanged();
    void wordSpacingChanged();
    void runEnabledChanged();
    void durationChanged();

public:
    configParam(QString filename);

    QString getBackGroundColor();
    QString getText();
    QString getTextColor();
    int getTextSize();
    int getTextWeight();
    int getScreenWidth();
    int getScreenHeight();

    double getWordSpacing();
    bool getRunEnabled();
    int getDuration();

private:
    QString backGroundColor;
    QString text;
    QString textColor;
    int textSize;
    int textWeight;
    int screenWidth;
    int screenHeight;
    double wordSpacing;
    bool runEnabled;
    int duration;
};

#endif // CONFIGPARAM_H

configparam.cpp

#include "configparam.h"
#include <QSettings>
#include <QDebug>
#include <QTextCodec>

configParam::configParam(QString filename)
{
    QSettings settings(filename, QSettings::IniFormat);

    settings.setIniCodec(QTextCodec::codecForName("utf-8"));
    backGroundColor = settings.value("common/backGroundColor").toString();
    text = settings.value("common/text").toString();
    textColor = settings.value("common/textColor").toString();
    textSize = settings.value("common/textSize").toInt();
    textWeight = settings.value("common/textWeight").toInt();
    screenWidth = settings.value("common/screenWidth").toInt();
    screenHeight = settings.value("common/screenHeight").toInt();
    wordSpacing = settings.value("common/wordSpacing").toDouble();
    runEnabled = settings.value("common/runEnabled").toBool();
    duration =  settings.value("common/duration").toInt();
    qDebug()<<backGroundColor << text <<textColor << textSize << textWeight <<screenWidth<<screenHeight<<wordSpacing<<runEnabled;
}

QString configParam::getBackGroundColor()
{
    return backGroundColor;
}

QString configParam::getText()
{
    return text;
}

QString configParam::getTextColor()
{
    return textColor;
}

int configParam::getTextSize()
{
    return textSize;
}

int configParam::getTextWeight()
{
    return textWeight;
}

int configParam::getScreenWidth()
{
    return screenWidth;
}

int configParam::getScreenHeight()
{
    return screenHeight;
}

double configParam::getWordSpacing()
{
    return wordSpacing;
}

bool configParam::getRunEnabled()
{
    return runEnabled;
}

int configParam::getDuration()
{
    return duration;
}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QFile>
#include <QDebug>

#include "configparam.h"


int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);
    QString fileName = QCoreApplication::applicationDirPath() + "/config.ini";

    if(!QFile::exists(fileName)){
        qDebug() << "File not found:" << fileName;
        return -1;
    }

    configParam param(fileName);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);

    auto context = engine.rootContext();
    context->setContextProperty("configParam",&param);

    engine.load(url);

    return app.exec();
}

main.qml

import QtQuick 2.9
import QtQuick.Window 2.3

Window {
    id: root
    visible: true
    width: configParam.screenWidth
    height: configParam.screenHeight
//    title: qsTr("Hello World")
    flags: Qt.FramelessWindowHint

    Item{
        anchors.fill: parent
        Rectangle{
            anchors.fill: parent
            anchors.centerIn: parent
            color: configParam.backGroundColor

            Text {
                id: txt

                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter

                text: configParam.text
                color: configParam.textColor

                font.pixelSize: configParam.textSize
                font.weight: configParam.textWeight
                font.wordSpacing: configParam.wordSpacing
                font.bold: true
//              font.family: "Times New Roman"

                XAnimator  {
                    id:animation
                    target: txt
                    running: configParam.runEnabled
                    loops: Animation.Infinite
                    from: root.width
                    to: -txt.width
                    duration: configParam.duration
                }
            }

        }
    }
}

Qt .pro文件

QT += quick
CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp \
    configparam.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
    configparam.h

config.ini配置文件

[common]
#设置屏幕分辨率宽/高
screenWidth=1920
screenHeight=600

#设置背景色,纯色包括,black,white,green,blue等
backGroundColor="black"

#设置显示的文字内容
text="欢迎XX领导莅临参观指导"

#设置字体大小与粗细
textSize=150
textWeight=100

#设置字体颜色,纯色包括,black,white,green,blue等
textColor="red"

#设置字间距
wordSpacing=0

#设置是否滚动显示
runEnabled=true

#设置滚动时长(速度),单位ms
duration=10000

标签:滚动,settings,int,common,字幕,QML,include,configParam,QString
From: https://www.cnblogs.com/Amumu1/p/17921668.html

相关文章

  • 【ffmpeg】使用 FFmpeg 给视频文件添加旁白和字幕
    一、视频添加旁白添加旁白需要将音频文件和视频文件的声音叠加在一起,可以使用FFmpeg的amix过滤器,以下是一个示例命令ffmpeg-ia.mp4-ia.mp3-filter_complex"[0:a]volume=0.5[a0];[1:a]volume=3[a1];[a0][a1]amix=inputs=2:duration=first[aout]"-map0:v-map"[aout......
  • 滚动阴影解决方案
    1.backgroud,限制高度,加overflow,codeopen,.g-scroll{top:-1px;position:relative;height:500px;overflow-y:scroll;overflow-x:hidden;background:linear-gradient(#fff,transparent)top/100%100px,radial-grad......
  • Ant Design + List + 滚动加载列表
    <div><InfiniteScrolldataLength={load.length}hasMore={load.length<datas.length+1}loader={null}endMessage={null}onScroll={(e)=>{/*屏幕发生滚动的执行方法。也可以采用官网的next+scrollableTarget或者loadMore也可*/......
  • QML语法
    一,基本语法1、import导入需要使用的模块。类似于C++,include。2、一个QML文件只能有一个根元素。类似一个QML文件就是自然界的一棵树。3、元素声明方法:类型{}4、元素下定义属性,属性赋值方法:name:value5、元素可并行,也可嵌套。子元素可用通过父元素id访问它的子元素和属性......
  • qt滚动条样式设计
    /*整个垂直滚动条区域样式*/QScrollBar:vertical{border:none;background:rgb(30,30,30);width:10px;margin:0px00px0;}/*整个水平滚动条区域样式*/QScrollBar:horizontal{border:none;background:rgb(30,30,30);height:10px;m......
  • qt 折叠与展开 窗口,折叠列表,展开列表,抽屉效果,根据窗口大小自动产生滚动条
      自定义折叠控件,h头文件#ifndefQUESTIONBANKWIDGET_H#defineQUESTIONBANKWIDGET_H#include<QWidget>#include<QVBoxLayout>#include<QHBoxLayout>#include<QToolButton>#include<QPushButton>#include<QLabel>#include<......
  • cocos creator ScrollView不滚动问题
    拖放默认的ScrollView可以,可以滚动显示文字,结构如下:ScrollView scrollBar(滚动条结点,不显示滚动条时,可直接删除,删除后把ScrollView里面的VertticalScrollBar值清了) bar view(视图结点) content item(具体文本)content......
  • C++和QML混合编程
    一、QML访问C++方法Qt元对象系统中注册C++类,在QML中实例化、访问。C++中实例化并设置为QML上下文属性,在QML中直接使用。      比较:方法1可以使C++类在QML中作为一个数据类型,例如函数参数类型或属性类型,也可以使用其枚举类型、单例等,功能更强大。二、QML访......
  • 新增数据后自动滚动到表格底部
    <table> <thead> <tr> <th></th> </tr> </thead> <tbody:ref="indexInfo.index_id":id="indexInfo.index_id"> <tr> <td></td> </tr> </tbody>......
  • vue路由切换时内容组件的滚动条回到顶部
    vue路由切换时内容组件的滚动条回到顶部:https://blog.csdn.net/Macao7_W/article/details/125517519?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170252373016800185826420%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=......