Opux
Opux

Reputation: 786

Qt: wrapping Q_OBJECT declaration in #ifndef directive

I think that

#ifndef SERVER
    Q_OBJECT
#endif

is causing

Note: No relevant classes found. No output generated.

from my compiler. Probably because whatever Qt preprocessor is looking for Q_OBJECT is not affected by (or not properly affected by) the C preprocessor directive. What's the proper way to deal with this?

I'm doing this because I don't need the signal-slot jazz on the server, so for the sake of efficiency, I eliminate that declaration.

I'm running Qt 4.8 building in GNU on Ubuntu 64-bit.

Upvotes: 1

Views: 490

Answers (2)

jpw
jpw

Reputation: 56

(Sorry I cannot comment yet, so I answer instead.)

I'd suggest @Mohammad Kanan's way, if possible in your case.

Otherwise, though, Qt's moc defines Q_MOC_RUN when it parses your code. That way, using #ifndef Q_MOC_RUN [...] #endif, you can exclude code portions from being processed by the moc entirely. (Reference: https://doc.qt.io/archives/qt-4.8/moc.html#command-line-options)

Upvotes: 0

Mohammad Kanan
Mohammad Kanan

Reputation: 4592

from what I understand, server and client compilation is done in same top level project and might coexist. if you want client to use Q_OBJECT macro while server not, then this code is C++ polymorphism realization of such requirement while having same class name for both (top_server):

Here all instances (Server, client ...etc.) are instantiated from top_Server ..

What matters is that server class never call Q_OBJECT macro while client class does implement the Q_OBJECT macro.

Top abstract class code:

abstract_class.h

#ifndef ABSTRACT_CLASS_H
#define ABSTRACT_CLASS_H
#include <QString>
class top_Server // Abstract Class
{
public:
    virtual bool method1 (QString,QString) =0;
};
#endif // ABSTRACT_CLASS_H

Then SERVER derived class declaration

server.h

#ifndef SERVER_H
#define SERVER_H
#include "abstract_class.h"
#include <QObject>

class Server: public top_Server
{

public:
    explicit Server (QString ,QString, QObject *parent = nullptr);
    bool method1 (QString,QString);
};
#endif // SERVER_H

Then Client derived Class declaration

client.h

#ifndef CLIENT_H
#define CLIENT_H
#include <QObject>
#include "abstract_class.h"
class client: public QObject, public top_Server // Client class inherits Abstract class and runs the Q_OBJECT macro
{
    Q_OBJECT

public:
    explicit client(QString ,QString, QObject *parent = nullptr);
    bool method1 (QString,QString);
};
#endif // CLIENT_H

Then class code in .CPP files

client::client(QString DB_user, QString DB_pwd, QObject *parent) : QObject(parent)
{

}
bool client::method1(QString str1, QString str2)
{

}

Server::Server (QString str1 ,QString str2 , QObject *parent)
{

}
bool Server::method1(QString str1, QString str2)
{

}

Now the implementation of Server and client is hidden and the same class name can be used to instantiate any, you also can switch from one object to another:

 top_Server *common_object_name;

then somewhere a client can be used:

common_object_name = new client;

.... and somewhere else a server

common_object_name = new server;

the cost is know of having two hidden classes while the advantage is obvious!

Upvotes: 1

Related Questions