Results 1 to 5 of 5

Thread: How to Route a Horizontal Slider Value to My Code?

  1. #1

    Default How to Route a Horizontal Slider Value to My Code?

    Hello, apologies for being a newb, and I hope my question makes sense. I am just starting to learn c++ and qt, and I'm working on a simple midi controlled oscillator with gui using the Synthesis Toolkit (mostly by copying and pasting code). I've run into a snag and I was hoping someone could help me.

    I have a (working) horizontal slider and the rest of the code is working too in another Qthread. So everything works, the slider displays, the oscillator makes sound and accepts midi messages. The problem is that I want to work the value of the slider into the code, and I don't know how to do that. The applicable parts of the code are gain.h and gain.cpp, which contain the slider code, and also sine.cpp, which I want to somehow make use of gain.h/gain.cpp. Here is the code, which again I copied and pasted from the internet to get a working slider:

    Qt Code:
    1. #ifndef LCDRANGE_H
    2. #define LCDRANGE_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QSlider;
    7.  
    8. class Gain : public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. Gain(QWidget *parent = 0);
    14.  
    15. int value() const;
    16.  
    17. public slots:
    18. void setValue(int value);
    19.  
    20. signals:
    21. void valueChanged(int newValue);
    22.  
    23. private:
    24. QSlider *slider;
    25. };
    26.  
    27. #endif
    To copy to clipboard, switch view to plain text mode 

    And here is gain.cpp:

    Qt Code:
    1. #include <QSlider>
    2. #include <QVBoxLayout>
    3. #include "gain.h"
    4.  
    5. Gain::Gain(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8.  
    9. slider = new QSlider(Qt::Horizontal);
    10. slider->setRange(1, 100);
    11. slider->setValue(50);
    12.  
    13. connect(slider, SIGNAL(valueChanged(int)),
    14. this, SIGNAL(valueChanged(int)));
    15.  
    16.  
    17. QVBoxLayout *layout = new QVBoxLayout;
    18. layout->addWidget(slider);
    19. setLayout(layout);
    20. }
    21.  
    22. int Gain::value() const
    23. {
    24. return slider->value();
    25. }
    26.  
    27. void Gain::setValue(int value)
    28. {
    29. slider->setValue(value);
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    Now how to route it to my other code? I specifically want to take the value of the horizontal slider, convert it to a value between 0 and 1, and multiply the value of my output so that it functions as a volume control for the oscillator. Every attempt I have made so far to add any reference to the horizontal slider in my oscillator code results in the code not compiling, the gui not displaying, the oscillator making no sound, or an error about "must create application before qpaint" something-or-other.

    Thanks in advance for your time and help.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to Route a Horizontal Slider Value to My Code?

    hard to tell without knowing how you instantiate Gain and how you tried to connect it.

    Btw, if you want to convert the value to be in the floating point range of 0 to 1 I would do that right there in Gain where you have access to the slider's range.
    I.e. Gain::value(), setValue() and valueChanged() would all deal with the 0..1 range, which they then map to the actual range of the slider.

    This allows you to change the "precision" at any time just by changing the slider's range.

    Cheers,
    _

  3. #3

    Default Re: How to Route a Horizontal Slider Value to My Code?

    Thanks for the reply.

    Well, for instance, in sine.cpp I tried this:

    Qt Code:
    1. int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status, void *dataPointer )
    2. {
    3. Gain *volume = new volume;
    4.  
    5. SineWave *sine = (SineWave *) dataPointer;
    6. register StkFloat *samples = (StkFloat *) outputBuffer;
    7. for ( unsigned int i=0; i<nBufferFrames; i++ )
    8. *samples++ = (sine->tick() * vel) * volume->valueChanged() / 100;
    9.  
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 

    This results in a number of errors including "no matching function for call to 'Gain::valueChanged()'" and "cannot convert 'int*' to 'gain*' in initialization". So I'm asking a very general question. What is a good way to instantiate Gain in the first place in any c++ code? Any ideas or links to examples? I can't seem to find anything useful about this on the internet, probably because I don't know enough to google the right questions.

  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: How to Route a Horizontal Slider Value to My Code?

    The "no matching function" error arises because valueChanged() is a signal and its generated code is declared in the protected section of the class. If you want the value from the slider object directly then you want the value() function, not the signal indicating it has changed.

    I assume that the "cannot convert 'int*' to 'gain*' in initialization" relates to this line:
    Qt Code:
    1. Gain *volume = new volume;
    To copy to clipboard, switch view to plain text mode 
    The LHS of this is type "Gain*". Since there's no class called "volume" the compiler has assumed an int so the RHS is type "int*". It's not entirely clear what you are trying to achieve here with this circular reference. If, as I suspect, you intended to create a new Gain object:
    Qt Code:
    1. Gain *volume = new Gain;
    To copy to clipboard, switch view to plain text mode 
    then, had you succeeded, you would have always had value() == 50 from a slider that the user neither saw nor interacted with.

    In a normal Qt program you would have a UI built containing widgets like your slider, ideally quite separate from the code that actually does substantial stuff in response. You can use Designer or hand code that UI. You would connect() the valueChanged() signal of your slider to a slot that reacts immediately to the changed value, for example pushing a new volume figure into your generator code. Assuming the tick() routine is part of the code you have pushed to another thread you could put the receiving slot in that QObject and store the current volume figure as a member variable in the generator thread.

    Unless you want to do something like anda_skoa suggests to get a floating point or logarithmic slider then it strikes me that your Gain class could simply be a QSlider subclass, something like:
    Qt Code:
    1. class Gain: public QSlider
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit Gain(QWidget *p = 0): QSlider(Qt::Horizontal, p) {
    6. setRange(1, 100);
    7. setValue(50);
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 
    Do you want to allow a zero volume?

  5. #5

    Default Re: How to Route a Horizontal Slider Value to My Code?

    Yeah, I really meant

    Qt Code:
    1. Gain *volume = new Gain;
    To copy to clipboard, switch view to plain text mode 

    I'll look into implementing the gui with Qt Designer, since I'm still having no luck. Thanks.

Similar Threads

  1. Code for Horizontal spacer
    By Gary7 in forum Newbie
    Replies: 2
    Last Post: 15th August 2012, 04:52
  2. Using GoogleMaps to show route?
    By Terreox in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2012, 22:56
  3. Replies: 0
    Last Post: 20th February 2012, 15:31
  4. Replies: 2
    Last Post: 21st March 2010, 09:01

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.