Skip to content

Instantly share code, notes, and snippets.

Created August 3, 2015 13:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/97d6b1fa78b13439ed35 to your computer and use it in GitHub Desktop.
Save anonymous/97d6b1fa78b13439ed35 to your computer and use it in GitHub Desktop.
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