本文介绍了如何在托管 C++ (C++/CLI) 中将 Qt 信号映射到事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 .NET (C++/CLI) 编写一个包装器,以便能够在 .NET 中使用一些本机 C++ Qt 代码.如何将 Qt 信号映射到托管的 .NET 事件中,以便当我的 Qt 代码触发信号时,我可以将其转发给 .NET 代码.

我的托管类定义了事件,但如果我尝试使用普通的 QObject::connect 方法连接到信号,它需要一个 QObject * 接收器......我想我必须做一些魔法编组排序?

解决方案

定义正常的非托管 Qt 事件处理程序.从此处理程序中引发托管事件.

如果你以前有 Qt 经验,你知道如何在非托管类中处理 Qt 信号.不要试图管理这个类.代替这个,编写包含这个 QObject 派生类的 C++/CLI 包装器.

编辑 2. 创建 C++/CLI 控制台应用程序,将其命名为 test1,并将以下代码添加到其中:

test1.cpp:

#include stdafx.h"#include "ManagedClass.h"使用命名空间系统;int main(数组^args){ManagedClass^ c = gcnew ManagedClass();c->测试();返回0;}

ManagedClass.h:

#pragma 一次类非托管类;//包装器参考类 ManagedClass{上市:托管类(无效);〜托管类();!ManagedClass();无效测试();无效回调();私人的: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(){如果(m_pUnmanagedClass){删除 m_pUnmanagedClass;m_pUnmanagedClass = NULL;}}无效 ManagedClass::Test(){m_pUnmanagedClass->Test();}无效 ManagedClass::Callback(){Console::WriteLine(L"此文本是从托管包装函数打印的,从非托管类调用.");Console::WriteLine(L"在这里您可以为 .NET 客户端引发托管事件.");Console::WriteLine(L"假设 UnmanagedClass 是 QObject 派生的,并且这个函数");Console::WriteLine(L"从 UnmanagedClass Qt 事件处理程序中调用 - 这就是你所需要的.");}

UnmanagedClass.h:

#pragma 一次#包括使用命名空间系统;参考类 ManagedClass;//封装的原生类类 UnmanagedClass{上市:UnmanagedClass(ManagedClass^ pWrapper);〜UnmanagedClass(无效);无效测试();私人的:gcroot m_pWrapper;};

UnmanagedClass.cpp:

#include "StdAfx.h"#include "UnmanagedClass.h"#include "ManagedClass.h"UnmanagedClass::UnmanagedClass(ManagedClass^ pWrapper){m_pWrapper = pWrapper;}UnmanagedClass::~UnmanagedClass(void){}无效 UnmanagedClass::Test(){m_pWrapper->回调();}

这让您了解非托管类和托管包装器如何一起发挥作用.如果您了解 Qt 编程并知道如何从 ref C++/CLI 类中引发 .NET 事件,那么这足以完成您的任务.

I'm writing a wrapper in .NET (C++/CLI) to be able to use some native C++ Qt code in .NET. How can I map a Qt signal into a managed .NET event, so that when my Qt code fires off a signal, I can bring this forward to the .NET code.

My Managed class defines the event, but if I try to use the normal QObject::connect method to hook up to the signal, it requires a QObject * receiver... I guess I have to do some magic marshalling of some sorts?

解决方案

Define normal unmanaged Qt event handler. From this handler, raise managed event.

Edit: if you have previous experience in Qt, you know how to handle Qt signals in unmanaged class. Don't try to make this class managed. Instead of this, write C++/CLI wrapper which contains this QObject-derived class.

Edit 2. Create C++/CLI Console application, call it 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's 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 idea how unmanaged class and managed wrapper can play together. If you know Qt programming and know how to raise .NET event from ref C++/CLI class, this is enough to accomplish your task.

这篇关于如何在托管 C++ (C++/CLI) 中将 Qt 信号映射到事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 00:29