Results 1 to 7 of 7

Thread: linking a slider to a global variable

  1. #1
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default linking a slider to a global variable

    Hi guys,

    I have successfully connected a slider and a spinBox so that if i change the value in one the other will update, however i dont want to connect the spinBox to the slider, i actually link the slider to a global variable so that when the slider changes so will the value in the variable.

    My code to connect a spinBox and a Slider is below:

    QObject::connect(spinBox_saturation, SIGNAL(valueChanged(int)), horzSlider_saturation, SLOT(setValue(int)));

    And the variable i want to connect to this slider is

    int output;

  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: linking a slider to a global variable

    your class as getSliderValue()

    Qt Code:
    1. private slots:
    2. void valueReceived(int);
    3.  
    4. connect(horzSlider_saturation, SIGNAL(valueChanged(int)), getSliderValue, SLOT(valueReceived(int));
    5.  
    6.  
    7. void getSliderValue::valueReceived(int output)
    8. {
    9. //output will have the value now
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wagmare; 29th July 2009 at 13:51. Reason: modified to slider
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: linking a slider to a global variable

    Hi,
    Thanks for your solution but i am having problems implimenting it. I don't understand why i need to create a seperate class to handle passing the variable.

    Is it not possible to just pass the variable as it stands, i have tried this idea myself and whereas the code compiles correctly the values do not seem to be passing from the slider to the variable.

    My idea:

    QObject::connect(horzSlider_hueMax, SIGNAL(sliderMoved(int)), this, SLOT(setValue(hMax)));

  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: linking a slider to a global variable

    code will compile but in runtime it will show in the console
    Object::connect: No such slot Test::setValue(hMax)
    if u try to connect a SIGNAL .. it should be a SLOT and not a variable or function ..

    SLOT may be static like close(), quit(), etc..
    also user defined like setValue(int)

    and another thing ... u cannot connect a SIGNAL(int) to SLOT(hMax)

    a SIGNAL(int) can be connect to SLOT(int) or SLOT(void)
    but not to SLOT(QString&) or SLOT(hMAx)
    "Behind every great fortune lies a crime" - Balzac

  5. The following user says thank you to wagmare for this useful post:

    switch (4th August 2009)

  6. #5
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: linking a slider to a global variable

    Hi wagmare, thanks for explaining your solution it works fine.

    However i came up with another method in the mean time.

    I realised that the spinBox has a function called value() which allows you to access the current value contained in the box, so using this function and by linking the slider to a spin box i managed to connect the global variable to my slider.

    int hMax;
    hMax = spinBox_hueMax->value();


    I am sure you can use the same technique to connect a slider directly to the variable.

    int hMax;
    hMax = slider_hueMax->value();

  7. #6
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    509
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: linking a slider to a global variable

    Hi, this way your variable will not be updated if the slider changes. If you need this, use wagmare's example and connect the slider to a slot.

    Ginsengelf

  8. #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: linking a slider to a global variable

    thanks ginsengelf ..
    look
    Qt Code:
    1. int hMax;
    2. hMax = slider_hueMax->value();
    To copy to clipboard, switch view to plain text mode 

    hMax will get the value only once when you call the above line .. if u want to test it every second u can use QTimer to check the value every second of the QSlider

    but connect() will return the value whenver the user change or Qt change the QSlider value and that value is readily available in hMax .

    every time the QSlider is called update() .. the connect respond with the QSlider signal emission which we can get it from SLOT()
    "Behind every great fortune lies a crime" - Balzac

Similar Threads

  1. how to declare a global variable right
    By Cruz in forum Newbie
    Replies: 13
    Last Post: 16th February 2010, 16:25
  2. nmake error during .pro compiling
    By mattia in forum Installation and Deployment
    Replies: 5
    Last Post: 18th June 2008, 10:15
  3. Link QVariant to global variable
    By jobrandt in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2007, 10:18
  4. global variable
    By Shuchi Agrawal in forum General Programming
    Replies: 10
    Last Post: 15th February 2007, 04:19

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.