编译QDbus服务器应用程序时出错?

时间:2014-02-19 10:51:37

标签: c++ qt

我是Dbus的新手,当然是QDBUS的新手。我试图从诺基亚开发者-QT论坛复制这个例子。我有一个xml文件,通过它我生成了qdbus接口adaptor.cpp和.h。现在,我正在尝试包含此文件,并构建它。但是,我得到了编译错误。你能帮他解决这个问题吗?

(。text.startup + 0x4c): - 1:错误:对MyDemo :: MyDemo(QObject *)'的未定义引用

generated- adaptor.cpp

/*
 * This file was generated by qdbusxml2cpp version 0.7
 * Command line was: qdbusxml2cpp -c DemoIfAdaptor -a demoifadaptor.h:demoifadaptor.cpp com.nokia.Demo.xml
 *
 * qdbusxml2cpp is Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 *
 * This is an auto-generated file.
 * Do not edit! All changes made to it will be lost.
 */

#include "demoifadaptor.h"
#include <QtCore/QMetaObject>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>

/*
 * Implementation of adaptor class DemoIfAdaptor
 */

DemoIfAdaptor::DemoIfAdaptor(QObject *parent)
    : QDBusAbstractAdaptor(parent)
{
    // constructor
    setAutoRelaySignals(true);
}

DemoIfAdaptor::~DemoIfAdaptor()
{
    // destructor
}

void DemoIfAdaptor::SayBye()
{
    // handle method call com.nokia.Demo.SayBye
    QMetaObject::invokeMethod(parent(), "SayBye");
}

void DemoIfAdaptor::SayHello(const QString &name, const QVariantMap &customdata)
{
    // handle method call com.nokia.Demo.SayHello
    QMetaObject::invokeMethod(parent(), "SayHello", Q_ARG(QString, name), Q_ARG(QVariantMap, customdata));
}

生成的Adaptor.h

/*
 * This file was generated by qdbusxml2cpp version 0.7
 * Command line was: qdbusxml2cpp -c DemoIfAdaptor -a demoifadaptor.h:demoifadaptor.cpp com.nokia.Demo.xml
 *
 * qdbusxml2cpp is Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 *
 * This is an auto-generated file.
 * This file may have been hand-edited. Look for HAND-EDIT comments
 * before re-generating it.
 */

#ifndef DEMOIFADAPTOR_H_1392803889
#define DEMOIFADAPTOR_H_1392803889

#include <QtCore/QObject>
#include <QtDBus/QtDBus>
class QByteArray;
template<class T> class QList;
template<class Key, class Value> class QMap;
class QString;
class QStringList;
class QVariant;

/*
 * Adaptor class for interface com.nokia.Demo
 */
class DemoIfAdaptor: public QDBusAbstractAdaptor
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "com.nokia.Demo")
    Q_CLASSINFO("D-Bus Introspection", ""
"  <interface name=\"com.nokia.Demo\">\n"
"    <method name=\"SayHello\">\n"
"      <annotation value=\"QVariantMap\" name=\"com.trolltech.QtDBus.QtTypeName.In1\"/>\n"
"      <arg direction=\"in\" type=\"s\" name=\"name\"/>\n"
"      <arg direction=\"in\" type=\"a{sv}\" name=\"customdata\"/>\n"
"    </method>\n"
"    <method name=\"SayBye\"/>\n"
"    <signal name=\"LateEvent\">\n"
"      <arg direction=\"out\" type=\"s\" name=\"eventkind\"/>\n"
"    </signal>\n"
"  </interface>\n"
        "")
public:
    DemoIfAdaptor(QObject *parent);
    virtual ~DemoIfAdaptor();

public: // PROPERTIES
public Q_SLOTS: // METHODS
    void SayBye();
    void SayHello(const QString &name, const QVariantMap &customdata);
Q_SIGNALS: // SIGNALS
    void LateEvent(const QString &eventkind);
};

#endif

main.cpp文件 -

#include <QtCore/QCoreApplication>
#include <QVariant>
#include <QDBusAbstractInterface>
#include "demoifadaptor.h"


class MyDemo : public QObject
{
Q_OBJECT
public:
explicit MyDemo(QObject *parent = 0);

public Q_SLOTS:
    void SayBye();
    void SayHello(const QString &name, const QVariantMap &customdata);
Q_SIGNALS:
    void LateEvent(const QString &eventkind);

};


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    MyDemo* demo = new MyDemo;

    new DemoIfAdaptor(demo);

     QDBusConnection connection = QDBusConnection::sessionBus();
     bool ret = connection.registerService("com.nokia.Demo");
     ret = connection.registerObject("/", demo);


    return a.exec();
}

MyDemo.pro文件。

#-------------------------------------------------
#
# Project created by QtCreator 2014-02-19T15:30:36
#
#-------------------------------------------------

QT       += core

QT       -= gui
QT += dbus

TARGET = MyDemo
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
    demoifadaptor.cpp

HEADERS += \
    demoifadaptor.h

1 个答案:

答案 0 :(得分:1)

您的示例提供了explicit MyDemo(QObject *parent = 0);的声明,但未提供其定义。因此链接器错误。其余的MyDemo方法也必须定义。

您还需要在源文件上运行元对象编译器以避免

  

“虚函数表...”

错误。尝试重新运行qmake。每次向Q_OBJECT宏添加新呼叫时,都需要再次运行qmake。您在评论中提到的vtables问题与此直接相关。

如果错误仍然存​​在,请在#include "MyDemo.moc"

的末尾添加main.cpp