Results 1 to 15 of 15

Thread: Getting the value from a spinbox

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Getting the value from a spinbox

    Add a new slot, for example Window::calculate(). Connect both spin boxes to that slot:
    Qt Code:
    1. connect(valueSpinBoxJ1,SIGNAL(valueChanged(int)),this,SLOT(calculate()));
    2. connect(valueSpinBoxJ2,SIGNAL(valueChanged(int)),lcd2,SLOT(calculate()));
    To copy to clipboard, switch view to plain text mode 
    In the slot, retrieve values and add them together:
    Qt Code:
    1. void Window::calculate()
    2. {
    3. int valJ1 = valueSpinBoxJ1->value();
    4. int valJ2 = valueSpinBoxJ2->value();
    5. lcd3->display(valJ1 + valJ2);
    6. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  2. #2
    Join Date
    May 2007
    Location
    Athens, Greece
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Unhappy Re: Getting the value from a spinbox

    Thanks JPN. So, I added "void calculate();" under the public slots inside window.h.

    I then added the two connections (with the correction of "lcd2" to "this", I assumed), under my "Window::Window()" inside the window.cpp and added at the end the "void Window::calculate() {.....}"

    First time run, it crashed - something not working with QLCDNumber.h it said. I changed that and instead of sending the value to an LCD, I used
    Qt Code:
    1. qDebug("Val12 = %.4f", valJ1+valJ2)
    To copy to clipboard, switch view to plain text mode 
    to check what's going on...

    In the debug window, I do get "Val12 = 0". The good part is that "Val12 = 0" reappears everytime I change something in the two spinboxes. The sad part is that it just doesn't have the correct value - stays zero. I suppose that "calculate" is done every time I change something in one of the two spinboxes, the valueChanged connection works OK, but valJ1 and valJ2 don't get the value of their spinbox from the valueSpinBoxJ1/2->value thing... which is not good at all... how can that be?

    WHAT'S WRONG!?!?!?!

    X-man

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Getting the value from a spinbox

    Oh yes, "lcd2" was a typo, sorry. There's at least a classical mix up with local and member variables. You have declared a member variable:
    Qt Code:
    1. QLCDNumber *lcdX;
    To copy to clipboard, switch view to plain text mode 
    But you are assigning to a local variable:
    Qt Code:
    1. QLCDNumber *lcdX = new QLCDNumber(X);
    To copy to clipboard, switch view to plain text mode 
    Now if you try to dereference it in another function:
    Qt Code:
    1. ldcX->something();
    To copy to clipboard, switch view to plain text mode 
    You'll get a crash because you are using an uninitialized pointer. So you should make it:
    Qt Code:
    1. lcdX = new QLCDNumber(X);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. #4
    Join Date
    May 2007
    Location
    Athens, Greece
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Smile Re: Getting the value from a spinbox

    Right on, JPN, about my mistake on the local/member variables - that is now corrected.

    So, I am now using:

    Qt Code:
    1. void Window::calculate()
    2. {
    3. int valJ1 = valueSpinBoxJ1->value();
    4. int valJ2 = valueSpinBoxJ2->value();
    5. int X = valJ1+valJ2;
    6. lcd3->display(X);
    7. qDebug("X = %.4f",X);
    8. }
    To copy to clipboard, switch view to plain text mode 

    lcd3 is working (obviously), but I still get consecutively "X=0.0000". My worry is that if I need to use X somewhere else, it will not be there with the correct value - how should I deal with that?

    Thanks for your help so far though!

    X-man

  5. #5
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    43
    Thanked 3 Times in 3 Posts

    Default Re: Getting the value from a spinbox

    whatup X-

    try:

    Qt Code:
    1. qDebug("X = %d",X);
    To copy to clipboard, switch view to plain text mode 

    your "X" variable is an int

  6. The following user says thank you to vonCZ for this useful post:

    X-man (28th September 2007)

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Getting the value from a spinbox

    With
    Qt Code:
    1. qDebug() << X;
    To copy to clipboard, switch view to plain text mode 
    there would be no such problem..
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    X-man (28th September 2007)

  9. #7
    Join Date
    May 2007
    Location
    Athens, Greece
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Smile Re: Getting the value from a spinbox

    You are both right - i accidentally fixed when I turner all the v's to floats. So now everything works like a charm...

    ...till everything stops working again.

    Thanks for your replies, (unfortunately) I will get back with more questions

    X-man

Similar Threads

  1. spinbox
    By mickey in forum Newbie
    Replies: 5
    Last Post: 26th July 2006, 19:09
  2. spinbox
    By mickey in forum Newbie
    Replies: 2
    Last Post: 22nd May 2006, 08:35
  3. buttonGroup and spinBox
    By mickey in forum Newbie
    Replies: 9
    Last Post: 21st May 2006, 17:50
  4. problem with spinbox control ?
    By vinod in forum Qt Programming
    Replies: 3
    Last Post: 19th April 2006, 19:58
  5. a simple question: spinbox
    By mickey in forum Newbie
    Replies: 3
    Last Post: 27th February 2006, 15:37

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.