首先让我快速介绍一下自己。 我叫 Jonathan,是一名来自比利时的视频游戏技术美术师和开发人员。
我主要使用 C# 或其他脚本语言(如 Max Script、Python 或 Mel),然后我开始使用 C++ 编写代码。我已经在 Visual Studio 中用 WinForm 和 WPF 做了一些小软件。
对我来说,StackOverflow 曾经是/并将永远是一个不可思议的资源。
我注册是因为我在 C++/Qt 学习中更进一步,现在我被 Qt 设计和代码问题困住了。
我过去对 WinForm 应用程序使用 MVP 模式,并尝试用 Qt 做同样的事情。因此,我调查并在将实现该接口(interface)的类中找到了带有 Q_DECLARE_INTERFACE(MyInterfaceClass, "interfaceNameString")
和 QT_INTERFACES
的接口(interface)。
但是我在将信号从接口(interface)连接到插槽时遇到了问题。
error: no matching function for call to 'Presenter::connect(QObject*&, void (IView_Creator::)(), Presenter, void (Presenter::*)())' QObject::connect(object,&IView_Creator::CreatorTest, this, &Presenter::Create);
error: no type named 'type' in 'struct std::enable_if'
界面:(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();
}
当我尝试使用 connect 函数的新 synthax 系统时出现问题。我似乎使用旧的 SIGNAL SLOT 字符串系统。
我已经尝试了在网上找到的所有方法,但没有成功。 也许具有更多 C++ 和 Qt 知识的人可以知道如何解决这个问题。
最佳答案
自 QObject
以来,这无法使用新的信号槽语法不继承自 IView_Creator
.
新旧语法的根本区别在于旧语法QObject::connect
在运行时检查连接的信号和槽是否实际存在,而这种检查是在编译时使用新语法执行的。
然而,在你施放了mw
之后至 QObject*
object
的信息实际上也是 IView_Creator
的一个实例因执行 QObject::connect
而丢失.它只知道 &IView_Creator::CreatorTest
需要 IView_Creator
子类型的对象而且不是每个QObject
(这就是它对 object
的了解)也是一个 IView_Creator
,所以不能保证这个连接总能被创建。因此编译失败。
关于c++ - 使用带有接口(interface)和新语法的 Qt 连接时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47003852/