Results 1 to 4 of 4

Thread: Signals and Slots

  1. #1
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Signals and Slots

    Hi

    I created a variable int c,whose value is to be updated through a slider, taking the value of the slider. The variable c will be then converted to a Qstring and output as the text of a QPushButton. I used the following code in the main.cpp. However, it does not work, since the button always displays the initial value of the slider, which suggests that the variable c is not being updated.



    QSlider *slider = new QSlider(Qt::Horizontal);
    slider->setRange(0, 130);

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

    int Contador = slider->value();
    QPushButton *butao = new QPushButton;
    butao->setText(QString::number(Contador));


    Any suggestions why this is not working?

    Cheers
    Last edited by Maluko_Da_Tola; 28th July 2010 at 00:32.

  2. #2
    Join Date
    Jul 2010
    Posts
    72
    Thanks
    35
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals and Slots

    Sorry,The variable int c is defined as int Contador instead!

    Cheers
    Last edited by Maluko_Da_Tola; 28th July 2010 at 00:33.

  3. #3
    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: Signals and Slots

    Quote Originally Posted by Maluko_Da_Tola View Post
    Hi
    Qt Code:
    1. QObject::connect(slider, SIGNAL(valueChanged(int)),
    2. slider, SLOT(setValue(int)));
    To copy to clipboard, switch view to plain text mode 
    This is almost certainly not what you wanted to do: setting the slider's value every time the slider changes.

    If the code fragment you posted appears as one chunk of continuous code then you have bigger problems. Can you post a small, complete program?

  4. #4
    Join Date
    May 2010
    Posts
    30
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signals and Slots

    You'll want to add this to your *.h file (replace MyClass with your class name):
    Qt Code:
    1. MyClass {
    2. public:
    3. ...
    4. private:
    5. int c;
    6. private slots:
    7. void setVal(int);
    8. }
    To copy to clipboard, switch view to plain text mode 

    implement the slot as follows:
    Qt Code:
    1. void MyClass::setVal(int value)
    2. {
    3. c = value;
    4. }
    To copy to clipboard, switch view to plain text mode 

    finally, change your connection to this:

    Qt Code:
    1. QObject::connect(slider, SIGNAL(valueChanged(int)), this, SLOT(setVal(int)));
    To copy to clipboard, switch view to plain text mode 

    That ought to get you started

Similar Threads

  1. Signals & Slots!
    By qtoptus in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2010, 01:50
  2. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  3. Signals and slots
    By sylvarant in forum Newbie
    Replies: 4
    Last Post: 11th September 2007, 15:48
  4. Signals and Slots
    By merry in forum Qt Programming
    Replies: 4
    Last Post: 22nd February 2007, 08:11
  5. Signals and Slots in dll
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2006, 08:12

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.