views:

156

answers:

4

Im using the Qt library. I'm currently trying to create my own QDockWidget (the class MY class is inheriting). Right now MY class has an ptr to QDockWidget. Does this even make sense? is that a legal statement? is there a better way to separate the QDockWidget from the rest of my program in Qt? Im a little lost on how to implement a new Dock Widget. Here is a copy of MY QDockWidget class:

#ifndef DOC_MENU_WIDGET_H
#define DOC_MENU_WIDGET_H

#include "App_interface.h"
#include <QObject>
#include <QWidget>
#include <QDockWidget>

 class Doc_menu_widget : public QWidget
 {
     //Q_OBJECT

 public:
     Doc_menu_widget(App_interface *parent);
     ~Doc_menu_widget(); 

 private:
    QDockWidget *dock_widget;


 };

#endif
+1  A: 

Shouldn't you be doing something like this?

class Doc_menu_widget : public QDockWidget
{
    // ...
};

Subclassing QWidget and then having a private QDockWidget attribute of course does make sense, but it will probably not help you for implementing "your own dock widget" (as long as I understand it correctly). You only get the original dock widget this way and you can place it somewhere inside your new widget.

Alternatively, if you want to implement an equivalent of QDockWidget from scratch, you probably don't need the private QDockWidget.

Lukáš Lalinský
A: 

Sure, why not. And if you are just using a ptr to a base class object, you don't have any problems at all.

Why aren't you deriving directly from QDockWidget in the first place?

Do you want to express:

  • (a) Doc_menu_widget IS-A QDockWidget --> derive

  • (b) IS-IMPLEMENTED_IN_TERMS-OF QDockWidget (same as PERFORMS-LIKE QDockWidget) --> use a private member

fmuecke
A: 

If you just need to create a QDockWidget* for your QMainWindow, most of the times you can simply create an instance and use it right away:

QDockWidget* dock = new QDockWidget(this);
dock->setWindowTitle("My Dock Widget");
addDockWidget(Qt::LeftDockWidgetArea, dock, Qt::Vertical);

If instead you want to create a customized QDockWidget with different behaviour or appearance, then it's probably more convenient to inherit from QDockWidget:

#include <QDockWidget>

class MyDockWidget : public QDockWidget
{
Q_OBJECT
    // ...
};

Having a separate class with a QDockWidget* private member is of course entirely possible and legitimate, but it's not the most common choice in this situation. The reason is that you most probably want your class to be a variation of a QDockWidget (is-a relationship) meaning that it should have all its public methods and its instances should be able to passed to methods asking QDockWidget instances (for example, you want to be able to add one to a QMainWindow).

Paolo Capriotti
s/Q_WIDGET/Q_OBJECT/
Lukáš Lalinský
+3  A: 

You seem to be confusing the IS-A relationship and the HAS-A relationship.

IS-A relations are implemented by inheritance. For instance, a QWidget IS-A QObject.

HAS-A relations are implemted by members. For instance, a QWidget HAS-A size.

Now, what's the relation between the class you are trying to develop and a QDockWidget? That will tell you which of the two you should choose.

MSalters
What's wrong with being both? A 'Dock Widget' is a 'Widget' but there is nothing wrong with also owning another 'Widget' (which may be a sub widget or a different 'Widget').
Martin York
In this particular case, it's unlikely that a `QDockWidget` has another `QDockWidget`.
MSalters
has-a can be implemented via private inheritance. Though it's usually better to view that as "implemented in terms of".
jkyle