Results 1 to 5 of 5

Thread: Slider Value always return 0

  1. #1
    Join Date
    Mar 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Slider Value always return 0

    hi,

    I'm trying to make a simple program that gets integers from sliders,
    and compute them.

    A sample of my code :

    data.h
    Qt Code:
    1. #include <QtGUI>
    2. #include <QObject>
    3.  
    4. #ifndef DATA_H
    5. #define DATA_H
    6. class Data : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Data(QWidget *parent = 0);
    12.  
    13. public slots:
    14. void doSum();
    15.  
    16. private:
    17. int Z0RealSliderValue;
    18. int ZLRealSliderValue;
    19.  
    20. QLineEdit *Product;
    21.  
    22. };
    23.  
    24. #endif // DATA_H
    To copy to clipboard, switch view to plain text mode 

    data.cpp
    Qt Code:
    1. #include <QtGUI>
    2. #include "data.h"
    3.  
    4. Data::Data(QWidget *parent)
    5. {
    6.  
    7. QSlider *Z0RealInput = new QSlider(Qt::Horizontal, this);
    8. Z0RealInput->setRange(0, 150);
    9.  
    10. QSlider *ZLRealInput = new QSlider(Qt::Horizontal, this);
    11. ZLRealInput->setRange(0, 150);
    12.  
    13. Z0RealSliderValue = Z0RealInput->value();
    14. ZLRealSliderValue = ZLRealInput->value();
    15.  
    16. Product = new QLineEdit("Sum");
    17. Product->setReadOnly(true);
    18.  
    19. QPushButton *Plot = new QPushButton(tr("Plot"),this);
    20. connect(Plot, SIGNAL(clicked()), this, SLOT(doSum()));
    21.  
    22. //layout etc etc
    23. ....
    24.  
    25. }
    26.  
    27. void Data::doSum()
    28. {
    29. int Sum = 0;
    30.  
    31. Sum = ZLRealSliderValue + Z0RealSliderValue;
    32.  
    33. Product->setText(QString::number(Sum));
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 

    It kept returning 0.

    I have tried to change setting the lineedit text with a constant string, and it worked,
    so I assume there is nothing wrong with the connection.

    Is it how I retrieve the value which make the code not working?

    Thanks in advance.

  2. #2
    Join Date
    Jul 2011
    Location
    Brasil
    Posts
    39
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slider Value always return 0

    Hi,

    The problem is that you are getting the values before the user can interact with the controls.
    You can remove the variables ZLRealSliderValue and Z0RealSliderValue (which you are initializing with the values from just created sliders) and put the QSlider::value() from each in the function doSum instead of the variables...

    HTH.

  3. #3
    Join Date
    Mar 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slider Value always return 0

    Thanks!!

    It worked perfectly
    Didn't realize that my previous code will ignore the interactions to the sliders.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Slider Value always return 0

    Your previous code was not ignoring interactions with the sliders. The way it was written there could not possible have been interaction with the sliders between when you created the widget and when you read the value. After that, you never attempted to read the values again.

  5. #5
    Join Date
    Mar 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slider Value always return 0

    Yes, I have changed my code to

    data.h
    Qt Code:
    1. #include <QtGUI>
    2. #include <QObject>
    3.  
    4. #ifndef DATA_H
    5. #define DATA_H
    6. class Data : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Data(QWidget *parent = 0);
    12.  
    13. public slots:
    14. void doSum();
    15.  
    16. private:
    17.  
    18. QSlider *Z0RealInput;
    19. QSlider *ZLRealInput;
    20.  
    21. int Z0RealSliderValue;
    22. int ZLRealSliderValue;
    23.  
    24. QLineEdit *Product;
    25.  
    26. };
    27.  
    28. #endif // DATA_H
    To copy to clipboard, switch view to plain text mode 

    data.cpp
    Qt Code:
    1. #include <QtGUI>
    2. #include "data.h"
    3.  
    4. Data::Data(QWidget *parent)
    5. {
    6.  
    7. Z0RealInput = new QSlider(Qt::Horizontal, this);
    8. Z0RealInput->setRange(0, 150);
    9.  
    10. ZLRealInput = new QSlider(Qt::Horizontal, this);
    11. ZLRealInput->setRange(0, 150);
    12.  
    13. Product = new QLineEdit("Sum");
    14. Product->setReadOnly(true);
    15.  
    16. QPushButton *Plot = new QPushButton(tr("Plot"),this);
    17. connect(Plot, SIGNAL(clicked()), this, SLOT(doSum()));
    18.  
    19. //layout etc etc
    20. ....
    21.  
    22. }
    23.  
    24. void Data::doSum()
    25. {
    26. int Sum = 0;
    27.  
    28. ZLRealSliderValue = ZLRealInput->value();
    29. Z0RealSliderValue = Z0RealInput->value();
    30.  
    31. Sum = ZLRealSliderValue + Z0RealSliderValue;
    32.  
    33. Product->setText(QString::number(Sum));
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 

    And it's working now
    Thanks

Similar Threads

  1. Double slider
    By whitefurrows in forum Qt Programming
    Replies: 8
    Last Post: 23rd February 2011, 15:29
  2. help on my slider
    By newb in forum Qt Programming
    Replies: 1
    Last Post: 13th July 2010, 15:36
  3. Replies: 2
    Last Post: 21st March 2010, 09:01
  4. vertical slider
    By TomASS in forum Newbie
    Replies: 4
    Last Post: 29th November 2009, 00:30
  5. get slider value
    By eric in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2007, 19:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.