【发布时间】:2018-04-10 18:14:13
【问题描述】:
首先让我快速介绍一下自己。 我叫 Jonathan,是一名来自比利时的视频游戏技术美术师和开发人员。
我主要使用 C# 或 Max Script、Python 或 Mel 等其他脚本语言工作,并开始使用 C++ 编写代码。我已经在 Visual Studio 中用 WinForm 和 WPF 做了一些小软件。
StackOverflow 曾经/并且将永远是我难以置信的资源。
我之所以注册是因为我在 C++/Qt 学习方面取得了进一步进展,但我现在遇到了 Qt 设计和代码问题。
我过去曾在 WinForm 应用程序中使用 MVP 模式,并尝试在 Qt 中做同样的事情。所以我调查并在将实现该接口的类中找到了带有Q_DECLARE_INTERFACE(MyInterfaceClass, "interfaceNameString") 和QT_INTERFACES 的接口。
但我有一个问题,将信号从接口连接到插槽来自我的演示者。
错误:没有匹配函数调用“Presenter::connect(QObject*&, void (IView_Creator::)(), Presenter, void (Presenter::*)())” QObject::connect(object,&IView_Creator::CreatorTest, this, &Presenter::Create);
错误:“struct std::enable_if”中没有名为“type”的类型
界面:(iview_creator.h)
#ifndef IVIEW_CREATOR_H
#define IVIEW_CREATOR_H
#include <QtPlugin>
class IView_Creator
{
public:
virtual ~IView_Creator(){}
virtual void WriteSomething() = 0;
signals:
virtual void CreatorTest() = 0;
};
Q_DECLARE_INTERFACE(IView_Creator, "interface")
#endif // IVIEW_CREATOR_H
主类:(mainWindow.h)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "iview_creator.h"
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow ,public IView_Creator
{
Q_OBJECT
Q_INTERFACES(IView_Creator)
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
// IView_Creator interface
signals:
void CreatorTest();
};
#endif // MAINWINDOW_H
演示者类:(presenter_creator.h)
#ifndef PRESENTER_H
#define PRESENTER_H
#include <QObject>
#include "mainwindow.h"
class Presenter : private QObject
{
Q_OBJECT
public:
Presenter(const MainWindow* mw);
private:
void Initialize(IView_Creator* mw);
private slots:
void Create();
};
#endif // PRESENTER_H
presenter的实现:
#include "presenter_creator.h"
Presenter::Presenter(const MainWindow *mw)
{
IView_Creator *i = qobject_cast<IView_Creator*>(mw);
if(i != NULL)
Initialize(i);
}
void Presenter::Initialize(IView_Creator *mw)
{
auto object = dynamic_cast<QObject*>(mw);
Q_ASSERT(object);
QObject::connect(object, SIGNAL(CreatorTest()), this, SLOT(Create()));
//QObject::connect(object,QOverload<QObject*>::of(&IView_Creator::CreatorTest), this, &Presenter::Create);
QObject::connect(object,&IView_Creator::CreatorTest, this, &Presenter::Create);
mw->WriteSomething();
}
void Presenter::Create()
{
printf("Create");
}
主类:
#include "mainwindow.h"
#include "presenter_creator.h"
#include <QApplication>
static Presenter* pt = NULL;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
MainWindow *mw = &w;
pt = new Presenter(mw);
w.show();
return a.exec();
}
当我尝试使用连接函数的新合成系统时出现问题。我似乎使用旧的 SIGNAL SLOT 字符串系统。
我已经尝试了我在网上找到的所有东西,但没有运气。 也许有更多 C++ 和 Qt 知识的人可以知道如何解决这个问题。
【问题讨论】:
-
不知道,为什么有些人投票赞成关闭。好问题。您应该了解
QObject::connect需要QObject实例。一种解决方案;您需要在QObject的真实实例上保留一个指针。第二:在connect中使用dynamic_cast。