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
Qt Code:
  1. TEMPLATE = subdirs
  2. CONFIG += ordered
  3. SUBDIRS += ui app
To copy to clipboard, switch view to plain text mode 

app.pro
Qt Code:
  1. TARGET = test
  2. TEMPLATE = app
  3. CONFIG += debug
  4. SOURCES += main.cpp
  5. HEADERS += mainwindow.h
  6. LIBS += -LE:\documents\test\app\plugins -lui
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include <QtGui/QApplication>
  2. #include <QPluginLoader>
  3. #include <QMessageBox>
  4. #include <QDir>
  5. #include "mainwindow.h"
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9. QApplication a(argc, argv);
  10. QDir dir(a.applicationDirPath ());
  11. dir.cdUp();
  12. dir.cd("plugins");
  13. QPluginLoader loader(dir.absolutePath() + "/ui.dll");
  14. QObject *plugin = loader.instance();
  15. if(plugin) {
  16. MainWindow *win = qobject_cast<MainWindow *>(plugin);
  17. win->setVisible(true);
  18. } else {
  19. QMessageBox::information(NULL, "load error", loader.errorString());
  20. }
  21. loader.unload();
  22. return a.exec();
  23. }
To copy to clipboard, switch view to plain text mode 

mainwindow.h
Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include "ivisible.h"
  6.  
  7. namespace Ui {
  8. class MainWindow;
  9. }
  10.  
  11. class MainWindow : public QMainWindow, public IVisible {
  12. Q_OBJECT
  13. Q_INTERFACES(IVisible)
  14.  
  15. public:
  16. MainWindow(QWidget *parent = 0);
  17. ~MainWindow();
  18.  
  19. void setVisible(bool v) { if(v) this->show(); else this->hide(); }
  20. bool visible() const { return this->visible(); }
  21.  
  22. protected:
  23. void changeEvent(QEvent *e);
  24.  
  25. private:
  26. Ui::MainWindow *ui;
  27. };
  28.  
  29. #endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode 

mainwindow.cpp
Qt Code:
  1. #include <QtPlugin>
  2.  
  3. #include "mainwindow.h"
  4. #include "ui_mainwindow.h"
  5.  
  6. MainWindow::MainWindow(QWidget *parent) :
  7. QMainWindow(parent),
  8. ui(new Ui::MainWindow)
  9. {
  10. ui->setupUi(this);
  11. }
  12.  
  13. MainWindow::~MainWindow()
  14. {
  15. delete ui;
  16. }
  17.  
  18. void MainWindow::changeEvent(QEvent *e)
  19. {
  20. QMainWindow::changeEvent(e);
  21. switch (e->type()) {
  22. case QEvent::LanguageChange:
  23. ui->retranslateUi(this);
  24. break;
  25. default:
  26. break;
  27. }
  28. }
  29.  
  30. 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!