PDA

View Full Version : Subclassing QApplication for global members and methods.



gilouu
31st December 2014, 15:26
Hello,

In the application I'm currently developping, I need to access (through setMode and getMode methods), from any classes, to a variable 'mode' that shows in which mode the application is.
I want other classes to be warned about changes of the value of this variables.
That's why I subclasses QApplication.
I cannot though access to this variable, this :

qDebug()<<QApplication_ac::instance()->getMode(); //here, the problem !

give the error :

class QCoreApplication' has no member named 'getMode'
qDebug()<<QApplication_ac::instance()->getMode();


How can I solve my problem ?

Many thanks for answer.

Here is the code :

qapplication_ac.h :


#ifndef QAPPLICATION_AC_H
#define QAPPLICATION_AC_H

#include <QApplication>

enum Mode {NOTHING=0,
PICKING_DETAIL,
PANNING,
ROTATION,
PICKING_POINT,
ABOUT_TO_CLOSE,
POLYGON_FINISHED
};

class QApplication_ac:public QApplication
{

Q_OBJECT

public:
QApplication_ac(int &argc, char *argv[]); //constuctor
Mode mode;
Mode getMode(){ return mode;}
void setMode(Mode modeToSet);

signals:

void modeChanged(Mode newMode,Mode oldMode); //other classes will be connected to this signal

};

#endif // QAPPLICATION_AC_H

qapplication_ac.cpp :


#include "qapplication_ac.h"

QApplication_ac::QApplication_ac(int &argc, char *argv[]):
QApplication(argc, argv)

{
mode=NOTHING;
}

void QApplication_ac::setMode(Mode modeToSet){

emit modeChanged(modeToSet,getMode());

mode=modeToSet;
}

wysota
31st December 2014, 15:31
You need to cast the pointer to your type as QCoreApplication::instance() returns a pointer to QCoreApplication. For convenience you can redefine the qApp macro to return your type directly.


#ifdef qApp
#undef qApp
#define qApp qobject_cast<QApplication_ac*>(QCoreApplication::instance())
#endif

... = qApp->getMode();

gilouu
31st December 2014, 16:29
So simple answer ! The year 2015 will start well b'cause you !
Many thanks.

Radek
31st December 2014, 17:39
How come the code has compiled?


void QApplication_ac::setMode(Mode modeToSet){

emit modeChanged(modeToSet,getMode());

mode=modeToSet;
}

modeToSet is an enum and it does not have a method getMode(). The compiler should protest. As to qApp, another simple method is creating your own replacement for qApp, say TheApp (a global variable):


QApplication_ac *TheApp = nullptr;

and setting it in the QApplication_ac ctor to this. Now, you can call TheApp->getMode().

ChrisW67
31st December 2014, 18:12
modeToSet is an enum value.
getMode() is a function that returns an enum value.
modeChanged() is a signal that takes two enum values as arguments: these are provided separated by a comma (not a period).

anda_skoa
1st January 2015, 14:50
The actual question is: why derive from QApplication?
Why make a tight coupling with a totally unrelated class?
Why not just have an application or library specific singleton?

Subclassing QApplication for something like that just makes it impossible to use any such code in unittests (they create the application object via macros).

Cheers,
_

wysota
2nd January 2015, 10:10
Subclassing QApplication for something like that just makes it impossible to use any such code in unittests (they create the application object via macros).
It's not that bad, you can create your own application object if you want.

anda_skoa
2nd January 2015, 12:23
It's not that bad, you can create your own application object if you want.

Of course you can, it has a virtual destructor :)
I am just saying that it is usually a bad idea.

Cheers,
_

wysota
2nd January 2015, 14:10
Of course you can, it has a virtual destructor :)
I am just saying that it is usually a bad idea.

I meant regarding the unit tests.