3

Why doesn't the slot Reset() work? I want the button "reset" to reset the value of the sider to zero.

class MySlider : public QSlider
{
   public:
    MySlider(Qt::Orientation orientation, QWidget *parent = 0) : QSlider(orientation parent){}

   public slots:
   void Reset()
   {
   this->setValue(0);
   }
};

//it doesnt work when i try this

MySlider * Slider = new MySlider(Qt::Horizontal, this);

QPushButton *Reset = new QPushButton(tr("Reset"), this);

connect(Reset, SIGNAL(clicked()), Slider, SLOT(Reset()) );
SamB
  • 9,039
  • 5
  • 49
  • 56
Geore Shg
  • 1,299
  • 5
  • 23
  • 38

1 Answers1

6

Try adding Q_OBJECT to the private section of the class. This will cause moc to create the meta-data necessary for signals and slots to work. See http://doc.qt.io/archives/qt-4.7/qobject.html#Q_OBJECT

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
  • I tried Q_GADGET (becuase i need it to be a QSlider subclass) and it didn't work – Geore Shg Oct 29 '11 at 04:57
  • 2
    Wow, I hadn't encountered Q_GADGET before, I actually had to go look that up :) QSlider is a QObject and so is your subclass of QSlider so use Q_OBJECT. – Arnold Spence Oct 29 '11 at 05:16
  • @ArnoldSpence When I tried QObject in the private section of the class I got this error: undefined reference to 'vtable for MySlider'. – Geore Shg Oct 29 '11 at 05:23
  • http://stackoverflow.com/questions/4774291/q-object-throwing-undefined-reference-to-vtable-error fixes this error – Geore Shg Oct 29 '11 at 05:40
  • 1
    @Geore: `QSlider` is a descendant of `QObject`, so `Q_OBJECT` is the correct choice, not `Q_GADGET` – Evan Teran Oct 30 '11 at 05:45