2017-11-07 30 views
1

NetworkManager의 org.freedesktop.NetworkManager.Settings.Connection 인터페이스를 쿼리하고 "GetSettings"를 호출합니다. Dbus 유형 용어로 Dict of {String, Dict of {String, Variant}} 또는 a{sa{sv}}을 반환합니다. 내 응용 프로그램을 빌드하려면 Qt4reator를 Qt4와 함께 사용하고 있습니다.{String, {String, Variant}}의 Dict을 QDBus와 어떻게 파싱합니까?

이 사전에서 유용한 정보를 얻을 수없는 것 같습니다. NetworkManager와 DBus 및 Qt4가 다른 시스템에 설치되어있는 경우 크게 의존하기 때문에 MVE를 제공 할 수 없습니다.

이 문자열 사전 및 사전 변형 정보를 얻으려는 방법입니다. qDebug()에 파이프 할 때 원하는 멋진 데이터를 볼 수 있습니다 : qDebug()<<reply.

void GetInfo() 
{ 
    //sysbus just means the system DBus. 
    QDBusInterface connSettings("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings/1", "org.freedesktop.NetworkManager.Settings.Connection", sysbus); 
    QDBusMessage reply = connections.call("GetSettings"); 
    if(reply.type() == QDBusMessage::ReplyMessage) 
    { 
     //I have tried everything I can imagine here, 
     //QVariant g = reply.arguments().at(0).value<QVariant>(); did not work 
     //g.canConvert<QMap>(); returns false, in contrast to what KDE says. 
     //QDbusArgument g = query.arguments().at(0).value<QDBusArgument>(); 
     //g.beginMap(); and such don't work 
    } 
} 

Dict 유형을 구문 분석하는 데 정보를 찾는 것이 매우 어렵습니다. 일부 정보를 제공하는 유일한 소스는 KDE입니다. 그것은 "DBus Dict 유형은 QMap에 따라야합니다, 따르는 예는 .."이고 Google이나 예제에는 다른 조회수가 없습니다. 어쩌면 나는 기초적인 DBus 지식을 놓치고 있을지 모르지만, 나는 혼란 스럽다.

나는 또한이 우수한 대답을 체크 아웃했다 : How do I extract the returned data from QDBusMessage in a Qt DBus call? 그러나 나는 그것을 받아쓰기를 해석하기 위해 적응할 수 없었다.

마지막으로 중첩 된 QVariant를 얻는 방법을 아는 사람이 있습니까?

+0

그것은 더 나은 것은 MCV가 당신을 도와 줘야하는 (Qt5에서이기는하지만,하지만 난 그것뿐만 아니라 QT4에서 작동합니다 기대) ... 아주 간단한 환경 qdbus에 대한 Dict을 보내고 받으려면 작업을 많이 에스컬레이션합니다. Btw xml에서 "GetSettings"메서드의 정의는 무엇입니까? – miradham

답변

2

불행히도 Qts DBUS API는 항상 가장 이해하기가 쉽지 않으므로 여기에 몇 가지 팁이 있습니다. 기본적으로 나에게 맞는 것을 찾았는데 응답에서 DBusArgument을 가져와야하고 실제 데이터를 얻기 위해 실제로 사용되는 것입니다. 추출중인 항목에 따라 .cpp 파일 (the documentation for QDBusArgument says how to do this)에서 operator<<operator>>을 무시하거나 추출 할 형식을 정의 할 수 있습니다. 주목해야 할 또 다른 중요한 점은 회신 또는 통화 여부에 따라 QDBusReply::arguments()contains either 또는 인수를 수신한다는 것입니다. 어쨌든

는 다음 날 위해 작품

QDBusInterface connSettings("org.freedesktop.NetworkManager", 
          "/org/freedesktop/NetworkManager/Settings/1", 
          "org.freedesktop.NetworkManager.Settings.Connection", 
          QDBusConnection::systemBus()); 
QDBusMessage reply = connSettings.call("GetSettings"); 

qDebug() << "Reply below:"; 
qDebug() << reply; 

qDebug() << "Extracted: "; 

// Extract the argument from the reply 
// In this case, we know that we get data back from the method call, 
// but a range check is good here as well(also to ensure that the 
// reply is a method reply, not an error) 
const QDBusArgument &dbusArg = reply.arguments().at(0).value<QDBusArgument>(); 

// The important part here: Define the structure type that we want to 
// extract. Since the DBus type is a{sa{sv}}, that corresponds to a 
// Map with a key of QString, which maps to another map of 
// QString,QVariant 
QMap<QString,QMap<QString,QVariant> > map; 
dbusArg >> map; 
qDebug() << "Map is: " << map; 

// We're just printing out the data in a more user-friendly way here 
for(QString outer_key : map.keys()){ 
    QMap<QString,QVariant> innerMap = map.value(outer_key); 

    qDebug() << "Key: " << outer_key; 
    for(QString inner_key : innerMap.keys()){ 
     qDebug() << " " << inner_key << ":" << innerMap.value(inner_key); 
    } 
} 
+0

도움이된다면 답을 수락하는 것을 잊지 마십시오! – rm5248