我有一个简单的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中处理事件/委托?
背景信息如下:
如果COM对象实现了IDispatch接口,则该对象的属性、方法和事件将作为Qt属性、槽和信号可用。
我已经使用以下资源进行了研究:
- 处理C# COM事件的C++方法 - 如何在托管代码中放置IDispatch* - 如何在C++中触发C# COM事件?
请帮忙!
connect(&eventClass, SIGNAL(Completed()), this, SLOT(test()));
,但那似乎不正确。 具体来说,我得到的错误是:**QObject::connect: No such signal MyEventClassLibrary::Calculator::Completed()**。 - Anduril28