Results 1 to 10 of 10

Thread: Connection a QVector<QSlider *> adequately

  1. #1
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Connection a QVector<QSlider *> adequately

    Hi!

    I have:
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyWidget();
    7. ~MyWidget();
    8. public slots:
    9. void adequate_function(int slider_id, int value);
    10. private:
    11. QVector<QSlider*> m_sliders;
    12. }
    To copy to clipboard, switch view to plain text mode 
    and my constructor looks like
    Qt Code:
    1. MyWidget::MyWidget()
    2. {
    3. for (int i=0 ; i<5 ; i++)
    4. {
    5. m_sliders.append(QSlider(Qt::Horizontal, this));
    6. m_sliders[i].setRange(0,100);
    7. m_sliders[i].setPageStep(20);
    8.  
    9. //this does not work but explains what I like
    10. QObject::connect( m_sliders[i], SIGNAL(valueChanged(int)),
    11. this, SLOT(setDeformationFactor( i , int) ) );
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    so, I would like to call a function and give the slider id as argument.

    Regards,Olli

  2. #2
    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: Connection a QVector<QSlider *> adequately

    it has to be
    Qt Code:
    1. m_sliders.append(new QSlider(Qt::Horizontal, this));
    To copy to clipboard, switch view to plain text mode 
    and what is "i" in
    Qt Code:
    1. QObject::connect( m_sliders[i], SIGNAL(valueChanged(int)), this, SLOT(setDeformationFactor( i , int) ) );
    To copy to clipboard, switch view to plain text mode 
    ?
    The fist argument of setDeformationFactor has to be int and the second must have a default value.

    EDIT: If you want to know which slider has called the slot use sender().

  3. #3
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Connection a QVector<QSlider *> adequately

    I see a few problems.

    1. You aren't adding pointers to sliders to your vector, you are adding objects created on the stack. Unless my c++ is failing me this code won't compile as is.

    2. The slot name in your header does not match the slot name you are connecting to in the constructor. Is that just a typo?

    3. The connection is not right. I see what you want to do by sending the "i" slider to the slot, but it doesn't work that way. The signal itself must emit the value. You have a couple options. There is a class called QSignalMapper. You could connect your sliders to that and it will emit a signal called mapped(int). Here is a short example from some code I wrote to do something similar:

    Qt Code:
    1. //in this example, m_effect_sliders is a QList<QSlider *>
    2. QSignalMapper * effect_slider_mapper = new QSignalMapper(this);
    3. for(int i = 0; i < 5; i++)
    4. {
    5. connect(m_effect_sliders.at(i), SIGNAL(valueChanged(int)), effect_slider_mapper, SLOT(map()));
    6. effect_slider_mapper->setMapping(m_effect_sliders.at(i), i);
    7. }
    8. connect(effect_slider_mapper, SIGNAL(mapped(int)), this, SLOT(updateEffectsValue(int)));
    To copy to clipboard, switch view to plain text mode 
    Last edited by JimDaniel; 16th March 2009 at 13:49.

  4. The following user says thank you to JimDaniel for this useful post:

    olidem (16th March 2009)

  5. #4
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connection a QVector<QSlider *> adequately

    Well, "i" is the index of the slider that has been changed.
    I want to solve elegantly the problem of handling each slider in a similar way:
    If I had eg 5 methods
    Qt Code:
    1. public slots:
    2. void adequate_function0(int value);
    3. void adequate_function1(int value);
    4. void adequate_function2(int value);
    5. void adequate_function3(int value);
    6. void adequate_function4(int value);
    To copy to clipboard, switch view to plain text mode 
    I could do something similar.

  6. #5
    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: Connection a QVector<QSlider *> adequately

    Quote Originally Posted by olidem View Post
    Well, "i" is the index of the slider that has been changed.
    You can't pass values that way!

    I want to solve elegantly the problem of handling each slider in a similar way:
    If I had eg 5 methods
    Qt Code:
    1. public slots:
    2. void adequate_function0(int value);
    3. void adequate_function1(int value);
    4. void adequate_function2(int value);
    5. void adequate_function3(int value);
    6. void adequate_function4(int value);
    To copy to clipboard, switch view to plain text mode 
    I could do something similar.
    Use a signal mapper as JimDaniel has suggested or sender() in your slot to get the sender.

  7. #6
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connection a QVector<QSlider *> adequately

    Quote Originally Posted by JimDaniel View Post
    3. The connection is not right. I see what you want to do by sending the "i" slider to the slot, but it doesn't work that way. The signal itself must emit the value. You have a couple options. There is a class called QSignalMapper. You could connect your sliders to that and it will emit a signal called mapped(int). Here is a short example from some code I wrote to do something similar:

    Qt Code:
    1. //in this example, m_effect_sliders is a QList<QSlider *>
    2. QSignalMapper * effect_slider_mapper = new QSignalMapper(this);
    3. for(int i = 0; i < 5; i++)
    4. {
    5. connect(m_effect_sliders.at(i), SIGNAL(valueChanged(int)), effect_slider_mapper, SLOT(map()));
    6. effect_slider_mapper->setMapping(m_effect_sliders.at(i), i);
    7. }
    8. connect(effect_slider_mapper, SIGNAL(mapped(int)), this, SLOT(updateEffectsValue(int)));
    To copy to clipboard, switch view to plain text mode 
    Yeah! This look great!
    But I don't see where the actual new value of the slider arives at the function. Do I have to request it from the concerned slider again manually?

  8. #7
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connection a QVector<QSlider *> adequately

    I found another solution:

    Qt Code:
    1. class NiceSlider : public QSlider
    2. {
    3. Q_OBJECT
    4. public:
    5. NiceSlider(int id, Qt::Orientation orientation, QWidget * parent);
    6.  
    7. signals:
    8. void valueChanged( int value, int id);
    9.  
    10. protected:
    11. void sliderChange(SliderChange c)
    12. {
    13. if(c == QAbstractSlider::SliderValueChange)
    14. emit valueChanged( this->value(), _id );
    15.  
    16. QSlider::sliderChange(c);
    17. };
    18.  
    19. private:
    20. int _id;
    21. };
    To copy to clipboard, switch view to plain text mode 
    and in the constructor from earlier:
    Qt Code:
    1. MyWidget::MyWidget()
    2. {
    3. for (int i=0 ; i<5 ; i++)
    4. {
    5. m_sliders.append( new NiceSlider ( i, Qt::Horizontal, this) );
    6. QObject::connect( m_sliders[i], SIGNAL(valueChanged(int,int)),
    7. &m_view, SLOT(setDeformationFactor(int,int)));
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    NICE, isn't it ?

    Thank you all a lot!

    Best regards,
    Olli

  9. #8
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Connection a QVector<QSlider *> adequately

    EDIT: The above subclassing method also works. I've done that before too, in certain scenarios. For your problem I would say using a signal mapper is still the cleaner method, unless you have other reasons to create a subclass.

    Yes, with the method I showed you need to manually grab the value from the list of QSliders, but it is very straightforward because you have the index. So you could do this inside your setDeformationFactor(int):

    Qt Code:
    1. setDeformationFactor(int index)
    2. {
    3. int new_slider_value = m_sliders[index]->value();
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by JimDaniel; 16th March 2009 at 20:25.

  10. #9
    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: Connection a QVector<QSlider *> adequately

    Quote Originally Posted by olidem View Post
    NICE, isn't it ?
    For your special need, yes. But think if you want to add a new slider while the program is running, you have to know which is the new id. And now think about dynamic adding and removing things became complicated and can rapid lead to a bug...

  11. #10
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Connection a QVector<QSlider *> adequately

    Yes, in principle I agree with you.
    But in my special case, I will remove all sliders and add completely new ones from time to time. So there will never appear the case where I already use n sliders and add an n+1th afterwards.

    Thanks for your great support and help!

    With best regards,
    Olli

Similar Threads

  1. SQL connection closure problem.
    By cbarmpar in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2008, 08:42
  2. Client/Server Error: BadIDChoice
    By 3nc31 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 10:22
  3. Replies: 3
    Last Post: 2nd August 2007, 21:28
  4. How do I keep the client connection open ?
    By probine in forum Newbie
    Replies: 2
    Last Post: 25th March 2006, 19:06
  5. Can I launch a dial-up connection in Windows?
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 9th February 2006, 12:32

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.