开发者

MVC in qt and problem with interfaces

I want to create view and pass it into to controller via constructor.

So, I've created interface:

#include <QString>

class IMainView
{
public:
    virtual ~IMainView() {}
    virtual void setWindowTitle1(QString &title) = 0;
};

Q_DECLARE_INTERFACE(IMainView, "IMainView/1.0");

Then I created view:

class MainWindow : public QMainWindow, IMainView
{
    Q_OBJECT
    Q_INTERFACES(IMainView)
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void setWindowTitle1(QString &title);

private:
    Ui::MainWindow *ui;
};

MainWindow from IMainView. Now I have to pass instance of IMainView to controller, because each controller register view:

#include "maincontroller.h"


MainController::MainController(IMainView *v)
{
    QString title = "my application";
    v->setWindowTitle1(title);
}

MainController::MainController()
{
}

And I got error: e:\pm\pm\mainapplication\IMainView.h:13: error: C2259: 'IMainView' : cannot instantiate abstract class due to following members: 'void IMainView::setWindowTitle1(QString &)' : is abstract

In C# it works, but in QT not.

Thanks


Sorry, wrong question. The problem is with interface, which is abstract class (iMainView.h):

#include <QString>

class IMainView
{
public:
    virtual ~IMainView() {}
    virtual void setWindowTitle1(QString &title) = 0;
};

Q_DECLARE_INTERFACE(IMainView, "IMainView/1.0");

and this class have to be attached to mainwindow header file (mainwindow.h):

#ifndef MAINCONTROLLER_H
#define MAINCONTROLLER_H

#include "IMainView.h"  //here is error


class MainController
{
public:
    //MainController(IMainView *v);
    MainController();


};

#endif // MAINCONTROLLER_H

and I got开发者_StackOverflow error:

e:\pm\pm\mainapplication\IMainView.h:13: error: C2259: 'IMainView' : cannot instantiate abstract class due to following members: 'void IMainView::setWindowTitle1(QString &)' : is abstract

Thanks


You need to implement setWindowTitle1 in class MainWindow, which you probably missed. That's plain C++ and not special to Qt.


Be sure to

> #include <QObject>

Somewhere above your interface declaration. QObject.h contains the definition of Q_DECLARE_INTERFACE and its dependencies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜