Define a normal unmanaged Qt event handler. From this handler, raise the managed event.
Edit: if you have previous experience with Qt, you know how to handle Qt signals in an unmanaged class. Do not try to make this class manageable. Instead, write a C ++ / CLI wrapper that contains this QObject-based class.
Edit 2. Create a C ++ / CLI Console application, call its test1 and add the following code to it:
test1.cpp:
#include "stdafx.h"
#include "ManagedClass.h"
using namespace System;
int main (array ^ args)
{
ManagedClass ^ c = gcnew ManagedClass ();
c-> Test ();
return 0;
}
ManagedClass.h:
#pragma once
class UnmanagedClass;
// Wrapper
ref class ManagedClass
{
public:
ManagedClass (void);
~ ManagedClass ();
! ManagedClass ();
void Test ();
void Callback ();
private:
UnmanagedClass * m_pUnmanagedClass;
};
ManagedClass.cpp:
#include "StdAfx.h"
#include "ManagedClass.h"
#include "UnmanagedClass.h"
ManagedClass :: ManagedClass (void)
{
m_pUnmanagedClass = new UnmanagedClass (this);
}
ManagedClass :: ~ ManagedClass ()
{
this ->! ManagedClass ();
GC :: SuppressFinalize (this);
}
ManagedClass ::! ManagedClass ()
{
if (m_pUnmanagedClass)
{
delete m_pUnmanagedClass;
m_pUnmanagedClass = NULL;
}
}
void ManagedClass :: Test ()
{
m_pUnmanagedClass-> Test ();
}
void ManagedClass :: Callback ()
{
Console :: WriteLine (L "This text is printed from managed wrapper function, called from unmanaged class.");
Console :: WriteLine (L "Here you can raise managed event for .NET client.");
Console :: WriteLine (L "Let say that UnmanagedClass is QObject-derived, and this funcstion");
Console :: WriteLine (L "is called from UnmanagedClass Qt event handler - this is what you need.");
}
UnmanagedClass.h:
#pragma once
#include
using namespace System;
ref class ManagedClass;
// Wrapped native class
class UnmanagedClass
{
public:
UnmanagedClass (ManagedClass ^ pWrapper);
~ UnmanagedClass (void);
void Test ();
private:
gcroot m_pWrapper;
};
UnmanagedClass.cpp:
#include "StdAfx.h"
#include "UnmanagedClass.h"
#include "ManagedClass.h"
UnmanagedClass :: UnmanagedClass (ManagedClass ^ pWrapper)
{
m_pWrapper = pWrapper;
}
UnmanagedClass :: ~ UnmanagedClass (void)
{
}
void UnmanagedClass :: Test ()
{
m_pWrapper-> Callback ();
}
This gives you an idea of โโhow an unmanaged class and a managed wrapper can play together. If you know Qt programming and know how to raise a .NET event from a C ++ / CLI class, this is enough to complete your task.
Alex f
source share