QT中如何处理C#的COM事件?
How to handle C# COM events in QT?
我有一个简单的 C# COM 可见库,如下所示:
namespace MyEventClassLibrary {
[Guid("0ab09e18-bf85-48c8-a45d-a0cebed77a5c")]
public interface ICalculator
{
int Add(int Num1, int Num2);
}
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid("9e09b634-8c1a-4926-83b2-f6f988595c85")]
public interface ICalculatorEvents
{
[DispId(1)]
void Completed(int Result);
}
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(ICalculatorEvents))]
[Guid("6cebc4db-2e8f-4e24-91a5-24ffdf97fc6a")]
public class Calculator : ICalculator
{
[ComVisible(false)]
public delegate void CompletedDelegate(int result);
public event CompletedDelegate Completed;
public int Add(int Num1, int Num2)
{
int Result = Num1 + Num2;
if (Completed != null)
Completed(Result);
return Result;
}
}}
我注册了它,然后将 .TLB 导入到 QT 中:
TYPELIBS = ..\MyEventClassLibrary\MyEventClassLibrary\bin\Debug\MyEventClassLibrary.tlb
我的 mainwindow.cpp 文件看起来像:
#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myeventclasslibrary.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MyEventClassLibrary::Calculator eventClass;
connect(&eventClass, SIGNAL(Completed()), this, SLOT(test()));
qDebug() << eventClass.Add(1,2);
}
void MainWindow::test()
{
qDebug() << "Here";
}
MainWindow::~MainWindow()
{
delete ui;
}
当我在 QT 中构建项目时,我遇到了 40 多个问题:
MyEventClassLibrary.h:144: error: C2535: 'MyEventClassLibrary::CompletedDelegate::CompletedDelegate(void)': member function already defined or declared
和
MyEventClassLibrary.h:147: error: C2065: 'iface': undeclared identifier
以上问题已通过向委托添加 [ComVisible(false)] 解决(有关更多信息,请参阅评论)
当我 运行 代码时,我得到一个错误:
QObject::connect: No such signal MyEventClassLibrary::Calculator::Completed()
我的问题是,你如何处理 COM 和 QT 中的 events/delegates?
作为背景信息,QT 文档说:
If the COM object implements the IDispatch interface, the properties, methods and events of that object become available as Qt properties, slots and signals.
我使用了以下资源和研究:
Handle C# COM events in C++
how to put IDispatch* in managed code
How to fire c# COM events in c++?
等等;请帮忙!
这个问题有一个两部分的问题,目标是在 QT 中触发一个 C# COM 可见事件:
当我最初构建 QT 应用程序时,由于委托可见,我遇到了 40 多个错误。 通过在委托声明上方添加 [ComVisible(false)] 解决了这个问题 谢谢@HansPassant。
解决此问题后,我尝试将 SLOT 连接到 COM SIGNAL,QT 说找不到 Completed 事件。 这已通过匹配函数原型解决; Completed 有一个参数 int,我没有在 SLOT 函数 中包括 int。 (笨蛋)
我原来的连接是:
connect(&eventClass, SIGNAL(Completed()), this, SLOT(test()));
应该是:
connect(&eventClass, SIGNAL(Completed(int)), this, SLOT(test(int)));
谢谢!
我有一个简单的 C# COM 可见库,如下所示:
namespace MyEventClassLibrary {
[Guid("0ab09e18-bf85-48c8-a45d-a0cebed77a5c")]
public interface ICalculator
{
int Add(int Num1, int Num2);
}
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid("9e09b634-8c1a-4926-83b2-f6f988595c85")]
public interface ICalculatorEvents
{
[DispId(1)]
void Completed(int Result);
}
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(ICalculatorEvents))]
[Guid("6cebc4db-2e8f-4e24-91a5-24ffdf97fc6a")]
public class Calculator : ICalculator
{
[ComVisible(false)]
public delegate void CompletedDelegate(int result);
public event CompletedDelegate Completed;
public int Add(int Num1, int Num2)
{
int Result = Num1 + Num2;
if (Completed != null)
Completed(Result);
return Result;
}
}}
我注册了它,然后将 .TLB 导入到 QT 中:
TYPELIBS = ..\MyEventClassLibrary\MyEventClassLibrary\bin\Debug\MyEventClassLibrary.tlb
我的 mainwindow.cpp 文件看起来像:
#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myeventclasslibrary.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MyEventClassLibrary::Calculator eventClass;
connect(&eventClass, SIGNAL(Completed()), this, SLOT(test()));
qDebug() << eventClass.Add(1,2);
}
void MainWindow::test()
{
qDebug() << "Here";
}
MainWindow::~MainWindow()
{
delete ui;
}
当我在 QT 中构建项目时,我遇到了 40 多个问题:
MyEventClassLibrary.h:144: error: C2535: 'MyEventClassLibrary::CompletedDelegate::CompletedDelegate(void)': member function already defined or declared
和
MyEventClassLibrary.h:147: error: C2065: 'iface': undeclared identifier
以上问题已通过向委托添加 [ComVisible(false)] 解决(有关更多信息,请参阅评论)
当我 运行 代码时,我得到一个错误:
QObject::connect: No such signal MyEventClassLibrary::Calculator::Completed()
我的问题是,你如何处理 COM 和 QT 中的 events/delegates?
作为背景信息,QT 文档说:
If the COM object implements the IDispatch interface, the properties, methods and events of that object become available as Qt properties, slots and signals.
我使用了以下资源和研究:
Handle C# COM events in C++
how to put IDispatch* in managed code
How to fire c# COM events in c++?
等等;请帮忙!
这个问题有一个两部分的问题,目标是在 QT 中触发一个 C# COM 可见事件:
当我最初构建 QT 应用程序时,由于委托可见,我遇到了 40 多个错误。 通过在委托声明上方添加 [ComVisible(false)] 解决了这个问题 谢谢@HansPassant。
解决此问题后,我尝试将 SLOT 连接到 COM SIGNAL,QT 说找不到 Completed 事件。 这已通过匹配函数原型解决; Completed 有一个参数 int,我没有在 SLOT 函数 中包括 int。 (笨蛋)
我原来的连接是:
connect(&eventClass, SIGNAL(Completed()), this, SLOT(test()));
应该是:
connect(&eventClass, SIGNAL(Completed(int)), this, SLOT(test(int)));
谢谢!