Results 1 to 6 of 6

Thread: Virtual class as a QWidget interface and dependency inversion principle.

  1. #1
    Join Date
    Jun 2009
    Location
    Krakow, Poland
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Virtual class as a QWidget interface and dependency inversion principle.

    Hi
    I have classes named IStorage and Storage. For example:
    Qt Code:
    1. class IStorage : public QWidget {
    2. Q_OBJECT
    3. public:
    4. virtual void Method1(const QString &par1);
    5.  
    6. signals:
    7. void IStorageSignal1(const QString &par1);
    8.  
    9. public slots:
    10. virtual void SetVariableValue(const int &val);
    11. };
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. namespace Ui {
    2. class Storage ;
    3. }
    4. class Storage : public IStorage {
    5. Q_OBJECT
    6. public:
    7. explicit Storage();
    8. public slots:
    9. void SetVariableValue(const int &val);
    10. private:
    11. int localVariable;
    12. };
    To copy to clipboard, switch view to plain text mode 

    How to (fallowing dependency inversion principle) implement class "MainWidgetDisplay" to make it independent from IStorage and to be able to connect with IStorage's signals and slots?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Virtual class as a QWidget interface and dependency inversion principle.

    There is no problem in using virtual slots, so if you write code like this:

    Qt Code:
    1. void MainWidgetDisplay::makeConnectionsToIStorage( IStorage * someStorage )
    2. {
    3. connect( someStorage, SIGNAL( IStorageSignal1( const QString & ) ), this, SLOT( myHandler( const QString & ) ) );
    4. connect( this, SIGNAL( setVariableValue( const int & ) ), someStorage, SLOT( SetVariableValue( const int & ) ) );
    5. }
    To copy to clipboard, switch view to plain text mode 

    Then if at runtime "someStorage" actually is a "Storage *" pointer, then everything should work as you expect.

    The Storage class can emit the IStorageSignal1 signal, and when MainWidgetDisplay emits it's setVariableValue() signal, the correct Storage slot will be called. MainWidgetDisplay.cpp needs to include IStorage.h and that's all. It doesn't need to know about classes derived from IStorage if it only uses the virtual methods.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Virtual class as a QWidget interface and dependency inversion principle.

    How to (fallowing dependency inversion principle) implement class "MainWidgetDisplay" to make it independent from IStorage and to be able to connect with IStorage's signals and slots?
    If you want to implement MainWidgetDisplay independent of IStorage you could well do it. One simple trick is to check your dependency is make sure that you don't include IStorage.h file from MainWidgetDisplay implementation and header files either directly or indirectly. So if you want to connect signal and slots between them, the signal and slots connections SHOULD NOT be made from either off these two classes. The signal and slots connections between them should be made by some other class generally by the parent object class implementation (as in below example)
    Qt Code:
    1. //Example SomeClass (parent objects's class)
    2. #include "SomeClass.h"
    3. #include "IStorage.h"
    4. #include "MainDisplayWidget.h"
    5.  
    6.  
    7. void SomeClass::ShowMainWidget(void)
    8. {
    9. IStorage* storage = new IStorage(this);
    10. MainDisplayWidget* widget = new MainDisplayWidget(this);
    11.  
    12. // IStorage Signal >>>>>>>>>>>>>>>> MainDisplayWidget Slot
    13. connect(storage, SIGNAL(IStorageSignal(const QString)), widget, SLOT(MainDisplayWidgetSlot(const QString)));
    14.  
    15. // MainDisplayWidget Signal >>>>>>>>>>>>>>>> IStorage Slot
    16. connect(widget, SIGNAL(MainDisplayWidgetSignal(const QString)), storage, SLOT(IStorageSlot(const QString)));
    17. }
    To copy to clipboard, switch view to plain text mode 
    This way MainDisplayWidget is independent of IStorage, and also IStorage is independent of MainDisplayWidget.

  4. The following user says thank you to Santosh Reddy for this useful post:

    porterneon (18th November 2011)

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Virtual class as a QWidget interface and dependency inversion principle.

    I think I misread the original post to say "independent of Storage" (not IStorage). As usual, Santosh has a good approach, which I actually use all the time when I want to connect two objects that don't need to know anything about each other.

  6. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Virtual class as a QWidget interface and dependency inversion principle.

    you can just forward declare the classes and you can still connect signals slots I think.

    Also, I'm not sure about your idea of dependency inversion - The method is to make the higher level class dependant on the abstraction - IStorage. So there is no problem with mainwindow being dependant up on it, because it is an abstraction.
    Last edited by amleto; 18th November 2011 at 21:39.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Virtual class as a QWidget interface and dependency inversion principle.

    you can just forward declare the classes and you can still connect signals slots I think.


    Also, I'm not sure about your idea of dependency inversion - The method is to make the higher level class dependant on the abstraction - IStorage. So there is no problem with mainwindow being dependant up on it, because it is an abstraction.
    I think the OP wanted MainDisplayWidget to be independent of the abstraction class itself (or the interface class itself), which is IStorage class in this case.

    Forward class declaration will make MainDisplayWidget class declaration independent of the IStorage class implementation / declaration (or Interface implementation / declaration) but MainDisplayWidget class implementation still has to dependent on IStorage class declaration.

Similar Threads

  1. Replies: 1
    Last Post: 17th December 2010, 16:43
  2. Replies: 1
    Last Post: 30th October 2010, 13:28
  3. separate class using an interface
    By qt_gotcha in forum Newbie
    Replies: 7
    Last Post: 3rd March 2010, 22:54
  4. Replies: 0
    Last Post: 3rd January 2009, 06:26
  5. Need help on wrapping Accessible Interface to QWidget
    By thi_optimist in forum Qt Programming
    Replies: 0
    Last Post: 27th November 2006, 20:10

Tags for this Thread

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.