C++ 使用Qt connect with interface和新synthax时出错
首先让我快速自我介绍一下。 我叫乔纳森,来自比利时,是一名视频游戏技术艺术家和开发者 <>我主要使用C或其他脚本语言,如Max Script、Python或Mel,我开始用C++编写代码。我已经用WinForm和WPF在Visual Studio中开发了一些小软件 StackOverflow对我来说曾经是/并且将永远是一个不可思议的资源 我注册是因为我进一步学习了C++/Qt,现在我陷入了Qt设计和代码问题 我过去在WinForm应用程序中使用MVP模式,并尝试在Qt上使用相同的模式。因此,我调查并在将实现接口的类中找到了带有C++ 使用Qt connect with interface和新synthax时出错,c++,qt,interface,mvp,C++,Qt,Interface,Mvp,首先让我快速自我介绍一下。 我叫乔纳森,来自比利时,是一名视频游戏技术艺术家和开发者 我主要使用C或其他脚本语言,如Max Script、Python或Mel,我开始用C++编写代码。我已经用WinForm和WPF在Visual Studio中开发了一些小软件 StackOverflow对我来说曾经是/并且将永远是一个不可思议的资源 我注册是因为我进一步学习了C++/Qt,现在我陷入了Qt设计和代码问题 我过去在WinForm应用程序中使用MVP模式,并尝试在Qt上使用相同的模式。因此,我调查并
Q_DECLARE\u接口(MyInterfaceClass,“interfaceNameString”)
和QT_接口的接口
但是我有一个问题,无法将接口的信号连接到演示者中的插槽
错误:调用“Presenter::connect(QObject*&,void(IView_创建者::)(),Presenter,void(Presenter::*)()”时没有匹配的函数
QObject::connect(对象,&IView_Creator::CreatorTest,this,&Presenter::Create)
错误:“struct std::enable_if”中没有名为“type”的类型
接口:(iview_creator.h)
\ifndef IVIEW\u创建者\u H
#定义IVIEW_CREATOR_H
#包括
类IView_创建者
{
公众:
虚拟~IView_Creator(){}
虚拟void writeMething()=0;
信号:
虚拟void CreatorTest()=0;
};
Q_DECLARE_接口(IView_创建者,“接口”)
#endif//IVIEW\u CREATOR\u H
主类:(mainWindow.h)
\ifndef主窗口
#定义主窗口
#包括
#包括“iview_creator.h”
名称空间用户界面
{
类主窗口;
}
类主窗口:公共QMainWindow、公共IView\u创建者
{
Q_对象
Q_接口(IView_创建者)
公众:
显式主窗口(QWidget*parent=0);
~main窗口();
私人:
Ui::MainWindow*Ui;
//IView_创建者界面
信号:
void CreatorTest();
};
#endif//main窗口
presenter类:(presenter_creator.h)
\ifndef演示者\u H
#定义演示者
#包括
#包括“mainwindow.h”
课堂演示者:私人QObject
{
Q_对象
公众:
演示者(常数主窗口*mw);
私人:
无效初始化(IView_Creator*mw);
专用插槽:
void Create();
};
#endif//PRESENTER\u H
演示者的实现:
#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");
}
#包括“presenter_creator.h”
演示者::演示者(常量主窗口*mw)
{
IView_Creator*i=QoObject_cast(mw);
如果(i!=NULL)
初始化(i);
}
void Presenter::初始化(IView_Creator*mw)
{
自动对象=动态投影(mw);
Q_断言(对象);
连接(对象,信号(createrTest()),这个,插槽(Create());
//QObject::connect(对象、QOverload::of(&IView_Creator::CreatorTest)、this和Presenter::Create);
QObject::connect(对象,&IView_Creator::CreatorTest,this,&Presenter::Create);
mw->writemething();
}
void Presenter::Create()
{
printf(“创建”);
}
主要类别:
#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();
}
#包括“mainwindow.h”
#包括“presenter\u creator.h”
#包括
静态演示者*pt=NULL;
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
主窗口w;
主窗口*mw=&w;
pt=新的演示者(mw);
w、 show();
返回a.exec();
}
当我尝试使用connect函数的新synthax系统时,问题出现了。我似乎在使用旧的信号槽串系统
我已经尝试了我在网上找到的一切,但没有运气。
也许有更多C++和QT知识的人知道如何解决这个问题。 < P>这不能适用于新的信号时隙语法,因为<代码> QObjult<代码>没有继承<代码> IVIEWIORIGRION//COD>
新旧语法之间的根本区别在于,使用旧语法QObject::connect
在运行时检查连接的信号和插槽是否实际存在,而使用新语法在编译时执行此检查
但是,在您将mw
转换为QObject*
之后,object
实际上也是IView\u Creator
实例的信息在QObject::connect
的实现中丢失。它只知道,&IView\u Creator::CreatorTest
需要一个属于IView\u Creator
子类型的对象,并且不是每个QObject
(这就是它所知道的对象
)也是一个IView\u Creator
,所以它不能保证总是可以创建这个连接。因此它无法编译。这不能与新的信号槽语法一起工作,因为QObject
不是从IView\u Creator
继承的
新旧语法之间的根本区别在于,使用旧语法QObject::connect
在运行时检查连接的信号和插槽是否实际存在,而使用新语法在编译时执行此检查
但是,在您将mw
转换为QObject*
之后,object
实际上也是IView\u Creator
实例的信息在QObject::connect
的实现中丢失。它只知道,&IView\u Creator::CreatorTest
需要一个属于IView\u Creator
子类型的对象,并且不是每个QObject
(这就是它所知道的对象
)也是一个IView\u Creator
,所以它不能保证总是可以创建这个连接。因此它无法编译。不知道,为什么有些人投票支持关闭。好问题。您应该了解,QObject::connect
需要一个QObject
实例。一种解决方案;您需要在QObject
的实际实例上保留指针。第二:使用动态_
#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
#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();
}