Hi, all! I want to put all UI components into one dll so I try to create a plugin. Then my folder tree is:
/ --- test.pro
|
ui ---- ui.pro
| | -- mainwindow.h
| | -- mainwindow.cpp
| | -- ivisible.h // this contains the interface that has only two functions: setVisible(bool) and visible()
|
app ----- app.pro
| --- main.cpp
| --- mainwindow.h // copy from ui/mainwindow.h
and the code is:
test.pro
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += ui app
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += ui app
To copy to clipboard, switch view to plain text mode
app.pro
TARGET = test
TEMPLATE = app
CONFIG += debug
SOURCES += main.cpp
HEADERS += mainwindow.h
LIBS += -LE:\documents\test\app\plugins -lui
TARGET = test
TEMPLATE = app
CONFIG += debug
SOURCES += main.cpp
HEADERS += mainwindow.h
LIBS += -LE:\documents\test\app\plugins -lui
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QtGui/QApplication>
#include <QPluginLoader>
#include <QMessageBox>
#include <QDir>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QDir dir
(a.
applicationDirPath ());
dir.cdUp();
dir.cd("plugins");
QObject *plugin
= loader.
instance();
if(plugin) {
MainWindow *win = qobject_cast<MainWindow *>(plugin);
win->setVisible(true);
} else {
QMessageBox::information(NULL,
"load error", loader.
errorString());
}
loader.unload();
return a.exec();
}
#include <QtGui/QApplication>
#include <QPluginLoader>
#include <QMessageBox>
#include <QDir>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDir dir(a.applicationDirPath ());
dir.cdUp();
dir.cd("plugins");
QPluginLoader loader(dir.absolutePath() + "/ui.dll");
QObject *plugin = loader.instance();
if(plugin) {
MainWindow *win = qobject_cast<MainWindow *>(plugin);
win->setVisible(true);
} else {
QMessageBox::information(NULL, "load error", loader.errorString());
}
loader.unload();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ivisible.h"
namespace Ui {
class MainWindow;
}
class MainWindow
: public QMainWindow,
public IVisible
{ Q_OBJECT
Q_INTERFACES(IVisible)
public:
~MainWindow();
void setVisible(bool v) { if(v) this->show(); else this->hide(); }
bool visible() const { return this->visible(); }
protected:
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ivisible.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow, public IVisible {
Q_OBJECT
Q_INTERFACES(IVisible)
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void setVisible(bool v) { if(v) this->show(); else this->hide(); }
bool visible() const { return this->visible(); }
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include <QtPlugin>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
Q_EXPORT_PLUGIN2(ui, MainWindow)
#include <QtPlugin>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
Q_EXPORT_PLUGIN2(ui, MainWindow)
To copy to clipboard, switch view to plain text mode
As you can see, main window is the code created by QtCreator and the interface IVisible has only two functions. My problem is there are errors while compiling:
undefined reference to `MainWindow::~MainWindow()'
undefined reference to `MainWindow::changeEvent(QEvent*)'
I think this is because I only add mainwindow.h to app.pro and Qt cannot find the cpp. But I should do this in order to create the main window in main() function. So how can I solve this problem? Must I create main window in application or create it in a function in the plugin and call it in main()?
Thank you!
Bookmarks