Use QML_ELEMENT macro instead of qmlRegister* for mediaplayer

Updated the MediaPlayer example to use compile-time QML registration via
the QML_ELEMENT macros instead of qmlRegisterSingletonType. This aligns
the example with modern Qt 6 best practices and improves startup
performance and tooling integration.

Pick-to: 6.9 6.8
Task-number: QTBUG-136712
Change-Id: Id34670f4d45200b9a1424612f5a05bebead0a60e
Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
This commit is contained in:
Dheerendra Purohit 2025-05-22 17:04:32 +05:30
parent fea28023bc
commit 303c85a782
4 changed files with 9 additions and 11 deletions

View File

@ -13,6 +13,11 @@ qt_add_executable(MediaPlayerApp
filenameprovider.h
)
qt_add_qml_module(MediaPlayerApp
URI "io.qt.filenameprovider"
VERSION 1.0
)
qt_add_ios_ffmpeg_libraries(MediaPlayerApp)
add_subdirectory(MediaPlayer)

View File

@ -10,7 +10,6 @@ import QtQuick.Layouts
import QtCore
import MediaControls
import Config
import io.qt.filenameprovider
Rectangle {

View File

@ -6,11 +6,15 @@
#include <QObject>
#include <QFileInfo>
#include <qqmlregistration.h>
// Helper class to retrieve the filename from the given path using QFileInfo
class FileNameProvider: public QObject
{
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
public:
explicit FileNameProvider(QObject* parent = nullptr): QObject(parent) {}
Q_INVOKABLE static QString getFileName(const QString &p) { return QFileInfo(p).fileName(); }

View File

@ -7,9 +7,6 @@
#include <QDir>
#include <QMediaFormat>
#include <QMimeType>
#include "filenameprovider.h"
#include <algorithm>
using namespace Qt::Literals::StringLiterals;
@ -52,13 +49,6 @@ int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterSingletonType<FileNameProvider>("io.qt.filenameprovider", 1, 0, "FileNameProvider",
[] (QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
static auto * provider = new FileNameProvider();
return provider;
}
);
QCoreApplication::setApplicationName("MediaPlayer Example");
QCoreApplication::setOrganizationName("QtProject");
QCoreApplication::setApplicationVersion(QT_VERSION_STR);