1

In Qt C++, is it possible to create a custom QWidget and then reuse this custom QWidget for all QWidget (that inherit all from the custom QWidget) of the project?

Dan
  • 12,157
  • 12
  • 50
  • 84
mav
  • 121
  • 3
  • 10

2 Answers2

3

Maybe I have misunderstood the question, but you can just create your custom QWidget, then use it everywhere.

class derivedQWidget : public QWidget
{
  Q_OBJECT

  derivedQWidget();
  virtual ~derivedQWidget();
}

class myWidget : public derivedQWidget 
{
  ...
}

class myWidget2 : public derivedQWidget 
{
  ...
}

If the question is: Can we reimplement QWidget?, no you can't.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
MokaT
  • 1,416
  • 16
  • 37
0

i have solved in this mode:

the first class, Widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QPushButton>
#include <QMouseEvent>
#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    virtual ~Widget();

    QPushButton *getBtn() const;
    void setBtn(QPushButton *value);

protected:
    void mousePressEvent(QMouseEvent *evt);
    void mouseMoveEvent(QMouseEvent *evt);

private:
    Ui::Widget *ui;
    QPushButton *btn;
    QPoint oldPos;
};

and the second class widExt.h, that inherit from Widget:

#ifndef WIDEXT_H
#define WIDEXT_H

#include "widget.h"

namespace Ui {
    class widExt;
}

class widExt : public Widget
{
public:
    widExt();


private slots:
    void on_dial_2_actionTriggered(int action);

private:
    Ui::widExt *ui;
};

#endif // WIDEXT_H

with the relative widExt.cpp:

#include "widext.h"
#include "ui_widext.h"

widExt::widExt() : ui(new Ui::widExt)
{
    ui->setupUi(this);
}

void widExt::on_dial_2_actionTriggered(int action)
{

}

in this mode, i inherit all from the first class and i can customize other classes independently.

mav
  • 121
  • 3
  • 10