• 4873阅读
  • 2回复

信号的值如何返回? [复制链接]

上一主题 下一主题
离线tsuibin
 

只看楼主 倒序阅读 楼主  发表于: 2009-03-31
代码如下
  1. #include <QApplication>
  2. #include <QPushButton>
  3. #include <QHBoxLayout>
  4. #include <QSlider>
  5. #include <QSpinBox>
  6. int main(int argc, char *argv[])
  7. {
  8.     QApplication app(argc, argv);
  9.     QWidget *window = new QWidget;
  10.        window->setWindowTitle("Show your Input!");
  11.     QSpinBox *spinBox = new QSpinBox;
  12.     QPushButton *button = new QPushButton("set spinBox to 0");
  13.     QSlider *slider = new QSlider(Qt::Horizontal);
  14.         spinBox->setRange(0,130);
  15.         slider->setRange(0,130);
  16.         button->setText("set 0");
  17.     QObject::connect(spinBox,
  18.                      SIGNAL(valueChanged(int)),
  19.                      slider,
  20.                      SLOT(setValue(int))
  21.                      );
  22.     QObject::connect(slider,
  23.                      SIGNAL(valueChanged(int)),
  24.                      spinBox,
  25.                      SLOT(setValue(int))
  26.                      );
  27.     QObject::connect(button,
  28.                      SIGNAL(clicked()),
  29.                      spinBox,SLOT(setValue(0))
  30.                      );
  31.      spinBox->setValue(35);
  32.         QHBoxLayout *layout = new QHBoxLayout;
  33.       layout->addWidget(spinBox);
  34.       layout->addWidget(slider);
  35.       layout->addWidget(button);
  36.       window->setLayout(layout);
  37.       window->show();
  38.     return app.exec();
  39. }


我希望在点击按钮的时候,文本框清零
   QObject::connect(button,
                     SIGNAL(clicked()),
                     spinBox,SLOT(setValue(0))
                     );
这里的SIGNAL是clicked()
SLOT是setValue(0)
可是点了却没有效果
要怎么作?
离线daily

只看该作者 1楼 发表于: 2009-03-31
SIGNAL clicked() 没有任何传出去的值。 所以spinbox的public slot setvalue(0) 是没有办法连接的, QT 默认信号参数要多于槽参数。
离线marswjk
只看该作者 2楼 发表于: 2009-04-02
你可以自定义一个信号和槽,槽用来接收button的clicked()信号,信号接收到之后,将value赋给自定义信号发射出去,发射给spinbox的setvalue,那样就可以了
我愿为你走遍天涯海角,直到地老天荒~~~
快速回复
限100 字节
 
上一个 下一个