PDA

View Full Version : Load form mdi child plugin



estanisgeyer
10th February 2008, 13:13
Hi guys,

I'm creating a plugin that contains a form. How can I load this plugin as a child window in an application mdi?

Thanks,

Marcelo E. Geyer

jpn
10th February 2008, 13:17
What did you try so far? How does the plugin create the widget?


class PluginInterface
{
public:
...
QWidget* createForm(QWidget* parent = 0) = 0;
...
};

estanisgeyer
10th February 2008, 13:35
I have no idea how this implementation. I will post here in the code. Anyway, I will try to take the hint and make us their tests here.

RecInterface.h:


#ifndef RECINTERFACE_H
#define RECINTERFACE_H

#include <QtPlugin>
#include <QWidget>

class ReceberInterface: public QWidget
{
public:
virtual ~RecInterface() {}
virtual QString getIcon() const = 0;
virtual QString typeInterface() const = 0;
virtual QString statustipInterface() const = 0;
virtual QString menuInterface() const = 0;

private:
};

Q_DECLARE_INTERFACE(RecInterface, "snet.RecInterface/1.0")

#endif



Rec.h:


#ifndef REC_H
#define REC_H

#include <QtGui>
#include "ui_Rec.h"
#include "RecInterface.h"

class Rec: public RecInterface
{
Q_OBJECT
Q_INTERFACES(RecInterface)

public:
Rec();
virtual ~Rec();

QString getIcon() const;
QString typeInterface() const;
QString statustipInterface() const;
QString menuInterface() const;

private:
Ui::Form_Rec ui;

protected:

};

#endif


Rec.cpp:



#include "Rec.h"

Rec::Rec()
{
qDebug() << "Rec is load...";
}

Rec::~Rec()
{

}

QString Rec::getIcon() const
{
QString icon = ":/imagens/contas_rec.png";
return icon;
}

QString Rec::typeInterface() const
{
QString tipo = "Account Rec...";
return tipo;
}

QString Rec::statustipInterface() const
{
QString statustip = "Account Rec...";
return statustip;
}

QString Rec::menuInterface() const
{
QString menu = "&Account";
return menu;
}

Q_EXPORT_PLUGIN2(librec, Rec)


Thanks,

Marcelo E. Geyer

jpn
10th February 2008, 13:42
First of all, the plugin interface must be a class with pure virtual functions only, thus you cannot make it inherit QWidget. Secondly, you should write a function which creates and returns a widget. This way you can control the life time of the widget properly.

For more details, see How to Create Qt Plugins - The Lower-Level API: Extending Qt Applications (http://doc.trolltech.com/4.3/plugins-howto.html#the-lower-level-api-extending-qt-applications).

estanisgeyer
10th February 2008, 15:27
Thanks for hints!
I'm resolved the problem, follow below the code:

MainWindow.cpp:



void MainWindow::slotShowPlugin()
{
QAction *act = qobject_cast<QAction *>(sender());
RecInterface *interface = qobject_cast<RecInterface *>(act->parent());

QMdiSubWindow *mdiSubWin = new QMdiSubWindow;
mdiSubWin->setWidget(interface->createWidget());
mdiSubWin->setAttribute(Qt::WA_DeleteOnClose);
mdiArea->addSubWindow(mdiSubWin);
mdiSubWin->show();
}


RecInterface.h


#include <QtPlugin>
#include <QWidget>

class RecInterface
{
public:
virtual QWidget *createWidget(QWidget *parent = 0) = 0;
virtual ~RecInterface() {};
virtual QString getIcon() const = 0;
virtual QString tipoInterface() const = 0;
virtual QString statustipInterface() const = 0;
virtual QString menuInterface() const = 0;

private:
};

Q_DECLARE_INTERFACE(RecInterface, "snet.RecInterface/1.0")

#endif



Rec.h:


#include <QWidget>
#include <QtDebug>
#include "ui_Rec.h"
#include "RecInterface.h"

class RecWidget;
class Rec: public QWidget, public RecInterface
{
Q_OBJECT
Q_INTERFACES(RecInterface)

public:
Rec(QWidget *parent = 0);
virtual ~Rec();
QWidget *createWidget(QWidget *parent = 0);
QString getIcon() const;
QString tipoInterface() const;
QString statustipInterface() const;
QString menuInterface() const;

RecWidget *recWid;

private:

protected:

};

class RecWidget: public QWidget
{
Q_OBJECT
public:
RecWidget(QWidget *parent = 0);
virtual ~RecWidget();

private:
Ui::Form_Rec ui;

protected:

};

#endif



Rec.cpp:


#include "Rec.h"

Rec::Rec(QWidget *parent)
:QWidget(parent)
{
qDebug() << "Loading rec...";
}

Rec::~Rec()
{

}

QString Rec::getIcon() const
{
QString icon = ":/imagens/nf.png";
return icon;
}
QString Rec::tipoInterface() const
{
QString tipo = "Plugin Rec...";
return tipo;
}

QString Rec::statustipInterface() const
{
QString statustip = "Account Rec...";
return statustip;
}

QString Rec::menuInterface() const
{
QString menu = "&Account";
return menu;
}

QWidget *Rec::createWidget(QWidget *parent)
{
recWid = new RecWidget(this);
return recWid;
}

Q_EXPORT_PLUGIN2(librec, Rec)


RecWidget::RecWidget(QWidget *parent)
:QWidget(parent)
{
qDebug() << "Loading form...";
ui.setupUi(this);
}

RecWidget::~RecWidget()
{

}