Results 1 to 14 of 14

Thread: how to make a button resets a slider to 0?

  1. #1
    Join Date
    May 2009
    Location
    Poland
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Smile how to make a button resets a slider to 0?

    hello ^_^
    i have been programming in QT since yesterday and i got a question how do i reset slider to 0?

    void setValue ( int )
    Changing the value also changes the sliderPosition.

    but it didnt work

    connect(resetButton, SIGNAL(clicked() ), slider, SLOT(setValue(0) ) );

    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QPushButton>
    4. #include <QLCDNumber>
    5. #include <QSlider>
    6. #include <QVBoxLayout>
    7.  
    8. #include <iostream>
    9.  
    10. class MyWidget : public QWidget
    11. {
    12. public:
    13. MyWidget(QWidget *parent);
    14. ~MyWidget() { std::cout << "abc" << std::endl; }
    15. };
    16.  
    17. MyWidget::MyWidget(QWidget *parent = 0) : QWidget(parent)
    18. {
    19. setFixedSize(320, 200);
    20.  
    21. QPushButton *resetButton = new QPushButton("Reset");
    22.  
    23. QLCDNumber *lcd = new QLCDNumber(2);
    24. lcd->setSegmentStyle(QLCDNumber::Filled);
    25.  
    26. QSlider *slider = new QSlider(Qt::Horizontal);
    27. slider->setValue(0);
    28. slider->setRange(0, 99);
    29.  
    30. connect(resetButton, SIGNAL(clicked() ), slider, SLOT(setValue(0) ) );
    31. connect(slider, SIGNAL(valueChanged(int) ), lcd, SLOT(display(int) ) );
    32.  
    33. QVBoxLayout *layout = new QVBoxLayout();
    34. layout->addWidget(resetButton);
    35. layout->addWidget(lcd);
    36. layout->addWidget(slider);
    37. setLayout(layout);
    38. }
    39.  
    40. int main(int argc, char *argv[] )
    41. {
    42. QApplication app(argc, argv);
    43.  
    44. MyWidget widget;
    45. widget.show();
    46.  
    47. app.exec();
    48. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to make a button resets a slider to 0?

    Quote Originally Posted by lunar View Post
    Qt Code:
    1. connect(resetButton, SIGNAL(clicked() ), slider, SLOT(setValue(0) ) );
    2. connect(slider, SIGNAL(valueChanged(int) ), lcd, SLOT(display(int) ) );
    To copy to clipboard, switch view to plain text mode 
    u cannot connect a void SIGNAL to int SLOT
    clicked() ->setValue(0) connection is not possible .. but reverse is possible ..

    so create a private slot
    Qt Code:
    1. private slot:
    2. void sliderValue();
    3.  
    4. connect(resetButton, SIGNAL(clicked()), this, SLOT(sliderValue()));
    5.  
    6. MyWidget::sliderValue()
    7. {
    8. slider->setValue(0);
    9. }
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    May 2009
    Location
    Poland
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to make a button resets a slider to 0?

    hey thx for help but iam running a little error:

    In member function `void MyWidget::SetSliderValue()':|
    error: `slider' was not declared in this scope|
    warning: unused variable 'slider'|
    ||=== Build finished: 1 errors, 1 warnings ===|

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent);
    5. ~MyWidget() { std::cout << "abc" << std::endl; }
    6.  
    7. private slots:
    8. void SetSliderValue() { slider->setValue(0); }
    9. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. connect(resetButton, SIGNAL(clicked() ), this, SLOT(SetSliderValue() ) );
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to make a button resets a slider to 0?

    Quote Originally Posted by lunar View Post
    hey thx for help but iam running a little error:



    Qt Code:
    1. connect(resetButton, SIGNAL(clicked() ), this, SLOT(SetSliderValue() ) );
    To copy to clipboard, switch view to plain text mode 

    declare slider in private (global)
    Qt Code:
    1. private :
    2. QSlider *slider;
    To copy to clipboard, switch view to plain text mode 


    use it like this
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent);
    5. ~MyWidget() { std::cout << "abc" << std::endl; }
    6.  
    7.  
    8. private slots:
    9. void SetSliderValue() { slider->setValue(0); }
    10.  
    11. private:
    12. QSlider *slider;
    13. };
    To copy to clipboard, switch view to plain text mode 

    otherwise u cant use it in separate function of the class ...
    "Behind every great fortune lies a crime" - Balzac

  5. #5
    Join Date
    May 2009
    Location
    Poland
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to make a button resets a slider to 0?

    damn didnt work



    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QPushButton>
    4. #include <QLCDNumber>
    5. #include <QSlider>
    6. #include <QVBoxLayout>
    7.  
    8. #include <iostream>
    9.  
    10. class MyWidget : public QWidget
    11. {
    12. public:
    13. MyWidget(QWidget *parent);
    14. ~MyWidget() { std::cout << "abc" << std::endl; }
    15.  
    16. private:
    17. QSlider *slider;
    18.  
    19. private slots:
    20. void SetSliderValue() { slider->setValue(0); }
    21. };
    22.  
    23. MyWidget::MyWidget(QWidget *parent = 0) : QWidget(parent)
    24. {
    25. setFixedSize(320, 200);
    26.  
    27. QPushButton *resetButton = new QPushButton("Reset");
    28.  
    29. QLCDNumber *lcd = new QLCDNumber(2);
    30. lcd->setSegmentStyle(QLCDNumber::Filled);
    31.  
    32. QSlider *slider = new QSlider(Qt::Horizontal);
    33. slider->setValue(0);
    34. slider->setRange(0, 99);
    35.  
    36. connect(resetButton, SIGNAL(clicked() ), this, SLOT(SetSliderValue() ) );
    37. connect(slider, SIGNAL(valueChanged(int) ), lcd, SLOT(display(int) ) );
    38.  
    39. QVBoxLayout *layout = new QVBoxLayout();
    40. layout->addWidget(resetButton);
    41. layout->addWidget(lcd);
    42. layout->addWidget(slider);
    43. setLayout(layout);
    44. }
    45.  
    46. int main(int argc, char *argv[] )
    47. {
    48. QApplication app(argc, argv);
    49.  
    50. MyWidget widget;
    51. widget.show();
    52.  
    53. app.exec();
    54. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to make a button resets a slider to 0?

    relax ................ u are new ...


    change this ..
    QSlider *slider = new QSlider(Qt::Horizontal);
    to
    slider = new QSlider(Qt::Horizontal);

    because u have already defined it in private:

    private:
    QSlider *slider;
    "Behind every great fortune lies a crime" - Balzac

  7. #7
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to make a button resets a slider to 0?

    i think u need the macro Q_OBJECT to connect ... it is giving error n connect .. how stupid i am not to mention it earlier ..
    "Behind every great fortune lies a crime" - Balzac

  8. #8
    Join Date
    May 2009
    Location
    Poland
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to make a button resets a slider to 0?

    hmm didnt work i think something is wrong with the button

    i have also added #include <QObject> and Q_OBJECT yesterday but didnt work
    QObject: No such file or directory|
    ||=== Build finished: 1 errors, 0 warnings ===|


    what about " emit "

    Qt Code:
    1. void Counter::setValue(int value)
    2. {
    3. if (value != m_value) {
    4. m_value = value;
    5. emit valueChanged(value);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by lunar; 20th May 2009 at 15:57.

  9. #9
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to make a button resets a slider to 0?

    see the code i attached with this thread ... i cant set the setValue(0) to slider .... its giving seg fault ...
    Attached Files Attached Files
    "Behind every great fortune lies a crime" - Balzac

  10. #10
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to make a button resets a slider to 0?

    yipeeee i completed this one .... its working .. your logic is right .. use this attachment
    Attached Files Attached Files
    "Behind every great fortune lies a crime" - Balzac

  11. #11
    Join Date
    May 2009
    Location
    Poland
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to make a button resets a slider to 0?

    i believe it will work but:

    |7|QObject: No such file or directory|
    ||=== Build finished: 1 errors, 0 warnings ===|

    looks like its gonna be more complicated than WinApi
    do u know what mistake does show this error?

  12. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to make a button resets a slider to 0?

    Quote Originally Posted by lunar View Post
    |7|QObject: No such file or directory|
    ||=== Build finished: 1 errors, 0 warnings ===|
    May you have to explicit add a #include <QObject> in the header file or more likely clean your directory before you build the new files.

  13. #13
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to make a button resets a slider to 0?

    which os u are using .. i compiled it in linux .... ok make clean then give make ...
    else qmake again the .pro file ...

    did u see what are the error u done .. ? in the code ...
    "Behind every great fortune lies a crime" - Balzac

  14. #14
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to make a button resets a slider to 0?

    FYI- it worked for me, too. WinXP, Visual Studio 2005 Command Prompt:

    C:\prompt> qmake
    C:\prompt> nmake release

Similar Threads

  1. How to make dropdown button bigger
    By Vadi in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2008, 00:54
  2. Window OS make distclean && qmake && make one line
    By patrik08 in forum General Programming
    Replies: 4
    Last Post: 22nd March 2007, 11:43
  3. Compiling with Qmake/Make
    By VireX in forum Newbie
    Replies: 25
    Last Post: 22nd February 2007, 06:57

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.