tags:

views:

32

answers:

2

hello, I have this snippet of the code:

#include <QApplication>
 #include <QFont>
 #include <QLCDNumber>
 #include <QPushButton>
 #include <QSlider>
 #include <QVBoxLayout>
 #include <QWidget>

 class MyWidget : public QWidget
 {
 public:
     MyWidget(QWidget *parent = 0);
 };

 MyWidget::MyWidget(QWidget *parent)
     : QWidget(parent)
 {
     QPushButton *quit = new QPushButton(tr("Quit"));
     quit->setFont(QFont("Times", 18, QFont::Bold));

     QLCDNumber *lcd = new QLCDNumber(3);
     lcd->setSegmentStyle(QLCDNumber::Flat);

     QSlider *slider = new QSlider(Qt::Horizontal);
     slider->setRange(0, 999);
     slider->setValue(0);

     connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

     connect(slider, SIGNAL(valueChanged(int)),
             lcd, SLOT(display(int)))

     QVBoxLayout *layout = new QVBoxLayout;
     layout->addWidget(quit);
     layout->addWidget(lcd);
     layout->addWidget(slider);
     setLayout(layout);
 }

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     MyWidget widget;
     widget.show();
     return app.exec();
 }

when I reach the max of the interval, I want to quit, how can I implement this with signals and slots, thanks in advance

+1  A: 

Add one more slot-function to your MyWidget class, for example on_maximum_exit(int) like that:

class MyWidget : public QWidget
{
public:
    MyWidget(QWidget *parent = 0);
public slots:
    void on_maximum_exit(int );
};

Define new function:

void MyWidget::on_maximum_exit(int value)
{
    if (value == slider->maximum())
        close();
}

In MyWidget consctuctor, after connect(slider, SIGNAL(valueChanged(int)),... add:

/* ... */
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(on_maximum_exit(int)));
/* ... */

And of course use Qt docs!

mosg
@mosg: I have two problems, in this row: value == slider->maximum(), slider is not defined within scope, and second, even after I changed it to 999, it doesn't work, can You please explain why?
wantSTUDY
Yes, you need predefine `slider` in your class first. Add `QSlider *slider;` in your class after `public` word and code just live `slider = new QSlider(Qt::Horizontal);`;
mosg
A: 

Like I said in the other post this should work for you:

main.cpp

#include <QApplication>
#include "mywidget.h"

int main(int argc, char *argv[])
{
 QApplication app(argc, argv);
 MyWidget widget;
 widget.show();
 return app.exec();
}

mywidget.h

 #ifndef MYWIDGET_H
 #define MYWIDGET_H
 #include <QWidget>
 #include <QObject>
 include <QPushButton>
 #include <QSlider>

 class MyWidget : public QWidget
 {
  Q_OBJECT
  public:
MyWidget(QWidget *parent = 0);
~MyWidget(){};
  public slots:
void getSliderValueAndQuit();
  private:
 QPushButton *quit;
 QSlider *slider;
 };

 #endif // MYWIDGET_H

myWidget.cpp

 #include "mywidget.h"
 #include <QWidget>
 #include <QObject>
 #include <QApplication>
 #include <QFont>
 #include <QLCDNumber>
 #include <QPushButton>
 #include <QSlider>
 #include <QVBoxLayout>

 MyWidget::MyWidget(QWidget *parent) :
 QWidget(parent)
 {
quit = new QPushButton(tr("Quit"));
quit->setFont(QFont("Times", 18, QFont::Bold));

QLCDNumber *lcd = new QLCDNumber(3);
lcd->setSegmentStyle(QLCDNumber::Flat);

slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 999);
slider->setValue(0);

connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

connect(slider, SIGNAL(valueChanged(int)),lcd, SLOT(display(int)));
connect(slider,SIGNAL(sliderReleased()), SLOT(getSliderValueAndQuit()));

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(quit);
layout->addWidget(lcd);
layout->addWidget(slider);
setLayout(layout);
 }

 void MyWidget::getSliderValueAndQuit(){
if(slider->value() == slider->maximum())
    close();
 }
Kms254