Results 1 to 3 of 3

Thread: Pure virtual class with singals and slots

  1. #1
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Pure virtual class with singals and slots

    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.

    Qt Code:
    1. class IGui
    2. {
    3. public slots:
    4. virtual void slot() = 0;
    5.  
    6. signals:
    7. virtual void signal() = 0;
    8. };
    9.  
    10. class MainWindow : public QMainWindow, public IGui
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private slots:
    19. void slot();
    20.  
    21. signals:
    22. void signal();
    23.  
    24. private:
    25. Ui::MainWindow *ui;
    26. };
    27.  
    28. class AppCore : public QObject
    29. {
    30. Q_OBJECT
    31.  
    32. public:
    33. explicit AppCore(IGui *gui, QObject *parent = 0);
    34.  
    35. signals:
    36. void doSlot();
    37.  
    38. private slots:
    39. void onSingal();
    40. ...
    41. ...
    42. }
    43.  
    44. AppCore::AppCore(IGui *gui, QObject *parent) :
    45. QObject(parent)
    46. {
    47. connect(gui, SIGNAL(signal()),
    48. this, SLOT(onSingal()));
    49.  
    50. connect(this, SIGNAL(doSlot()),
    51. gui, SLOT(slot()));
    52. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Pure virtual class with singals and slots

    One obvious problem is that IGui doesn't derive from QObject (or other QObject derived class), so it can't use signals and slots.

  3. #3
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Re: Pure virtual class with singals and slots

    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'

Similar Threads

  1. Replies: 4
    Last Post: 5th April 2012, 12:38
  2. QGraphicsItem: pure virtual method call problem
    By phuongot in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2012, 07:35
  3. Replies: 2
    Last Post: 26th November 2011, 04:52
  4. Cost of pure virtual
    By ShaChris23 in forum General Programming
    Replies: 4
    Last Post: 4th November 2007, 18:20
  5. inheritance, pure virtual
    By TheKedge in forum General Programming
    Replies: 2
    Last Post: 18th January 2007, 11:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.