我有一个 C++ 库。该库使用发布-订阅者模式。
namespace mylib {
typedef unsigned MyLibType;
class IEvent
{
virutal ~IEvent() {}
virtual void event1() = 0;
virtual void event2(MyLibType) = 0;
};
class IMyClass
{
public:
virtual ~IMyClass() {}
// operations
};
} // mylib
// C interface
int CreateMyClass( mylib::IMyClass** class, mylib::IEvent* listener );
// implementation
class MyClass : public IMyClass { // ...
创建 MyClass 时,使用生成一些事件的线程。
现在在 Qt 上:
class QtMyApplication : public QMainWindow, public mylib::IEvent
{
Q_OBJECT
public:
explicit QtMyApplication(QWidget *parent = 0);
~QtMyApplication();
signals:
void MyLibEvent1();
void MyLibEvent2(mylib::MyLibType);
private slots:
void OnMyLibEvent1();
void OnMyLibEvent2(mylib::MyLibType);
private: // IEvent interface
void event1()
{
emit MyLibEvent1();
}
void event2(mylib::MyLibType i)
{
emit OnMyLibEvent2( i );
}
private:
Ui::QtMyApplication* ui;
mylib::IMyLib mMyLib;
};
// implementation
QtMyApplication::QtMyApplication( QWidget *parent ) :
QMainWindow(parent),
ui(new Ui::QtMyApplication)
{
ui->setupUi( this );
CreateMyLib( &mMyLib, this );
connect( this, SIGNAL(MyLibEvent1()),
this, SLOT(OnMyLibEvent1()) );
connect( this, SIGNAL(MyLibEvent2(mylib::MyLibType)),
this, SLOT(OnMyLibEvent2(mylib::MyLibType)) );
}
当我运行应用程序 event2() 在 QtMyApplication 中被调用但 OnMyLibEvent2 从未被调用,而是 event1() 被调用并且 OnMyLibEvent1 也被调用。我错了什么?参数参数是问题吗?
编辑 1
我发现连接对参数有一些限制......因为如果我改变MyLibEvent2(mylib::MyLibType)
所有MyLibEvent2(int)
工作正常......