Qt 小例子学习45 - QStringList视图播放列表

2024-04-13 15:08

本文主要是介绍Qt 小例子学习45 - QStringList视图播放列表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Qt 小例子学习45 - QStringList视图播放列表

FileModel.h

#ifndef FILEMODEL_H
#define FILEMODEL_H#include <QAbstractListModel>
#include <QDirIterator>
#include <QFuture>
#include <QMetaType>
#include <QUrl>
#include <QtConcurrent>struct File
{Q_GADGETQ_PROPERTY(QString name MEMBER name)Q_PROPERTY(QUrl url MEMBER url)
public:QString name;QUrl url;File(const QString &name = ""){this->name = QFileInfo(name).fileName();this->url = QUrl::fromLocalFile(name);}
};
Q_DECLARE_METATYPE(File)class FileModel : public QAbstractListModel
{enum dashBoardRoles { NameRole = Qt::UserRole + 1, URLRole };Q_OBJECTQ_PROPERTY(QString folder READ folder WRITE setFolder NOTIFY folderChanged)Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFiltersNOTIFY nameFiltersChanged)
public:FileModel(QObject *parent = Q_NULLPTR) : QAbstractListModel(parent) {}Q_INVOKABLE QVariant get(int index){return QVariant::fromValue(m_all_dirs[index]);}int rowCount(const QModelIndex &parent = QModelIndex()) const{Q_UNUSED(parent)return m_all_dirs.count();}QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const{if (index.row() < 0 && index.row() >= rowCount())return QVariant();File file = m_all_dirs[index.row()];if (role == NameRole)return file.name;else if (role == URLRole)return file.url;return QVariant();}QHash<int, QByteArray> roleNames() const{QHash<int, QByteArray> roles;roles[NameRole] = "fileName";roles[URLRole] = "url";return roles;}QString folder() const { return mFolder; }void setFolder(const QString &folder){if (mFolder == folder)return;mFolder = folder;emit folderChanged();findFiles();}QStringList nameFilters() const { return mNameFilters; }void setNameFilters(const QStringList &nameFilters){if (mNameFilters == nameFilters)return;mNameFilters = nameFilters;emit nameFiltersChanged();findFiles();}signals:void folderChanged();void nameFiltersChanged();private:void findFiles(){beginResetModel();m_all_dirs.clear();if (QDir(mFolder).exists()){QFuture<QStringList> future = QtConcurrent::run([ = ](){QStringList files;QDirIterator it(mFolder, mNameFilters, QDir::Files,QDirIterator::Subdirectories);while (it.hasNext()){files << it.next();}return files;});QStringList fullNames = future.result();for (const QString &fullName : fullNames){File file{fullName};m_all_dirs << file;}}endResetModel();}QString mFolder;QList<File> m_all_dirs;QStringList mNameFilters;
};#endif // FILEMODEL_H

main.qml

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtMultimedia 5.8import com.eyllanesc.filemodel 1.0ApplicationWindow {visible: truewidth: 640height: 480title: qsTr("Tabs")SwipeView {id: swipeViewanchors.fill: parentcurrentIndex: tabBar.currentIndexPage {ListView {id: lvwidth: 200; height: 400Component {id: fileDelegateText { text: fileNameMouseArea{anchors.fill: parentonClicked: playMusic(index)}}}model: FileModel{id: myModelfolder: "/home/xz/视频"nameFilters: ["*.mp4"]}delegate: fileDelegate}Button {id: buttonanchors.top: lv.bottomwidth: parent.widthtext: "Play"background: Rectangle {implicitHeight: 40border.color: "#26282a"border.width: 2radius: 4}onClicked: playMusic(0)}}Page {MediaPlayer {id: playeronStopped: {if(status===MediaPlayer.EndOfMedia){playMusic((lv.currentIndex+1) % lv.count)}}}VideoOutput {id: videoanchors.fill: parentsource: player}}}function playMusic(index){player.stop()player.source = myModel.get(index).urlplayer.play()swipeView.setCurrentIndex(1)}footer: TabBar {id: tabBarcurrentIndex: swipeView.currentIndexTabButton {text: qsTr("Page 1")}TabButton {text: qsTr("Page 2")}}
}

main.cpp

#include "filemodel.h"#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTimer>
#include <QDebug>int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endifQGuiApplication app(argc, argv);qmlRegisterType<FileModel>("com.eyllanesc.filemodel", 1, 0, "FileModel");QQmlApplicationEngine engine;engine.load(QUrl(QStringLiteral("qrc:/main.qml")));if (engine.rootObjects().isEmpty())return -1;return app.exec();
}

在这里插入图片描述

这篇关于Qt 小例子学习45 - QStringList视图播放列表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/900475

相关文章

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

Spring MVC使用视图解析的问题解读

《SpringMVC使用视图解析的问题解读》:本文主要介绍SpringMVC使用视图解析的问题解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring MVC使用视图解析1. 会使用视图解析的情况2. 不会使用视图解析的情况总结Spring MVC使用视图

Qt 中 isHidden 和 isVisible 的区别与使用小结

《Qt中isHidden和isVisible的区别与使用小结》Qt中的isHidden()和isVisible()方法都用于查询组件显示或隐藏状态,然而,它们有很大的区别,了解它们对于正确操... 目录1. 基础概念2. 区别清见3. 实际案例4. 注意事项5. 总结1. 基础概念Qt 中的 isHidd

QT移植到RK3568开发板的方法步骤

《QT移植到RK3568开发板的方法步骤》本文主要介绍了QT移植到RK3568开发板的方法步骤,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录前言一、获取SDK1. 安装依赖2. 获取SDK资源包3. SDK工程目录介绍4. 获取补丁包二

Qt把文件夹从A移动到B的实现示例

《Qt把文件夹从A移动到B的实现示例》本文主要介绍了Qt把文件夹从A移动到B的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录如何移动一个文件? 如何移动文件夹(包含里面的全部内容):如何删除文件夹:QT 文件复制,移动(

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Qt实现发送HTTP请求的示例详解

《Qt实现发送HTTP请求的示例详解》这篇文章主要为大家详细介绍了如何通过Qt实现发送HTTP请求,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、添加network模块2、包含改头文件3、创建网络访问管理器4、创建接口5、创建网络请求对象6、创建一个回复对