jeudi 17 décembre 2015

Is it feasible to use decorator pattern with QWidget?

According to wikipedia, I need to

In the Decorator class, redirect all "Component" methods to the "Component" pointer;

But QWidget has too many methods to redirect. For example I want to create a moveable widget decorator:

class Moveable : public QWidget
{
    Q_OBJECT

public:
    explicit Moveable(QWidget * widgetToBeDecorated){
        this->widgetToBeDecorated = widgetToBeDecorated;
    }
    ~Moveable();

protected:
    void mousePressEvent(QMouseEvent* e) override {
        lastPoint = e->pos();
        widgetToBeDecorated->mousePressEvent(e);
    }
    void mouseMoveEvent(QMouseEvent* e) override {
        auto offset = e->pos()-lastPoint;
        lastPoint = e->pos();

        widgetToBeDecorated->move(widgetToBeDecorated->pos()+offset);
        widgetToBeDecorated->mouseMoveEvent(e);
    }
    void mouseReleaseEvent(QMouseEvent* e) override {
        widgetToBeDecorated->mouseReleaseEvent(e);
    }

private:
    QWidget* widgetToBeDecorated;
    QPoint lastPoint;
};

Do I need to redirect other methods like show(), paintEvent(QPaintEvent*)?

Aucun commentaire:

Enregistrer un commentaire