PDA

View Full Version : Pure virtual class with singals and slots



rubikon
31st August 2012, 10:49
Hello.

I want to write a small tool and like to separate the business logic from the GUI so I can easily make and use different GUIs.

Therefore I've made a class which contains the business logic called AppCore. The GUI class(es) are called MainWindow.

My idea is to pass the constructor of the AppCore a reference to the GUI so the constructor of the AppCore can connect the AppCore singals to the GUI slots and vice versa.

But to make it flexible I don't want to have the actual GUI as type of the parameter but an Interface (pure virtual class) called IGui which the GUI inherits from.



class IGui
{
public slots:
virtual void slot() = 0;

signals:
virtual void signal() = 0;
};

class MainWindow : public QMainWindow, public IGui
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void slot();

signals:
void signal();

private:
Ui::MainWindow *ui;
};

class AppCore : public QObject
{
Q_OBJECT

public:
explicit AppCore(IGui *gui, QObject *parent = 0);

signals:
void doSlot();

private slots:
void onSingal();
...
...
}

AppCore::AppCore(IGui *gui, QObject *parent) :
QObject(parent)
{
connect(gui, SIGNAL(signal()),
this, SLOT(onSingal()));

connect(this, SIGNAL(doSlot()),
gui, SLOT(slot()));
}

But when I try to compile I get errors:



AppCore.cpp: In constructor 'AppCore::AppCore(IGui*, QObject*)':
AppCore.cpp:9: error: no matching function for call to 'AppCore::connect(IGui*&, const char [10], AppCore* const, const char [12])'
c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qobject.h:204: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qobject.h:217: note: static bool QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)
c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qobject.h:337: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
AppCore.cpp:12: error: no matching function for call to 'AppCore::connect(AppCore* const, const char [10], IGui*&, const char [8])'
c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qobject.h:204: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qobject.h:217: note: static bool QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)
c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qobject.h:337: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const


How do I have to do it right?

Zlatomir
31st August 2012, 11:26
One obvious problem is that IGui doesn't derive from QObject (or other QObject derived class), so it can't use signals and slots.

rubikon
31st August 2012, 11:32
If I let IGui derive from QObject I get this errors:


release\moc_MainWindow.cpp: In static member function 'static void MainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)':
release\moc_MainWindow.cpp:52: error: 'QObject' is an ambiguous base of 'MainWindow'
release\moc_MainWindow.cpp: In member function 'virtual const QMetaObject* MainWindow::metaObject() const':
release\moc_MainWindow.cpp:78: error: 'QObject' is an ambiguous base of 'MainWindow'
release\moc_MainWindow.cpp:78: error: 'QObject' is an ambiguous base of 'MainWindow'
release\moc_MainWindow.cpp: In member function 'virtual int MainWindow::qt_metacall(QMetaObject::Call, int, void**)':
release\moc_MainWindow.cpp:98: error: 'QObject' is an ambiguous base of 'MainWindow'
release\moc_MainWindow.cpp: In member function 'virtual void MainWindow::signal()':
release\moc_MainWindow.cpp:107: error: 'QObject' is an ambiguous base of 'MainWindow'