
Originally Posted by
wysota
Is this .pro file for the plugin or for the application? What is the "Directory" class? Where is it defined? If it is defined in a plugin then you can't access it directly from the application. If you want to access it from the application, you need to link your application against the library (which stops being a plugin at this moment). If you do that, remember to export all the symbols from the library that you want to be accessible in code using that library.
Hello wysota thanks for your prompt reply.
1.- THe pro file that i posted is for the applicationn
2.- The pro file for my plugin is:
TEMPLATE = lib
CONFIG += qt plugin
QT += declarative
DESTDIR += ../plugins
OBJECTS_DIR = tmp
MOC_DIR = tmp
TARGET = FileDialog
HEADERS += directory.h \
file.h \
dialogPlugin.h
SOURCES += directory.cpp \
file.cpp \
dialogPlugin.cpp
TEMPLATE = lib
CONFIG += qt plugin
QT += declarative
DESTDIR += ../plugins
OBJECTS_DIR = tmp
MOC_DIR = tmp
TARGET = FileDialog
HEADERS += directory.h \
file.h \
dialogPlugin.h
SOURCES += directory.cpp \
file.cpp \
dialogPlugin.cpp
To copy to clipboard, switch view to plain text mode
I'm not sure wether I'm developing a qt plugin or a shared library. But I'm sure I'm trying to implement this modulehttp://qt-project.org/doc/qt-5.0/qtd...tartedqml.html
3.- Directory is a class defined in the plugin This is the code for the headers of the plugin:
the source code for dialogPlugin.h is:
#ifndef DIALOGPLUGIN_H
#define DIALOGPLUGIN_H
#include <QtDeclarative/QDeclarativeExtensionPlugin>
class DialogPlugin : public QDeclarativeExtensionPlugin
{
Q_OBJECT
public:
//registerTypes is inherited from QDeclarativeExtensionPlugin
void registerTypes(const char *uri);
};
#ifndef DIALOGPLUGIN_H
#define DIALOGPLUGIN_H
#include <QtDeclarative/QDeclarativeExtensionPlugin>
class DialogPlugin : public QDeclarativeExtensionPlugin
{
Q_OBJECT
public:
//registerTypes is inherited from QDeclarativeExtensionPlugin
void registerTypes(const char *uri);
};
To copy to clipboard, switch view to plain text mode
Source code for DialogPlugin.cpp (This is the only class that exports something)
#include "dialogPlugin.h"
#include "directory.h"
#include "file.h"
#include <QtDeclarative/qdeclarative.h>
void DialogPlugin::registerTypes(const char *uri)
{
//register the class Directory into QML as a "Directory" element version 1.0
qmlRegisterType<Directory>(uri, 1, 0, "Directory");
qmlRegisterType<File>(uri,1,0,"File");
}
//FileDialog is the plugin name (same as the TARGET in the project file) and DialogPlugin is the plugin classs
Q_EXPORT_PLUGIN2(FileDialog, DialogPlugin);
#include "dialogPlugin.h"
#include "directory.h"
#include "file.h"
#include <QtDeclarative/qdeclarative.h>
void DialogPlugin::registerTypes(const char *uri)
{
//register the class Directory into QML as a "Directory" element version 1.0
qmlRegisterType<Directory>(uri, 1, 0, "Directory");
qmlRegisterType<File>(uri,1,0,"File");
}
//FileDialog is the plugin name (same as the TARGET in the project file) and DialogPlugin is the plugin classs
Q_EXPORT_PLUGIN2(FileDialog, DialogPlugin);
To copy to clipboard, switch view to plain text mode
Source code in Directory.h, this module create some variables accessible form QML via Q_PROPERTY
#ifndef DIRECTORY_H
#define DIRECTORY_H
#include "file.h"
#include <QDir>
#include <QStringList>
#include <QTextStream>
#include <QDeclarativeListProperty>
#include <QObject>
Q_OBJECT
//number of files in the directory
Q_PROPERTY(int filesCount READ filesCount)
//list property containing file names as QString
Q_PROPERTY(QDeclarativeListProperty<File> files READ files CONSTANT )
//file name of the text file to read/write
Q_PROPERTY(QString filename READ filename WRITE setFilename NOTIFY filenameChanged
)
//text content of the file
Q_PROPERTY(QString fileContent READ fileContent WRITE setFileContent NOTIFY fileContentChanged
)
public:
//properties' read functions
int filesCount() const;
QDeclarativeListProperty<File> files();
//properties' write functions
void setFilename
(const QString &str
);
void setFileContent
(const QString &str
);
//accessible from QML
Q_INVOKABLE void saveFile();
Q_INVOKABLE void loadFile();
signals:
void directoryChanged();
void filenameChanged();
void fileContentChanged();
private:
File currentFile;
//contains the file data in QString format
//Registered to QML in a plugin. Accessible from QML as a property of Directory
QList<File *> m_fileList;
//refresh content of the directory
void refresh();
};
#ifndef DIRECTORY_H
#define DIRECTORY_H
#include "file.h"
#include <QDir>
#include <QStringList>
#include <QTextStream>
#include <QDeclarativeListProperty>
#include <QObject>
class Directory : public QObject {
Q_OBJECT
//number of files in the directory
Q_PROPERTY(int filesCount READ filesCount)
//list property containing file names as QString
Q_PROPERTY(QDeclarativeListProperty<File> files READ files CONSTANT )
//file name of the text file to read/write
Q_PROPERTY(QString filename READ filename WRITE setFilename NOTIFY filenameChanged)
//text content of the file
Q_PROPERTY(QString fileContent READ fileContent WRITE setFileContent NOTIFY fileContentChanged)
public:
Directory(QObject *parent = 0);
//properties' read functions
int filesCount() const;
QString filename() const;
QString fileContent() const;
QDeclarativeListProperty<File> files();
//properties' write functions
void setFilename(const QString &str);
void setFileContent(const QString &str);
//accessible from QML
Q_INVOKABLE void saveFile();
Q_INVOKABLE void loadFile();
signals:
void directoryChanged();
void filenameChanged();
void fileContentChanged();
private:
QDir m_dir;
QStringList m_dirFiles;
File currentFile;
QString m_saveDir;
QStringList m_filterList;
//contains the file data in QString format
QString m_fileContent;
//Registered to QML in a plugin. Accessible from QML as a property of Directory
QList<File *> m_fileList;
//refresh content of the directory
void refresh();
};
To copy to clipboard, switch view to plain text mode
Source code for directory.cpp, only normal C++ source code without exports.
#include "directory.h"
#include <QDebug>
/*
Directory constructor
Initialize the saves directory and creates the file list
*/
{
m_dir.
cd( QDir::currentPath() );
//go to the saved directory. if not found, create save directory
m_saveDir = "saves";
if ( m_dir.cd(m_saveDir) == 0 ) {
m_dir.mkdir(m_saveDir);
m_dir.cd(m_saveDir);
}
m_filterList << "*.txt";
refresh();
}
/*
Directory::filesNumber
Return the number of Files
*/
int Directory::filesCount() const
{
return m_fileList.size();
}
/*
Function called to append data onto list property
*/
void appendFiles(QDeclarativeListProperty<File> * property, File * file)
{
Q_UNUSED(property);
Q_UNUSED(file);
//Do nothing. can't add to a directory using this method
}
/*
Function called to retrieve file in the list using an index
*/
File* fileAt(QDeclarativeListProperty<File> * property, int index)
{
return static_cast< QList<File *> *>(property->data)->at(index);
}
/*
Returns the number of files in the list
*/
int filesSize(QDeclarativeListProperty<File> * property)
{
return static_cast< QList<File *> *>(property->data)->size();
}
/*
Function called to empty the list property contents
*/
void clearFilesPtr(QDeclarativeListProperty<File> *property)
{
return static_cast< QList<File *> *>(property->data)->clear();
}
/*
Returns the list of files as a QDeclarativeListProperty.
*/
QDeclarativeListProperty<File> Directory::files()
{
refresh();
return QDeclarativeListProperty<File>( this, &m_fileList, &appendFiles, &filesSize, &fileAt, &clearFilesPtr );
}
/*
Return the name of the currently selected file
*/
QString Directory
::filename() const {
return currentFile.name();
}
/*
Return the file's content as a string.
*/
QString Directory
::fileContent() const {
return m_fileContent;
}
/*
Set the file name of the current file
*/
void Directory
::setFilename(const QString &str
) {
if( str != currentFile.name() ) {
currentFile.setName(str);
emit filenameChanged();
}
}
/*
Set the content of the file as a string
*/
void Directory
::setFileContent(const QString &str
) {
if(str != m_fileContent){
m_fileContent = str;
emit fileContentChanged();
}
}
/*
Called from QML to save the file using the filename and file content.
Saving makes sure that the file has a .txt extension.
*/
void Directory::saveFile()
{
if(currentFile.name().size() == 0){
qWarning()<< "Empty filename. no save";
return;
}
QString extendedName
= currentFile.
name();
if(!currentFile.name().endsWith(".txt")){
extendedName.append(".txt");
}
QFile file( m_dir.
filePath(extendedName
) );
if ( file.
open(QFile::WriteOnly |
QFile::Truncate) ) { outStream << m_fileContent;
}
file.close();
refresh();
emit directoryChanged();
}
/*
Load the contents of a file.
Only loads files with a .txt extension
*/
void Directory::loadFile()
{
m_fileContent.clear();
QString extendedName
= currentFile.
name();
if( !currentFile.name().endsWith(".txt") ) {
extendedName.append(".txt");
}
QFile file( m_dir.
filePath(extendedName
) );
if ( file.
open(QFile::ReadOnly ) ) {
do {
line = inStream.read(75);
m_fileContent.append(line);
} while ( !line.isNull() ) ;
}
file.close();
}
/*
Reloads the content of the files list. This is to ensure that the newly
created files are added onto the list.
*/
void Directory::refresh()
{
m_dirFiles
= m_dir.
entryList(m_filterList,
QDir::Files,
QDir::Name);
m_fileList.clear();
File * file;
for(int i = 0; i < m_dirFiles.size() ; i ++) {
file = new File();
if( m_dirFiles.at(i).endsWith(".txt") ) {
file->setName( name.remove(".txt",Qt::CaseSensitive) );
}
else {
file->setName(m_dirFiles.at(i));
}
m_fileList.append(file);
}
}
#include "directory.h"
#include <QDebug>
/*
Directory constructor
Initialize the saves directory and creates the file list
*/
Directory::Directory(QObject *parent) : QObject(parent)
{
m_dir.cd( QDir::currentPath() );
//go to the saved directory. if not found, create save directory
m_saveDir = "saves";
if ( m_dir.cd(m_saveDir) == 0 ) {
m_dir.mkdir(m_saveDir);
m_dir.cd(m_saveDir);
}
m_filterList << "*.txt";
refresh();
}
/*
Directory::filesNumber
Return the number of Files
*/
int Directory::filesCount() const
{
return m_fileList.size();
}
/*
Function called to append data onto list property
*/
void appendFiles(QDeclarativeListProperty<File> * property, File * file)
{
Q_UNUSED(property);
Q_UNUSED(file);
//Do nothing. can't add to a directory using this method
}
/*
Function called to retrieve file in the list using an index
*/
File* fileAt(QDeclarativeListProperty<File> * property, int index)
{
return static_cast< QList<File *> *>(property->data)->at(index);
}
/*
Returns the number of files in the list
*/
int filesSize(QDeclarativeListProperty<File> * property)
{
return static_cast< QList<File *> *>(property->data)->size();
}
/*
Function called to empty the list property contents
*/
void clearFilesPtr(QDeclarativeListProperty<File> *property)
{
return static_cast< QList<File *> *>(property->data)->clear();
}
/*
Returns the list of files as a QDeclarativeListProperty.
*/
QDeclarativeListProperty<File> Directory::files()
{
refresh();
return QDeclarativeListProperty<File>( this, &m_fileList, &appendFiles, &filesSize, &fileAt, &clearFilesPtr );
}
/*
Return the name of the currently selected file
*/
QString Directory::filename() const
{
return currentFile.name();
}
/*
Return the file's content as a string.
*/
QString Directory::fileContent() const
{
return m_fileContent;
}
/*
Set the file name of the current file
*/
void Directory::setFilename(const QString &str)
{
if( str != currentFile.name() ) {
currentFile.setName(str);
emit filenameChanged();
}
}
/*
Set the content of the file as a string
*/
void Directory::setFileContent(const QString &str)
{
if(str != m_fileContent){
m_fileContent = str;
emit fileContentChanged();
}
}
/*
Called from QML to save the file using the filename and file content.
Saving makes sure that the file has a .txt extension.
*/
void Directory::saveFile()
{
if(currentFile.name().size() == 0){
qWarning()<< "Empty filename. no save";
return;
}
QString extendedName = currentFile.name();
if(!currentFile.name().endsWith(".txt")){
extendedName.append(".txt");
}
QFile file( m_dir.filePath(extendedName) );
if ( file.open(QFile::WriteOnly | QFile::Truncate) ) {
QTextStream outStream(&file);
outStream << m_fileContent;
}
file.close();
refresh();
emit directoryChanged();
}
/*
Load the contents of a file.
Only loads files with a .txt extension
*/
void Directory::loadFile()
{
m_fileContent.clear();
QString extendedName = currentFile.name();
if( !currentFile.name().endsWith(".txt") ) {
extendedName.append(".txt");
}
QFile file( m_dir.filePath(extendedName) );
if ( file.open(QFile::ReadOnly ) ) {
QTextStream inStream(&file);
QString line;
do {
line = inStream.read(75);
m_fileContent.append(line);
} while ( !line.isNull() ) ;
}
file.close();
}
/*
Reloads the content of the files list. This is to ensure that the newly
created files are added onto the list.
*/
void Directory::refresh()
{
m_dirFiles = m_dir.entryList(m_filterList,QDir::Files,QDir::Name);
m_fileList.clear();
File * file;
for(int i = 0; i < m_dirFiles.size() ; i ++) {
file = new File();
if( m_dirFiles.at(i).endsWith(".txt") ) {
QString name = m_dirFiles.at(i);
file->setName( name.remove(".txt",Qt::CaseSensitive) );
}
else {
file->setName(m_dirFiles.at(i));
}
m_fileList.append(file);
}
}
To copy to clipboard, switch view to plain text mode
source code in file.h
#include <QString>
#include <QObject>
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged
)
public:
signals:
void nameChanged();
private:
};
#include <QString>
#include <QObject>
class File : public QObject{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
public:
File(QObject *parent = 0);
QString name() const;
void setName(const QString &str);
signals:
void nameChanged();
private:
QString m_name;
};
To copy to clipboard, switch view to plain text mode
source code for file.cpp
#include "file.h"
{
m_name = "";
}
return m_name;
}
void File::setName(const QString &str
){ if(str != m_name){
m_name = str;
emit nameChanged();
}
}
#include "file.h"
File::File(QObject *parent) : QObject(parent)
{
m_name = "";
}
QString File::name() const{
return m_name;
}
void File::setName(const QString &str){
if(str != m_name){
m_name = str;
emit nameChanged();
}
}
To copy to clipboard, switch view to plain text mode
As you could see I'm trying to expand QML functionality through plugins as the examples "getting started", previously quoted proposes.
Thanks for your help, I appreciate it a lot.
Ulises Vega
Bookmarks