Search code examples
c++qtslot

connect signals on a child widget slot


I have some problems with inheritance in widgets and connecting slots. I have created an abstract Widget which inherits from QWidget. Here is the prototype :

class WidgetParams : public QWidget
{
   Q_OBJECT
 public:
     explicit WidgetParams(QWidget *parent = 0) : QWidget(parent){}
     virtual bool paramChanged() = 0;
 protected:
      bool paramsChanged;
 };

Then I created derivated class from WidgetParams, for example WidgetParamsWindows:

class WidgetParamsWindows : public WidgetParams
{
  public:
    explicit WidgetParamsWindows(QWidget *parent = 0);
    virtual bool paramChanged(){return paramsChanged;}
  private:
    QFormLayout *layout;
    QSpinBox *svertical;
    QSpinBox *shorizontal;
  signals:

  public slots:
    void changeSomeParam(int value);
};

In WidgetParamsWindows, I have some QSpinBox, QPushButton etc. to adjust the params.

I connect the QSpinBox in WidetParamsWindows like this :

connect(spinbox,SIGNAL(valueChanged(int)),this,SLOT(changeSomeParam(int));

After that, I created a WidgetParamsWindows and put It in a list of WidgetParams, in order to show the correct WidgetParams when the user clicks on it.

But when I tried to change the value in the QSpinBox, nothing change and I have the following message in the console :

QObject::connect: No such slot WidgetParams::changeSomeParam(int)

I don't know why the parent Widget takes the slot, instead of WidgetParamsWindows, do you have any ideas?


Solution

  • There is no Q_OBJECT macro in WidgetParamsWindow, so moc doesn't resolve slot macros, try to add Q_OBJECT in WidgetParamsWindow