[Qt-interest] inteface implementation in C++/Qt

lbutler3 lbutler3 at comcast.net
Wed Feb 23 02:39:32 CET 2011


I'm looking to define a common interface for MDI windows in my application.  I would like each MDI window to be derived from a kind of QWidget so that the windows can be easily parented to a QMdiSubWindow or a QDialog on demand.  The MainWindow would only know that the window is derived from some sort of QWidget, and that it implements the interface.  The two approaches that occur to me are:  

Defining a base class that derives from QWidget and has the necessary interface, and derive Mdi window types fromt this class:
class MyMdiWindow : public QWidget {
MyMdiWindow(parent *p=NULL) : QWidget(p) {}
virtual void fileLoad(const QString &fileName) = 0;
};

Defining a template class, declare a class with the appropriate widget class, then derive from *that* to provide file-type specific methods:
template <class widgetType>
class MyMdiWindow : public widgetType {
MyMdiWindow(parent *p=NULL) : QWidget(p) {}
virtual void fileLoad(const QString &fileName) = 0;
};

The first approach is nice because it allows easy specialization and only one level of inheritance.  However, most of my MdiWindows only have a single child widget inside, so the extra layering seems wasteful.  Perhaps not a big deal, but it didn't seem "elegant".

The second approach allows the "widgetType" to be the one type of widget needed for the display, but would require another level of inheritance to provide the specialization.

Under a language like ObjectiveC I would define an Interface and simply state that the class implements the interface.  Is there an appropriate C++ or Qt way of doing this?

Lee


More information about the Qt-interest-old mailing list