Results 1 to 7 of 7

Thread: Signal and Slot -changing value of double or integer

  1. #1
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Signal and Slot -changing value of double or integer

    Hello,

    I am new to QT and I just wanted to ask if there is some way of changing the value of an integer as a slot when your signal is a QSpinBox

    Basically I am trying to do something like this:

    int rollangle ;
    QSpinBox *spinrotate = new QSpinBox;
    spinrotate->setRange(0,180);
    QObject::connect(spinrotate,SIGNAL(valueChanged(in t)),rollangle,SLOT(setValue(int)));

    Off course I realize this is not how its supposed to be done since, as far as I know, both signal and slot should be of the same type.

    But I need the integer rollangle to change with spinrotate value.
    Because I am implementing a Qtransform::rotate function after this and i want the rotation to change every time spinrotate is changed

    I would be really grateful if someone can help me with this!

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Signal and Slot -changing value of double or integer

    Primitives like int do not have slots. All you need to do is connect the spinbox signal to a function that updates the value of rollangle:

    Qt Code:
    1. connect(spinrotate, SIGNAL(valueChanged(int), this, SLOT(updateRollAngle(int));
    2. ...
    3. void YourClass::updaterollAngle(int i)
    4. {
    5. rollangle = i;
    6. }
    To copy to clipboard, switch view to plain text mode 

    By the way, you are sort of correct with regards to
    both signal and slot should be of the same type
    They have to have the same parameters, or, failing that, the signal may have more parameters in which case the extra ones are ignored by the slot. However, even in that case the parameters that are not ignored by the slot must be the same as those in the slot.

  3. The following user says thank you to Urthas for this useful post:

    samee999 (22nd August 2010)

  4. #3
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Wink Re: Signal and Slot -changing value of double or integer

    thanx a lot for ur post,

    I am trying to do as u pointed out. so this is my code:
    class faith1
    {
    public:
    void updaterollAngle(int);
    private:

    };
    void faith1::updaterollAngle(int i)
    {
    int rollangle;
    rollangle = i;

    }

    in the int main function i do this :


    faith1 andy;
    QSpinBox *spinrotate = new QSpinBox;
    spinrotate->setRange(0,180);

    connect(spinrotate,SIGNAL(valueChanged(int)),this, SLOT(andy.updaterollAngle(int)));

    this is off course not the complete code, but i get the following bug:

    "invalid use of 'this' in non-member function"


    i am sorry if there is a very lame reason that this is happening, (there must be), but i dont know exactly how to use the 'this' pointer. hope somebody can help,

    p.s. i know , i suck at programming

  5. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Signal and Slot -changing value of double or integer

    Read at least first half of the following link *thoroughly*: http://doc.trolltech.com/4.6/signalsandslots.html

    When you are done, you will know how to structure the faith1 header file for what you want to do. Then, remove the rollangle declaration from your updaterollAngle() code. It should be a faith1 instance variable, declared privately in the header. In your connect statement, replace "this" with "&andy".

    update: andy became &andy per Lykurg's good eye
    Last edited by Urthas; 20th August 2010 at 22:01.

  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: Signal and Slot -changing value of double or integer

    Please read the documentation for signals and slots. You are missing the slot keyword as well as the Q_OBJECT macro. Further you want replace this with &andy.

  7. #6
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal and Slot -changing value of double or integer

    Hello,

    thanx a lot for your reply. I think i managed to figure it out. but now i have another issue, jus like i said i would like to use the rollangle variable to allow me to perform real time rotation of a dial.

    While the variable rollangle does change since i call signal slot, for its update, the call to the function transform.rotate(andy.myvalue) is not called everytime there is a change in rollangle, so obviously the dial does nt rotate real time in this way. does any1 v an idea of how i can call transform.rotate() using a signal slot method so that it is updated everytime the variable rollangle changes?
    if there is any other way i can perform this, that is off course also quite welcome

    btw , myvalue is a public function in my class:

    int myvalue() const { return rollangle; }

    (andy is the name of the class instance)

  8. #7
    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: Signal and Slot -changing value of double or integer

    You can use it for real time operations. you just have to call an update function inside the slot. Or use a setter function in your class which performs an update if the value gets changed.

  9. The following user says thank you to Lykurg for this useful post:

    samee999 (22nd August 2010)

Similar Threads

  1. Signal connected to slot (or signal)
    By Althor in forum Newbie
    Replies: 2
    Last Post: 6th July 2010, 10:00
  2. Passing an integer to a slot
    By bizmopeen in forum Newbie
    Replies: 6
    Last Post: 30th October 2009, 09:51
  3. Replies: 1
    Last Post: 8th November 2007, 17:11
  4. QTreeWidget double click signal
    By Pinco Pallino in forum Newbie
    Replies: 2
    Last Post: 18th November 2006, 16:37
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

Tags for this Thread

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.