You can do this with a QAbstractFileEngine. Following is code:
Header:
#ifndef TEST14_H
#define TEST14_H
#include <QtGui/QMainWindow>
#include <QAbstractFileEngineHandler>
#include <QAbstractFileEngine>
#include <QDateTime>
#include <QByteArray>
#include <QtAlgorithms>
{
Q_OBJECT
public:
~MainWindow();
};
{
public:
};
{
public:
NumbatEngine(const QString& fileName);
bool caseSensitive () const { return false; }
bool close () { return true; }
FileFlags fileFlags ( FileFlags type = FileInfoAll ) const
QString fileName
( FileName
file = DefaultName
) const { return m_fileName;
} bool mkdir
( const QString & dirName,
bool createParentDirectories
) const { return false;
} uint ownerId ( FileOwner owner ) const { return -2; }
bool remove () { return false; }
bool rename ( const QString & newName
) { return false;
} bool rmdir
( const QString & dirName,
bool recurseParentDirectories
) const { return false;
} void setFileName
( const QString & file ) { m_fileName
= file;
} bool setPermissions ( uint perms ) { return false; }
bool setSize ( qint64 size ) { m_bytes->resize(size); return true; }
qint64 size () const { return m_bytes->size(); }
bool seek ( qint64 offset ) { m_filePos = offset; return true; }
bool open
( QIODevice::OpenMode mode
) { return true;
} bool isRelativePath () const { return false; }
qint64 write ( const char * data, qint64 len );
qint64 read ( char * data, qint64 maxlen );
private:
qint64 m_filePos;
};
#endif // TEST14_H
#ifndef TEST14_H
#define TEST14_H
#include <QtGui/QMainWindow>
#include <QAbstractFileEngineHandler>
#include <QAbstractFileEngine>
#include <QDateTime>
#include <QByteArray>
#include <QtAlgorithms>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
class NumbatEngineHandler : public QAbstractFileEngineHandler
{
public:
QAbstractFileEngine *create(const QString &fileName) const;
};
class NumbatEngine : public QAbstractFileEngine
{
public:
NumbatEngine(const QString& fileName);
bool caseSensitive () const { return false; }
bool close () { return true; }
QStringList entryList ( QDir::Filters filters, const QStringList & filterNames ) const { return QStringList(); }
FileFlags fileFlags ( FileFlags type = FileInfoAll ) const
{ return QAbstractFileEngine::ReadOtherPerm | QAbstractFileEngine::ExeOtherPerm | QAbstractFileEngine::FileType | QAbstractFileEngine::WriteOtherPerm; }
QString fileName ( FileName file = DefaultName ) const { return m_fileName; }
QDateTime fileTime ( FileTime time ) const { return QDateTime(); }
bool mkdir ( const QString & dirName, bool createParentDirectories ) const { return false; }
QString owner ( FileOwner owner ) const { return QString(); }
uint ownerId ( FileOwner owner ) const { return -2; }
bool remove () { return false; }
bool rename ( const QString & newName ) { return false; }
bool rmdir ( const QString & dirName, bool recurseParentDirectories ) const { return false; }
void setFileName ( const QString & file ) { m_fileName = file; }
bool setPermissions ( uint perms ) { return false; }
bool setSize ( qint64 size ) { m_bytes->resize(size); return true; }
qint64 size () const { return m_bytes->size(); }
bool seek ( qint64 offset ) { m_filePos = offset; return true; }
bool open ( QIODevice::OpenMode mode ) { return true; }
bool isRelativePath () const { return false; }
qint64 write ( const char * data, qint64 len );
qint64 read ( char * data, qint64 maxlen );
private:
QByteArray* m_bytes;
QString m_fileName;
qint64 m_filePos;
};
#endif // TEST14_H
To copy to clipboard, switch view to plain text mode
Source:
#include "Test14.h"
#include <QMap>
#include <QPixmap>
#include <QLabel>
#include <QApplication>
#include <QDebug>
{
// We handle all files which start with ???
if (fileName.toLower().startsWith("???"))
return new NumbatEngine(fileName);
else
return 0;
}
NumbatEngine::NumbatEngine(const QString& fileName)
{
if (map.contains(m_fileName))
{
m_bytes = map.value(m_fileName);
}
else
{
map[m_fileName] = m_bytes;
}
}
qint64 NumbatEngine::write ( const char * data, qint64 len )
{
if (m_filePos + len > m_bytes->size()) m_bytes->resize(m_filePos + len);
qCopy(data, data + len, m_bytes->data() + m_filePos);
m_filePos += len;
return len;
}
qint64 NumbatEngine::read ( char * data, qint64 maxlen )
{
qint64 readBytes;
if (m_filePos + maxlen > m_bytes->size())
readBytes = m_bytes->size() - m_filePos;
else
readBytes = maxlen;
qCopy(m_bytes->constData() + m_filePos,
m_bytes->constData() + m_filePos + readBytes, data);
m_filePos += readBytes;
return readBytes;
}
/* Demonstration. */
MainWindow
::MainWindow(QWidget *parent
){
pix.save("???Hello.jpg");
lbl->setStyleSheet("QLabel {background-image:url(\"???Hello.jpg\");}");
setCentralWidget(lbl);
}
MainWindow::~MainWindow()
{
}
int main(int argc, char **argv)
{
NumbatEngineHandler engine;
MainWindow window;
window.show();
return app.exec();
}
#include "Test14.h"
#include <QMap>
#include <QPixmap>
#include <QLabel>
#include <QApplication>
#include <QDebug>
QAbstractFileEngine * NumbatEngineHandler::create(const QString &fileName) const
{
// We handle all files which start with ???
if (fileName.toLower().startsWith("???"))
return new NumbatEngine(fileName);
else
return 0;
}
NumbatEngine::NumbatEngine(const QString& fileName)
: QAbstractFileEngine(), m_fileName(fileName), m_filePos(0)
{
static QMap<QString, QByteArray*> map;
if (map.contains(m_fileName))
{
m_bytes = map.value(m_fileName);
}
else
{
m_bytes = new QByteArray;
map[m_fileName] = m_bytes;
}
}
qint64 NumbatEngine::write ( const char * data, qint64 len )
{
if (m_filePos + len > m_bytes->size()) m_bytes->resize(m_filePos + len);
qCopy(data, data + len, m_bytes->data() + m_filePos);
m_filePos += len;
return len;
}
qint64 NumbatEngine::read ( char * data, qint64 maxlen )
{
qint64 readBytes;
if (m_filePos + maxlen > m_bytes->size())
readBytes = m_bytes->size() - m_filePos;
else
readBytes = maxlen;
qCopy(m_bytes->constData() + m_filePos,
m_bytes->constData() + m_filePos + readBytes, data);
m_filePos += readBytes;
return readBytes;
}
/* Demonstration. */
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QPixmap pix("image.jpeg");
pix.save("???Hello.jpg");
QLabel * lbl = new QLabel;
lbl->setStyleSheet("QLabel {background-image:url(\"???Hello.jpg\");}");
setCentralWidget(lbl);
}
MainWindow::~MainWindow()
{
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
NumbatEngineHandler engine;
MainWindow window;
window.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks