I'm trying to use static polymorphism instead of dynamics polymorphism with Qt signal/slot mechanism. But I get compile error. What is wrong in my code? What is workaround?
devices.h
#ifndef DEVICES_H
#define DEVICES_H
#include <QtCore>
#include <qdebug.h>
class DeviceController : public QObject
{
Q_OBJECT
public:
explicit DeviceController(QObject *parent = nullptr):QObject(parent){}
virtual ~DeviceController() {}
void doAllDevicesInit(){
emit deviceAInitSignal();
}
signals:
void deviceAInitSignal();
};
template<typename T> class BaseDevice {
public:
void init() {
static_cast<T*>(this)->doInit();
qDebug() << QString("BaseDevice initialized!");
}
};
class DeviceA : public BaseDevice<DeviceA> {
public:
void doInit() {
qDebug() << "DeviceA initialized!";
}
};
#endif // DEVICES_H
main.cpp
#include "devices.h"
int main(int argc, char *argv[])
{
Q_UNUSED(argc);Q_UNUSED(argv);
DeviceA deviceA;
DeviceController deviceController;
QObject::connect(&deviceController,&DeviceController::deviceAInitSignal,
&deviceA, &DeviceA::init);
deviceController.doAllDevicesInit();
return 0;
}
Compile output
Qt5.12.2/5.12.2/gcc_64/include/QtCore/qobjectdefs_impl.h:414:94:
error: invalid static_cast from type ‘QObject*’ to type
‘QtPrivate::FunctionPointer::*)()>::Object*’
{aka ‘BaseDevice*’}
FuncType::template call(static_cast(this_)->function, static_cast(r), a);
Thanks to drescherjm comment, a workaround is as follow
devices.h
...
template<typename T>
class BaseDevice: public QObject {
//Q_OBJECT, Error: Template classes not supported by Q_OBJECT
public:
explicit BaseDevice(QObject *parent = nullptr):QObject(parent){}
virtual ~BaseDevice() {}
void init() {
static_cast<T*>(this)->doInit();
qDebug() << QString("BaseDevice initialized!");
}
};
class DeviceA : public BaseDevice<DeviceA> {
Q_OBJECT
public:
explicit DeviceA(QObject *parent = nullptr):BaseDevice<DeviceA>(parent){}
virtual ~DeviceA() {}
void doInit() {
qDebug() << "DeviceA initialized!";
}
};
#endif // DEVICES_H
Related
This code fails on second Q_ASSERT.
class A : public QObject
{
Q_OBJECT
public:
void function(QObject *receiveOb, const char *slot)
{
Q_ASSERT((bool)connect(this, SIGNAL(mySignal(int)), receiveOb, SLOT(mySlot(int))));
Q_ASSERT(receiveOb->metaObject()->indexOfMethod(slot) != -1);
}
signals:
void mySignal(int param);
};
class MainClass : public QObject
{
Q_OBJECT
A a;
public slots:
void mySlot(int param)
{
param++;
}
public:
MainClass(QObject *papi = Q_NULLPTR) : QObject(papi)
{ }
void doIt()
{
a.function(this, SLOT(mySlot(int)));
}
};
As I can see, if connect is able to lookup the slot method, I could do the same.
What am I doing wrong?
What other checks can I do to find out my mistake?
I try to get a QObject-subclassed object from a Qt shared library to a Qt application dynamically.
I have tried to apply a previous answer about this subject : QLibrary - import a class
Here is my common interface tabappinterface.h:
#ifndef TABAPP_H
#define TABAPP_H
#include <QObject>
#include <QWidget>
class TabAppInterface : public QObject
{
Q_OBJECT
public:
virtual ~TabAppInterface()
{
}
QWidget *app;
QString title;
};
#endif // TABAPP_H
My class dll-side mytabapp.h:
#ifndef MYTAB_H
#define MYTAB_H
#include "tabapp_global.h"
#include "tabappinterface.h"
class MYTABSHARED_EXPORT MyTabApp: public TabAppInterface
{
public:
MyTabApp();
virtual ~MyTabApp();
QWidget *app;
QString title;
};
extern "C" MYTABSHARED_EXPORT TabAppInterface *getTabApp();
and its implementation mytabapp.cpp:
#include "mytabapp.h"
MyTabApp::MyTabApp()
{
app = new AppWidget();
title = QStringLiteral("My Tab App");
}
MyTabApp::~MyTabApp()
{
}
TabAppInterface *getTabApp()
{
return new MyTabApp();
}
My app-side implementation:
void ContainerMainWindow::loadLibraries()
{
QLibrary myLib("mytabapp.dll");
if(myLib.isLoaded())
{
qDebug() << "Loaded!";
}
typedef TabAppInterface *(*tabAppGetProt)();
auto tabAppGetter = (tabAppGetProt) myLib.resolve("getTabApp");
if(tabAppGetter)
{
auto *tabApp = tabAppGetter(); // not null
qDebug() << tabApp->title; // print empty string
qDebug() << (QWidget *)(tabApp->app); // SEGFAULT
}
}
As stated in comment in the last lines, the object members are not retrieved although tabApp is not null.
Any idea?
Thanks!
You're accessing the base class variables (fields) title and app, which nobody ever initialized. These variables aren't virtual at all, so those in the derived class just hide those in the base class. As a solution, you can declare them as protected in the base class and encapsulate them in getters and setters.
This way, your base class is like:
class TabAppInterface : public QObject
{
Q_OBJECT
public:
virtual ~TabAppInterface(){}
QWidget *getApp() const { return app; }
void setApp(QWidget *value) { app = value; }
QString getTitle() const { return title; }
void setTitle(const QString &value) { title = value; }
protected:
QWidget *app;
QString title;
};
and the derived class is just like:
class MYTABSHARED_EXPORT MyTabApp: public TabAppInterface
{
public:
MyTabApp();
virtual ~MyTabApp();
};
You can still directly access the variables inside the derived class (i.e. initialize them in constructor) and through the getters/setters methods from outside:
auto *tabApp = tabAppGetter();
qDebug() << tabApp->getTitle();
qDebug() << tabApp->getApp();
using Qt 5.0.0
The following is roughly an Observer pattern (the code is stripped to bare minimum to explain only the problem):
class A : public QObject
{
Q_OBJECT
public:
void registerListner(Observer *pObs);
static A* getInstance();
signals:
void sig();
};
void A::registerListner(Observer *pObs)
{
connect(this, SIGNAL(sig()), pObs, SLOT(slo));
}
////////////////////////////////////////////////////////////////
class Observer : public QObject
{
Q_OBJECT
public slots:
virtual void slo() = 0;
};
class ConcreteObserver : public Observer , public QWidget
{
Q_OBJECT
public: //re-mentioning "slots" is not necessary
virtual void slo();
};
ConcreteObserver *pCObs = new ConcreteObserver;
A::getInstance()->registerListner(pCObs);
/////////////////////////////////////////////////////////////
problem (apart from dreaded-diamond):
Can't inherit multiple times from QObject - moc does not allow it.
One possible solution is derive Observer from QWidget and then ConcreteObserver from Observer alone. However this is putting a constraint on ConcreteObserver. Maybe ConcreteObserver_2 needs to derive from QDialog instead etc.
How do i solve this design problem? Is there anything specific to Qt 5.0.0 Signal-Slot (in addition to earlier versions) that can solve this, or what would you suggest?
If runtime warnings are not enough for you, you can add a bit of compile-time type checking by making registerListener a function template and avoid multiple inheritance of QObject by not defining an Observer class per-se.
Here's what this could look like: (Note: my SFINAE skills are non-existent, this could probably be made nicer.)
#include <QObject>
#include <QDebug>
#include <type_traits>
class A : public QObject
{
Q_OBJECT
public:
template <typename T>
void registerListener(T *pObs)
{
static_assert(std::is_base_of<QObject, T>::value,
"Listener must be a QObject");
static_assert(std::is_same<void,
decltype(std::declval<T>().slo())
>::value,
"Slot slo must have signature void slo();");
connect(this, SIGNAL(sig()), pObs, SLOT(slo()));
}
static A* getInstance() { return instance; }
static void init() { instance = new A; }
void doStuff() { emit sig(); }
signals:
void sig();
private:
static A *instance;
};
A few test cases:
class BadObject1 : public QObject
{
Q_OBJECT
public:
BadObject1() {}
public slots:
void slo(int){}
};
class BadObject2 : public QObject
{
Q_OBJECT
public:
BadObject2() {}
public slots:
int slo(){return 0;}
};
struct BadObject3 {
void slo();
};
class ObservedObject : public QObject
{
Q_OBJECT
public:
ObservedObject(QString const& name): QObject() {
setObjectName(name);
}
public slots:
virtual void slo(){
qDebug() << objectName();
}
};
class ObservedObject2 : public ObservedObject
{
Q_OBJECT
public:
ObservedObject2(QString const& name)
: ObservedObject(name + " (derived)") {}
};
And a main file:
#include "A.h"
A* A::instance = 0;
int main(int , char **)
{
A::init();
A::getInstance()->registerListener(new BadObject1);
A::getInstance()->registerListener(new BadObject2);
A::getInstance()->registerListener(new BadObject3);
A::getInstance()->registerListener(new ObservedObject("foo"));
A::getInstance()->registerListener(new ObservedObject2("bar"));
A::getInstance()->doStuff();
}
You'll get compiler errors for all the BadObjectN cases. If you comment them out, the output will look like:
"foo"
"bar (derived)"
A warning though: this will not check if the void slo(); member is indeed a slot. You can check that at runtime with something like:
if (pObs->metaObject()->indexOfSlot("slo()") == -1) {
qDebug() << "Class" << pObs->metaObject()->className()
<< "doesn't have a slo slot.";
::exit(1);
}
This will work and do what is expected (unless you've got a class hierarchy where the slot wasn't declared virtual - then strange things will happen in derived classes that omit the slots "specifier". So I advocate that your docs not have the comment you have above about that specifier: it is always a good idea to have it when overloading a slot).
I don't believe this last check is achievable at compile-time, "slot resolution" is done with a runtime walk of the QObject meta-data and involves parsing moc-generated strings. Even if it was with some recursive template magic, I don't think it's work the effort. You'll get a runtime error message at registration type in which you can include the actual class name of the faulty object. That's a very accurate error message, and should be caught by the simplest testcases.
in my project if I define base class Base_Dialog as non template and then try to assign 'caller' in already_exists_ it works in the way expected but if I make Base_Dialog as a template class then the algorithm 'already_exists_' (unchanged) will not work and caller will not change (this is especially intriguing that the algorithm is unchanged and yet once works and other time it doesn't):
//This is minimal example (I know it's somewhat longer than usual but in order to show what I mean it needs to be this length)
MAIN_DIALOG_HPP
#ifndef MAIN_DIALOG_HPP
#define MAIN_DIALOG_HPP
#include <QSet>
#include <QtDebug>
#include "Base_Dialog.hpp"
#include "ui_Main_Dialog.h"
#include "_1Dialog.hpp"
#include "_2Dialog.hpp"
/*The following approach will not work*/
class Main_Dialog : public Base_Dialog<Ui::Main_Dialog>
{
/*but if I would do as below (changing Base_Dialog to non-template) it will work:*/
//class Main_Dialog : public QDialog, private Ui::Main_Dialog, public Base_Dialog
Q_OBJECT
QSet<QDialog*>* dialogs_;
private:
template<class Dialog,class Caller>
bool already_created_(Caller*const&, QDialog*& already_exists);
template<class Dialog,class Caller, class Parent>
QDialog* create_(Caller*const&,Parent*const&);
public:
explicit Main_Dialog(QWidget *parent = 0);
template<class Dialog,class Caller>
QDialog* get_dialog(Caller*const& caller);
public slots:
void _1clicked()
{
this->hide();
get_dialog<_1Dialog>(this)->show();
}
void _2clicked()
{
this->hide();
get_dialog<_2Dialog>(this)->show();
}
};
template<class Dialog,class Caller>
bool Main_Dialog::already_created_(Caller*const& caller,QDialog*& already_exists)
{/*the already_exists is introduced here in order to remove repetions of code and
searching*/
auto beg = dialogs_->begin();
auto end = dialogs_->end();
while(beg != end)
{
if(dynamic_cast<Dialog*>(*beg))
{
already_exists = *beg;
static_cast<Base_Dialog*>(already_exists)->set_caller(caller);
return true;
}
++beg;
}
return false;
}
template<class Dialog,class Caller, class Parent>
QDialog* Main_Dialog::create_(Caller *const&caller, Parent *const&parent)
{
return (*dialogs_->insert(new Dialog(this,caller,parent)));
}
template<class Dialog,class Caller>
QDialog* Main_Dialog::get_dialog(Caller *const&caller)
{
QDialog* already_exists = nullptr;
if (already_created_<Dialog>(caller,already_exists))
{
return already_exists;
}
else
{
return create_<Dialog>(caller,this);
}
}
Main_Dialog::Main_Dialog(QWidget *parent) :
Base_Dialog<Ui::Main_Dialog>(this,this,parent),dialogs_(new QSet<QDialog*>)
{
setupUi(this);
}
#endif // MAIN_DIALOG_HPP
BASE_DIALOG_HPP
#ifndef BASE_DIALOG_HPP
#define BASE_DIALOG_HPP
#include <QDialog>
#include <QString>
class Main_Dialog;
template<class Ui_Dialog>
class Base_Dialog : public QDialog, protected Ui_Dialog
{
// Q_OBJECT //no signals/slots
protected:
Main_Dialog* main_dlg_;
QDialog* caller_;
public:
Base_Dialog(Main_Dialog *const &main_dlg, QDialog *const&caller, QWidget *parent = nullptr);
QDialog* set_caller(QDialog *const&);
QDialog* clear_caller();
Main_Dialog* clear_main_dlg();
};
/*----------------*/
//#include "Main_Dialog.hpp"
template<class Ui_Dialog>
Base_Dialog<Ui_Dialog>::Base_Dialog(Main_Dialog *const&main_dlg,QDialog *const&caller, QWidget *parent):
QDialog(parent),
main_dlg_(main_dlg),
caller_(caller)
{
//setupUi(this);
}
#include <QtDebug>
template<class Ui_Dialog>
QDialog* Base_Dialog<Ui_Dialog>::set_caller(QDialog *const&new_caller)
{
QDialog* old_caller = caller_;
caller_ = new_caller;
return old_caller;
}
#endif
_1DIALOG_HPP
#ifndef _1DIALOG_HPP
#define _1DIALOG_HPP
#include "Base_Dialog.hpp"
#include "ui__1Dialog.h"
class Main_Dialog;
class _1Dialog : public Base_Dialog<Ui::_1Dialog>
{
Q_OBJECT
public:
explicit _1Dialog(Main_Dialog* main_dlg, QDialog*caller, QWidget *parent = 0);
private slots:
void _2clicked();
void caller_clicked();
void main_clicked();
};
#endif // _1DIALOG_HPP
_1Dialog cpp
//_1Dialog cpp
#include "_1Dialog.hpp"
#include "_2Dialog.hpp"
#include "Main_Dialog.hpp"
_1Dialog::_1Dialog(Main_Dialog* main_dlg, QDialog*caller, QWidget *parent) :
Base_Dialog<Ui::_1Dialog>(main_dlg,caller,parent)
{
setupUi(this);
}
void _1Dialog::_2clicked()
{
this->hide();
main_dlg_->get_dialog<_2Dialog>(this)->show();
}
void _1Dialog::caller_clicked()
{
this->hide();
caller_->show();
}
void _1Dialog::main_clicked()
{
this->hide();
main_dlg_->show();
}
_2DIALOG_HPP
#ifndef _2DIALOG_HPP
#define _2DIALOG_HPP
#include "Base_Dialog.hpp"
#include "ui__2Dialog.h"
class Main_Dialog;
class _2Dialog : public Base_Dialog<Ui::_2Dialog>//,private Ui::_2Dialog
{
Q_OBJECT
private:
public:
explicit _2Dialog(Main_Dialog* main_dlg, QDialog*caller, QWidget *parent = 0);
private slots:
void _1clicked();
void caller_clicked();
void main_clicked();
};
#endif // _2DIALOG_HPP
_2Dialog cpp
//_2Dialog cpp
#include "_2Dialog.hpp"
#include "_1Dialog.hpp"
#include "Main_Dialog.hpp"
_2Dialog::_2Dialog(Main_Dialog* main_dlg, QDialog*caller, QWidget *parent) :
Base_Dialog<Ui::_2Dialog>(main_dlg,caller,parent)
{
setupUi(this);
}
void _2Dialog::_1clicked()
{
this->hide();
main_dlg_->get_dialog<_1Dialog>(this)->show();
}
void _2Dialog::caller_clicked()
{
this->hide();
caller_->show();
}
void _2Dialog::main_clicked()
{
this->hide();
main_dlg_->show();
}
Why is this behavior? Algorithm is unchanged and yet once it assigns correctly and the other time it doesn't?
In already_exists_ change line:
static_cast<Base_Dialog*>(already_exists)->set_caller(caller);
to:
static_cast<Dialog*>(already_exists)->set_caller(caller);
and add virtual inheritance for QDialog in Base_Dialog
#sehe, I want to thank you for pointing me into right direction, which allow me to resolve this problem. Great thanks, +1;
In my code I have:
template<class Ui_Dialog>
QDialog* Base_Dialog<Ui_Dialog>::set_caller(QDialog *new_caller)
{
QDialog* old_caller = caller_;
caller_ = new_caller;//Here I'm trying to set this to new caller
return old_caller;
}
but after setting the caller to new caller and exiting from this fnc, when I call caller I'm still getting the old caller instead of new one, as if no changes were made. Why?
EDIT:
//caller is defined in a following way:
class Main_Dialog : public Base_Dialog<Ui::Main_Dialog> {};
EDIT 2:
The interesting thing is that if instead of public Base_Dialog I alter Base_Dialog to non-template and define Main_Dialog as:
class Main_Dialog : public Base_Dialog, private Ui::Main_Dialog {};
Then it works as intended. Why?!
EDIT 3:
class Main_Dialog;
template<class Ui_Dialog>
class Base_Dialog : public QDialog, protected Ui_Dialog
{
// Q_OBJECT //no signals/slots
protected:
Main_Dialog* main_dlg_;
QDialog* caller_;
public:
Base_Dialog(Main_Dialog *main_dlg, QDialog *caller, QWidget *parent = nullptr);
QDialog* set_caller(QDialog *);
QDialog* clear_caller();
Main_Dialog* clear_main_dlg();
};
/*----------------*/
#include "Main_Dialog.hpp"
template<class Ui_Dialog>
Base_Dialog<Ui_Dialog>::Base_Dialog(Main_Dialog *main_dlg,QDialog *caller, QWidget *parent):
QDialog(parent),
main_dlg_(main_dlg),
caller_(caller)
{
//setupUi(this);
}
#include <QtDebug>
template<class Ui_Dialog>
QDialog* Base_Dialog<Ui_Dialog>::set_caller(QDialog *new_caller)
{
QDialog* old_caller = caller_;
caller_ = new_caller;
return old_caller;
}
#include "_1Dialog.hpp"
#include "_2Dialog.hpp"
class Main_Dialog : public Base_Dialog<Ui::Main_Dialog>
{
Q_OBJECT
QSet<QDialog*>* dialogs_;
private:
template<class Dialog,class Caller>
bool already_created_(Caller*&, QDialog*& already_exists)const;
template<class Dialog,class Caller, class Parent>
QDialog* create_(Caller*,Parent*);
/*template<class Dialog>
void show_();*/
/* template<class Dialog,class Caller>
QDialog* find_(Caller*)const;*/
public:
explicit Main_Dialog(QWidget *parent = 0);
template<class Dialog,class Caller>
QDialog* get_dialog(Caller*& caller);
public slots:
void _1clicked();
void _2clicked();
};
template<class Dialog,class Caller>
bool Main_Dialog::already_created_(Caller*& caller,QDialog*& already_exists)const
{/*the already_exists is introduced here in order to remove repetions of code and
searching*/
auto beg = dialogs_->begin();
auto end = dialogs_->end();
while(beg != end)
{
if(dynamic_cast<Dialog*>(*beg))
{
already_exists = *beg;
static_cast<Base_Dialog*>(already_exists)->set_caller(caller);
return true;
}
++beg;
}
return false;
}
template<class Dialog,class Caller, class Parent>
QDialog* Main_Dialog::create_(Caller *caller, Parent *parent)
{
return (*dialogs_->insert(new Dialog(this,caller,parent)));
}
template<class Dialog,class Caller>
QDialog* Main_Dialog::get_dialog(Caller *&caller)
{
QDialog* already_exists = nullptr;
if (already_created_<Dialog>(caller,already_exists))
{
return already_exists;
}
else
{
return create_<Dialog>(caller,this);
}
#include "Base_Dialog.hpp"
#include "ui__1Dialog.h"
class Main_Dialog;
class _1Dialog : public Base_Dialog<Ui::_1Dialog>
{
Q_OBJECT
public:
explicit _1Dialog(Main_Dialog* main_dlg, QDialog*caller, QWidget *parent = 0);
private slots:
void _2clicked();
void caller_clicked();
void main_clicked();
};
#endif // _1DIALOG_HPP
#include "_1Dialog.hpp"
#include "_2Dialog.hpp"
#include "Main_Dialog.hpp"
_1Dialog::_1Dialog(Main_Dialog* main_dlg, QDialog*caller, QWidget *parent) :
Base_Dialog(main_dlg,caller,parent)
{
setupUi(this);
}
void _1Dialog::_2clicked()
{
this->hide();
main_dlg_->get_dialog<_2Dialog>(this)->show();
}
void _1Dialog::caller_clicked()
{
this->hide();
caller_->show();
}
void _1Dialog::main_clicked()
{
this->hide();
main_dlg_->show();
}
}
#endif // MAIN_DIALOG_HPP
I think your problem not in set_caller. Next works for me:
#include <iostream>
using namespace std;
class QDialog {
int i;
public:
QDialog() : i(0) {}
QDialog(int i) : i(i) {}
void me() { cout << i << endl; }
};
namespace UI {
class Main_Dialog {}; // EDITED
}
template<class Ui_Dialog>
class Base_Dialog : public QDialog, protected Ui_Dialog
{
QDialog* caller_;
public:
QDialog* set_caller(QDialog *new_caller);
QDialog* get_caller() {return caller_; } ;
};
template<class Ui_Dialog>
QDialog* Base_Dialog< Ui_Dialog>::set_caller(QDialog *new_caller)
{
QDialog* old_caller = caller_;
caller_ = new_caller;//Here I'm trying to set this to new caller
return old_caller;
}
class Main_Dialog : public Base_Dialog<UI::Main_Dialog> {
public:
};
int main()
{
QDialog q1(1);
QDialog q2(2);
Main_Dialog md;
md.set_caller(&q1);
md.get_caller()->me();
md.set_caller(&q2);
md.get_caller()->me();
return 0;
}