I have a custom class Dummy which I want to send as an object using Dynamic Replica in Qt6. I'm able to send the class but it's being transferred as QVariant which I'm not able to extract(or cast) from QVariabnt object.
Below is my implementation:
Dummy.h file
#ifndef CLIENT_DUMMY_H
#define CLIENT_DUMMY_H
#include <QtCore/qobject.h>
#include <QtCore/qdatastream.h>
#include <QtCore/qvariant.h>
#include <QtCore/qmap.h>
#include <QtCore/qmetatype.h>
#include <QtRemoteObjects/qremoteobjectnode.h>
#include <QtRemoteObjects/qremoteobjectsource.h>
#include <QtCore>
class Dummy {
Q_GADGET
Q_PROPERTY(QString m_name READ name WRITE setName)
public:
Dummy(){}
explicit Dummy(QString str) : m_name(str) {}
QString name() const {
return m_name;
}
void setName(QString str){
m_name = str;
}
~Dummy() = default;
private:
QString m_name;
};
Q_DECLARE_METATYPE(Dummy)
inline QDataStream &operator<<(QDataStream &ds, const Dummy &obj) {
QtRemoteObjects::copyStoredProperties(&obj, ds);
return ds;
}
inline QDataStream &operator>>(QDataStream &ds, Dummy &obj) {
QtRemoteObjects::copyStoredProperties(ds, &obj);
return ds;
}
inline bool operator==(const Dummy &left, const Dummy &right) Q_DECL_NOTHROW {
return left.name() == right.name();
}
inline bool operator!=(const Dummy &left, const Dummy &right) Q_DECL_NOTHROW {
return !(left == right);
}
inline QDebug operator<<(QDebug dbg, const Dummy &obj) {
dbg.nospace() << "Dummy(" << "m_name: " << obj.name() << ")";
return dbg.maybeSpace();
}
The class is present at both sides server as well as client.
.rep file
class Interface {
PROP(Dummy dummy);
[...removed extra code]
}
Server method sending data to client:
void send() {
Dummy dummy("DummyString");
setDummy(dummy);
}
Client side file:
Inside constructor:
QObject::connect(reptr.data(), SIGNAL(dummyChanged(Dummy)), this, SLOT(receiveDummy(Dummy)));
void DynamicClient::receiveDummy(Dummy dummy) {
if(reptr.data()->isReplicaValid()){
QVariant variant = reptr.data()->property("dummy");
qDebug() << variant;
}
}
But when object from server to client is sent, qDebug() prints below:
QVariant(Dummy, QVariant(QString, "DummyString"))
I'm not able to extract Dummy Object from Qvariant object.
I've tried registering my custom type using qRegisterMetaType() as well but still it didn't work. Apart from that, I've used qvariant_cast and variant.value() but I printed the value I get some random character each time.
Thanks in advance. I can post more code if required.
Turns out I was not registering the Dummy class on client side due to which client was not able to recognize the type.
qRegisterMetaType needs to be used at both ends.
Related
I need some help with walking a tree with a listener (using C++). After initiating the tree walk with my listener and using gdb to attach to the process (I built the C++ runtime library with the Debug flag to be able to have the debug info in gdb), I can see it enters ParseTreeWalker::walk(), and if I execute getText() on the ParseTree* parameter I can see it has all the tokens from my input, so far so good. But when walk() calls enterRule():
void ParseTreeWalker::enterRule(ParseTreeListener *listener, ParseTree *r) const {
auto *ctx = downCast<ParserRuleContext*>(r);
listener->enterEveryRule(ctx);
ctx->enterRule(listener);
}
When I try to step in ctx->enterRule(listener) it just goes back to walk(). I've added breakpoints to my listener methods I want to debug but it appears they're not being called. I even added some std::cout statements in them but I don't see them being executed. I also added some std::cout statements to my overriden version of visitTerminal and those are getting printed...
If I inspect the ctx variable inside this method, it does seem to have the right type
_vptr.ParseTree = 0x6e6048 <vtable for MyParser::DocumentContext+16>...
So, I'm very confused, am I missing something obvious that's causing my enter* methods not get executed?
The generated BaseListener looks like this:
#include "antlr4-runtime.h"
#include "MyDCXParserListener.h"
class MyDCXParserBaseListener : public MyDCXParserListener {
public:
virtual void enterDocument(MyDCXParser::DocumentContext * /*ctx*/) override { }
virtual void exitDocument(MyDCXParser::DocumentContext * /*ctx*/) override { }
virtual void enterLine(MyDCXParser::LineContext * /*ctx*/) override { }
virtual void exitLine(MyDCXParser::LineContext * /*ctx*/) override { }
virtual void enterEveryRule(antlr4::ParserRuleContext * /*ctx*/) override { }
virtual void exitEveryRule(antlr4::ParserRuleContext * /*ctx*/) override { }
virtual void visitTerminal(antlr4::tree::TerminalNode * /*node*/) override { }
virtual void visitErrorNode(antlr4::tree::ErrorNode * /*node*/) override { }
};
The derived class I created looks like this:
.h
#include "antlr4-runtime.h"
#include "MyDCXParser.h"
#include "MyDCXParserBaseListener.h"
#include <iostream>
using namespace std;
class MyDCXListener : public MyDCXParserBaseListener
{
private:
public:
//
MyDCXListener();
//
~MyDCXListener();
// Don't allow copy constructor nor assignment operator
MyDCXListener(const MyDCXListener&) = delete;
MyDCXListener& operator=(const MyDCXListener&) = delete;
//
void enterDocument(MyDCXParser::DocumentContext * /*ctx*/) override;
void exitDocument(MyDCXParser::DocumentContext * /*ctx*/) override {}
void enterLine(MyDCXParser::LineContext * /*ctx*/) override;
void exitLine(MyDCXParser::LineContext * /*ctx*/) override {}
void visitTerminal(antlr4::tree::TerminalNode *node) override { std::cout << "visitTerminal(): '" << node->getText().c_str() << std::endl; }
};
.cpp
#include "MyDCXListener.h"
//
MyDCXListener::MyDCXListener()
{
}
MyDCXListener::~MyDCXListener()
{
}
void MyDCXListener::enterDocument(MyDCXParser::DocumentContext * ctx)
{
std::cout << "MyDCXListener::enterDocument()" << std::endl;
}
void MyDCXListener::enterLine(MyDCXParser::LineContext * ctx)
{
std::cout << "MyDCXListener::enterLine()" << std::endl;
}
I see the "visitTerminal" message but not the ones for enterDocument() nor enterLine()
Is it possible to serialize std::chrono::duration or especially std::chrono::milliseconds with boost::serialization?
The following error occurred:
"struct std::chrono::duration >’ has no member named ‘serialize"
I need this for my template-class:
#include <iostream>
#include <chrono>
#include <boost/serialization/serialization.hpp>
template<typename T>
class Field {
public:
// Default Constructor
Field() {}
Field(std::string name, T value) :
mName(name), mValue(value) {}
~Field() {}
const std::string& getName() const {
return mName;
}
T getValue() const {
return mValue;
}
private:
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive& archive, const unsigned int) {
archive & boost::serialization::make_nvp("Name", mName);
archive & boost::serialization::make_nvp("Value", mValue);
}
std::string mName;
T mValue;
};
BOOST_CLASS_IMPLEMENTATION( Field<int>, boost::serialization::object_serializable )
BOOST_CLASS_IMPLEMENTATION( Field<std::chrono::milliseconds>, boost::serialization::object_serializable )
I'm creating the following object:
Field<std::chrono::milliseconds> mSimTime;
Let's say you have:
auto earlier = std::chrono::system_clock::now();
auto now = std::chrono::system_clock::now();
auto aLotOfMilliseconds= std::chrono::duration_cast<std::chrono::milliseconds>(
now - earlier);
// Serialize here int64_t.
then after deserialization, you have:
std::chrono::duration<int64_t, std::milli> duration(aLotOfMilliseconds);
I have a very confusing problem.
I had a simple project that was downloading files from some ftp servers. It worked very good.
Then, I tried implementing that same code into a larger project (first one was a Console App, and the second one is GUI, but I don't think that changes anything..).
After doing some debugging it seems to me that finished() signal from QNetworkAccessManager somehow never gets emitted (or received).
Again, the exact same lines of code work as a separate project.
downloader.h
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QFile>
#include <QDebug>
class Downloader : public QObject
{
Q_OBJECT
public:
explicit Downloader(QObject *parent = 0);
signals:
void dloadend();
void printed();
public slots:
void replyFinished (QNetworkReply *reply);
void doDownload(QUrl url);
void printDLend();
private:
QNetworkAccessManager *manager;
};
#endif // DOWNLOADER_H
downloader.cpp
#include "downloader.h"
Downloader::Downloader(QObject *parent) :
QObject(parent)
{
}
void Downloader::doDownload(QUrl url)
{
qDebug()<<"entry: doDownload\n";
QNetworkRequest req(url);
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),this,SLOT(printDLend()));//SLOT(replyFinished(QNetworkReply*)));
manager -> get(req);
qDebug()<<"exit: doDownload\n";
}
void Downloader::replyFinished (QNetworkReply *reply)
{
qDebug()<<"entry: reply\n";
if(reply->error()) {
qDebug() << "ERROR!";
qDebug() << reply->errorString();
}
else
{
qDebug() << "Download finished!";
QFile *file = new QFile("C:/users/jelicicm/Desktop/test1.hex");
if(file->open(QFile::Append))
{
file->write(reply->readAll());
file->flush(); file->close();
qDebug() <<"Downloaded file size:" <<file->size() <<"Bytes";
qDebug() <<"File name: "<< file->fileName();
;
}
delete file;
}
reply->deleteLater();
manager->deleteLater();
emit dloadend();
}
mainwindow.cpp (important part)
void MainWindow::on_actionDownloadFirmwareImage_triggered()
{
Downloader d;
QUrl url("ftp://ftp.xenbase.org/pub/Genomics/JGI/Xenla6.0/Xenla_6.0_JGI_Gene_Models.fasta.tgz");
qDebug() << "url, debug";
ui->plainTextEdit->appendPlainText(url.toDisplayString());
d.doDownload(url);
QObject::connect(&d,SIGNAL(dloadend()),this, SLOT(printDLend()));
}
Can't get my head around this.
Any help is welcome,
Thanks!
EDIT> More info:
Debugger posts this>
url, debug
entry: doDownload
exit: doDownload
You create object Downloader on stack and it is deleted right after your function exits. You must create object using new, and provide MainWindow object as parent, so after you close MainWindow, the object will be destroyed.
If the download finish, you still need to destroy the object, so simply connect the dloadend() signal to deleteLater() slot, Qt loop will delete your object right after all signal are processed.
void MainWindow::on_actionDownloadFirmwareImage_triggered()
{
Downloader *d = new Downloader(this);
QUrl url("ftp://example.com/some.file.tgz");
qDebug() << "url, debug";
ui->plainTextEdit->appendPlainText(url.toDisplayString());
d->doDownload(url);
QObject::connect(d,SIGNAL(dloadend()),this, SLOT(printDLend()));
QObject::connect(d,SIGNAL(dloadend()), d, SLOT(deleteLater()));
}
I have a large solution with 50+ unmanaged projects in it. I have recently added a project with managed code in it to the solution. The managed code accesses Windows.Devices.Sensors in a .NET dll. This dll is eventually wrapped by unmanaged code and called from another unmanaged project.
My problem is that I get the following access violation before main() even executes.
Unhandled exception at 0x744b8ea0 in myApplication.exe: 0xC0000005: Access violation.
Managed code:
#using <Windows.winmd>
using namespace Windows::Devices::Sensors;
#include <math.h>
namespace TabletSensors
{
namespace NET
{
public ref class DotNetDllClass
{
public:
DotNetDllClass()
{
Initialization();
}
~DotNetDllClass()
{
}
float* GetQuaternion()
{
OrientationSensorReading^ reading = _orientation->GetCurrentReading();
if( reading != nullptr )
{
float* quat = new float[4];
quat[0] = reading->Quaternion->X;
quat[1] = reading->Quaternion->Y;
quat[2] = reading->Quaternion->Z;
quat[3] = reading->Quaternion->W;
return quat;
}
else
{
return NULL;
}
}
private:
void Initialization()
{
_orientation = OrientationSensor::GetDefault();
if( _orientation != nullptr )
{
_orientation->ReportInterval = 16;
}
else
{
// not good ... throw exception or something
}
}
OrientationSensor^ _orientation;
};
}
}
Wrapper header file:
namespace TabletSensors
{
namespace NETWrapper
{
class DLLEXPORT_SENSORS WrapperClass
{
public:
__stdcall WrapperClass();
__stdcall ~WrapperClass();
float* __stdcall GetQuaternion();
};
}
}
Wrapper cpp file:
#define MIXSENSORS_BUILD
#include <gcroot.h>
#include "DotNetWrapper.h"
#include "DotNetDll.h"
using namespace TabletSensors::NETWrapper;
using namespace TabletSensors::NET;
static gcroot<TabletSensors::NET::DotNetDllClass^> Sensors = nullptr;
static System::UInt16 refCount = 0;
#pragma managed
inline TabletSensors::NET::DotNetDllClass^ GetSensors(void)
{
return (TabletSensors::NET::DotNetDllClass^)Sensors;
}
void Init()
{
++refCount;
if(GetSensors() == nullptr)
{
Sensors = gcnew TabletSensors::NET::DotNetDllClass();
}
}
void CleanUp()
{
if( refCount > 0 )
{
--refCount;
}
}
float* GetQuaternion_()
{
return Sensors->GetQuaternion();
}
#pragma unmanaged
TabletSensors::NETWrapper::WrapperClass::WrapperClass()
{
Init();
}
TabletSensors::NETWrapper::WrapperClass::~WrapperClass()
{
CleanUp();
}
float* TabletSensors::NETWrapper::WrapperClass::GetQuaternion()
{
float* x = new float[4];
return GetQuaternion_();
}
#pragma managed
Unmanaged project referencing my wrapper class:
#include "DotNetWrapper.h"
.
.
.
void UnmanagedProject::Update()
{
// if this line is present, I get an access violation without hitting any breakpoints.
TabletSensors::NETWrapper::WrapperClass _tabletSensors;
.
.
.
}
Since the managed code is trying to access Tablet Sensors I understand why it doesn't work on my Windows 7 desktop. What I don't understand it why it won't even allow me to debug my code at all. No breakpoints are hit before the Access Violation occurs.
What I would really like to figure out is how to use exception handling or #ifdefs to keep this crash from happening. But I have had very little luck.
Any ideas?
The fix is to Delay Load the managed DLL. The allows the application to run until that DLL is explicitly called. Thanks to Ben Voight for his answer here: https://stackoverflow.com/a/28467701/1454861
I'm trying to write a wrapper for a very simple std::pair<std::string, float> in C++/CLI but I get the error message: no instance of constructor "std::pair<_Ty1, _Ty2>::pair [with _Ty1=std::string, _Ty2=float]" matches the argument list, argument types are: (std::string, float)
What am I doing wrong and why doesn't std::string match std::string?
#include <msclr\marshal_cppstd.h>
#include <string>
using namespace System;
using namespace System::Runtime::InteropServices;
typedef std::pair<std::string, float> Parameter;
static std::string StringToNative(String^ str)
{
msclr::interop::marshal_context context;
return context.marshal_as<std::string>(str);;
}
public ref class CLIParameter
{
public:
CLIParameter(System::String^ name, float value) : _name(name), _value(value) {};
Parameter toNativeParameter()
{
return Parameter(StringToNative(_name), _value);
}
private:
System::String^ _name;
float _value;
};
int main()
{
CLIParameter^ par = gcnew CLIParameter("test", 1);
}
Your method toNativeParameter() is incorrect. It should be defined as follows:
Parameter toNativeParameter()
{
// copy the floating point value from the managed heap to the local stack
float value = _value;
// create the pair
return std::make_pair(StringToNative(_name), value);
}
Notice that you should use std::make_pair to create the actual pair. In addition, a key step to make this work is copying the floating point value from the managed heap into the local stack. The reason is that native functions such as std::make_pair cannot create native references to an object from the managed (garbage collected) heap i.e. a member of a managed class.