Qt自定义插槽

时间:2011-10-29 03:40:47

标签: c++ qt slider signals-slots qt-signals

为什么插槽Reset()不起作用?我希望按钮“reset”将sider的值重置为零。

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()) );

1 个答案:

答案 0 :(得分:6)

尝试将Q_OBJECT添加到课程的私有部分。这将导致moc创建信号和插槽工作所需的元数据。见http://doc.qt.io/archives/qt-4.7/qobject.html#Q_OBJECT