Results 1 to 6 of 6

Thread: Why Qt do not use interfaces for their classes?

  1. #1
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Why Qt do not use interfaces for their classes?

    Hi all,

    In my company I started implementing a library where some of classes should inherit from Qt classes.

    I look on source code for example of QAuthenticator class and I see that it does not use interface ( does not inherit any interface / pure abstract class ) but use PIMPL idiom ( it is fine ).

    And now when I want for example change the QAuthenticator implementation without change public API of this class I can not do that.

    I would like ask you if do you think that good library should be build using interfaces and PIMPL idiom? For example:

    Qt Code:
    1. class IAuthenticator
    2. {
    3. public:
    4. virtual ~IAuthenticator() {}
    5. virtual QString user() const = 0;
    6. virtual void setUser(const QString &user) = 0;
    7.  
    8. virtual QString password() const = 0;
    9. virtual void setPassword(const QString &password) = 0;
    10.  
    11. // etc ...
    12. };
    13.  
    14.  
    15. class AuthenticatorPrivate
    16. {
    17. public:
    18. QString method;
    19. QString user;
    20. QString password;
    21. QString realm;
    22. };
    23.  
    24.  
    25. class Authenticator : public IAuthenticator
    26. {
    27. public:
    28. // ...
    29.  
    30. private:
    31. QScopedPointer<AuthenticatorPrivate> d; // d-pointer == PIMPL
    32. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why Qt do not use interfaces for their classes?

    Interfaces are nice when you expect multiple implementations of something.
    E.g. Qt uses those in such places, e.g. QAbstractButton, QAbstractItemModel, etc.

    Sometimes the API class itself is constant but different implementations get loaded, e.g. QWindow and QPlatformWindow.

    QAuthenticator is a value class, a structured data container, like QUrl, QColor, QString, etc.
    They basically just bundle different primitive items into one compound item.

    Value classes in C++ are almost never derived from anything, at least not virtually, since that can easily lead to unpleasent things like slicing.

    Cheersm
    _

  3. #3
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why Qt do not use interfaces for their classes?

    Ok, so can you tell me how I should design classes in my library using Qt?

    For example it is a good?

    Qt Code:
    1. class AuthenticatorPrivate : public QSharedData
    2. {
    3. public:
    4. AuthenticatorPrivate();
    5. ~AuthenticatorPrivate();
    6.  
    7. QString method;
    8. QString user;
    9. QString password;
    10. };
    11.  
    12. class Authenticator
    13. {
    14. public:
    15. Authenticator();
    16.  
    17. QString method() const;
    18. void setMethod(const QString &method);
    19.  
    20. QString user() const;
    21. void setUser(const QString &user);
    22.  
    23. QString password() const;
    24. void setPassword(const QString &password);
    25.  
    26. private:
    27. QSharedDataPointer<AuthenticatorPrivate> d;
    28. };
    To copy to clipboard, switch view to plain text mode 

    so all 'private' classes inherit from QSharedData and then this private class is store in private section of my class as QSharedDataPointer<AuthenticatorPrivate> d;,
    It is a good design?

    Regards,

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why Qt do not use interfaces for their classes?

    If you want your objects to have the copy-on-write behavior that Qt's value classes have, then this is a good approach.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why Qt do not use interfaces for their classes?

    So all 'private' class should / can be inherited from QSharedData?

    I saw that PIMPL idiom in different libraries is implemented on very different way, for example one library use struct for private class, when another use class, one library use pure pointer for private class, when another use smart pointer etc...

    I look for 'general' design technics for all my classes which should use PIMPL idiom.

    Regards,

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why Qt do not use interfaces for their classes?

    The type of pointer being used depends on the use case.

    QSharedData and QSharedDataPointer are nice helper classes when you want to have reference countred, copy-on-write data classes.

    A shared pointer can be used if multiple copies of a API object should point to the same private.

    You can use a QScopedPointer if the private is always allocated by the API class constructor.

    You can use a raw pointer or a shared pointer if the private is not always allocated as long as the API class is.

    Cheers,
    _

Similar Threads

  1. QMetaObject list interfaces
    By golubevsv in forum Qt Programming
    Replies: 5
    Last Post: 13th September 2014, 10:17
  2. How to use Microsoft COM interfaces in QT?
    By luochen601 in forum Qt Programming
    Replies: 3
    Last Post: 31st May 2010, 04:22
  3. Qt Designer for web interfaces
    By rbp in forum Qt Tools
    Replies: 3
    Last Post: 14th October 2009, 02:01
  4. Polymorphism interfaces in C++
    By ComaWhite in forum General Programming
    Replies: 2
    Last Post: 1st July 2009, 12:38
  5. Plugin Interfaces
    By Paul Drummond in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2006, 13:37

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.