Skip to content

Instantly share code, notes, and snippets.

/fieliosingleton.h
Created Aug 3, 2015

Embed
What would you like to do?
A Qt Quick 2 extension plugin for file IO which can be used in statically-linked Qt Quick applications
#ifndef FILEIOSINGLETON_H
#define FILEIOSINGLETON_H
#include <QQuickItem>
#include <QDir>
#include <QFile>
class CFileIoSingleton : public QQuickItem
{
Q_OBJECT
Q_DISABLE_COPY(CFileIoSingleton)
CFileIoSingleton() {}
~CFileIoSingleton() {}
public:
static QObject *qmlInstance(QQmlEngine* pEngine, QJSEngine* pScriptEngine)
{
Q_UNUSED(pEngine);
Q_UNUSED(pScriptEngine);
return new CFileIoSingleton;
}
//-------------------------------------------------------------------------------------------------
// Method definitions
//-------------------------------------------------------------------------------------------------
/**
* @brief Checks if a file with the given path exists
*
* @param filePath Full path to the file
* @return True if the file exists, false if not
*/
Q_INVOKABLE static bool doesFileExist(QString filePath)
{
QFile theFile(filePath);
return theFile.exists();
}
/**
* @brief Get the size of a file, in bytes
*
* If the file doesn't exist, this function returns zero.
*
* @param filePath Full path to the file
* @return
*/
Q_INVOKABLE static int getFileSizeBytes(QString filePath)
{
if (!doesFileExist(filePath))
{
return 0;
}
QFile theFile(filePath);
return theFile.size();
}
/**
* @brief Check if a directory exists.
*
* @param directoryPath Directory path
* @return True if the directory exists
*/
Q_INVOKABLE static bool doesDirectoryExist(QString directoryPath)
{
QDir theDirectory(directoryPath);
return theDirectory.exists();
}
};
#endif // FILEIOSINGLETON_H
TEMPLATE = lib
TARGET = fileioplugin
QT += qml quick
CONFIG += qt plugin
TARGET = $$qtLibraryTarget($$TARGET)
uri = com.yourcompany.qmlcomponents
QMAKE_MOC_OPTIONS += -Muri=com.yourcompany.qmlcomponents
# Input
SOURCES += \
fileioplugin_plugin.cpp \
fileiosingleton.cpp \
FileIoSingleton.qml
HEADERS += \
fileioplugin_plugin.h \
fileiosingleton.h
DISTFILES = qmldir
qmldir.files = qmldir
#include "fileioplugin_plugin.h"
#include "fileiosingleton.h"
#include <qqml.h>
void CFileIoPlugin::registerTypes(const char *uri)
{
// @uri com.yourcompany.qmlcomponents
qmlRegisterSingletonType<CFileIoSingleton>(uri, 1, 0, "FileIoSingleton", &CFileIoSingleton::qmlInstance);
}
#ifndef FILEIOPLUGIN_PLUGIN_H
#define FILEIOPLUGIN_PLUGIN_H
#include <QQmlExtensionPlugin>
class CFileIoPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public:
void registerTypes(const char *uri);
};
#endif // FILEIOPLUGIN_PLUGIN_H
module com.yourcompany.qmlcomponents
plugin fileioplugin
classname CFileIoPlugin
typeinfo plugins.qmltypes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.