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
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Getting the value from a spinbox

    You might want to try to show a QMessageBox instead of using qDebug(), just in case something is wrong with qDebug. Looks like the slot is not called at all, so it would be nice if you shew us the line of code where you make the respective signal-slot connection.

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

    Default Re: Getting the value from a spinbox

    Ok, I am putting up a short version of my window.h and window.cpp with just two spinboxes and no connection to other .cpp files.

    window.h
    Qt Code:
    1. #ifndef WINDOW_H
    2. #define WINDOW_H
    3.  
    4. #include <QWidget>
    5. class QGroupBox;
    6. class QLabel;
    7. class QSpinBox;
    8. class QLCDNumber;
    9.  
    10. class Window : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. Window();
    16.  
    17. public slots:
    18. void setMinimum1(int value);
    19. void setMaximum1(int value);
    20. void setMinimum2(int value);
    21. void setMaximum2(int value);
    22.  
    23. private:
    24. void createControls(const QString &title);
    25.  
    26. QGroupBox *controlsGroup;
    27. QLabel *J1;
    28. QLabel *J2;
    29. QLabel *minimumLabel;
    30. QLabel *maximumLabel;
    31. QLabel *valueLabel;
    32.  
    33. QSpinBox *minimumSpinBoxJ1;
    34. QSpinBox *maximumSpinBoxJ1;
    35. QSpinBox *valueSpinBoxJ1;
    36. QSpinBox *minimumSpinBoxJ2;
    37. QSpinBox *maximumSpinBoxJ2;
    38. QSpinBox *valueSpinBoxJ2;
    39.  
    40. QLCDNumber *lcd1;
    41. QLCDNumber *lcd2;
    42.  
    43. };
    44. #endif
    To copy to clipboard, switch view to plain text mode 

    and window.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include "window.h"
    3.  
    4. Window::Window()
    5. {
    6. createControls(tr("Controls"));
    7.  
    8. connect(minimumSpinBoxJ1, SIGNAL(valueChanged(int)), this, SLOT(setMinimum1(int)));
    9. connect(maximumSpinBoxJ1, SIGNAL(valueChanged(int)), this, SLOT(setMaximum1(int)));
    10. connect(minimumSpinBoxJ2, SIGNAL(valueChanged(int)), this, SLOT(setMinimum2(int)));
    11. connect(maximumSpinBoxJ2, SIGNAL(valueChanged(int)), this, SLOT(setMaximum2(int)));
    12.  
    13. QGridLayout *mainLayout = new QGridLayout;
    14. mainLayout->addWidget(controlsGroup,1,0);
    15. mainLayout->addWidget(sizesGroup,2,0);
    16. mainLayout->addWidget(coinWidget,0,0,1,0);
    17. setLayout(mainLayout);
    18. minimumSpinBoxJ1->setValue(0);
    19. maximumSpinBoxJ1->setValue(0);
    20. valueSpinBoxJ1->setValue(0);
    21. minimumSpinBoxJ2->setValue(0);
    22. maximumSpinBoxJ2->setValue(0);
    23. valueSpinBoxJ2->setValue(0);
    24.  
    25. }
    26.  
    27. void Window::createControls(const QString &title)
    28. {
    29. controlsGroup = new QGroupBox(title);
    30. controlsGroup->setAlignment(Qt::AlignCenter);
    31.  
    32. J1 = new QLabel(tr("J1"));
    33. J1->setAlignment(Qt::AlignCenter);
    34. J2 = new QLabel(tr("J2"));
    35. J2->setAlignment(Qt::AlignCenter);
    36.  
    37. minimumLabel = new QLabel(tr("Angle - minimum value:"));
    38. maximumLabel = new QLabel(tr("Angle - maximum value:"));
    39. valueLabel = new QLabel (tr("Angle - current value:"));
    40. minimumSpinBoxJ1 = new QSpinBox;
    41. minimumSpinBoxJ1->setRange(-500,500);
    42. minimumSpinBoxJ1->setSingleStep(1);
    43. maximumSpinBoxJ1 = new QSpinBox;
    44. maximumSpinBoxJ1->setRange(-500,500);
    45. maximumSpinBoxJ1->setSingleStep(1);
    46. valueSpinBoxJ1 = new QSpinBox;
    47. valueSpinBoxJ1->setSingleStep(1);
    48. minimumSpinBoxJ2 = new QSpinBox;
    49. minimumSpinBoxJ2->setRange(-500,500);
    50. minimumSpinBoxJ2->setSingleStep(1);
    51. maximumSpinBoxJ2 = new QSpinBox;
    52. maximumSpinBoxJ2->setRange(-500,500);
    53. maximumSpinBoxJ2->setSingleStep(1);
    54. valueSpinBoxJ2 = new QSpinBox;
    55. valueSpinBoxJ2->setSingleStep(1);
    56.  
    57. QLCDNumber *lcd1 = new QLCDNumber(3);
    58. lcd1->setSegmentStyle(QLCDNumber::Flat);
    59. QLCDNumber *lcd2 = new QLCDNumber(4);
    60. lcd2->setSegmentStyle(QLCDNumber::Flat);
    61. QLCDNumber *lcd3 = new QLCDNumber(4);
    62.  
    63. connect(valueSpinBoxJ1,SIGNAL(valueChanged(int)),lcd1,SLOT(display(int)));
    64. connect(valueSpinBoxJ2,SIGNAL(valueChanged(int)),lcd2,SLOT(display(int)));
    65.  
    66. QGridLayout *controlsLayout = new QGridLayout;
    67. controlsLayout->addWidget(J1,1,1);
    68. controlsLayout->addWidget(J2,1,2);
    69.  
    70. controlsLayout->addWidget(lcd1,5,1);
    71. controlsLayout->addWidget(lcd2,5,2);
    72.  
    73. controlsLayout->addWidget(minimumLabel, 2, 0);
    74. controlsLayout->addWidget(maximumLabel, 3, 0);
    75. controlsLayout->addWidget(valueLabel, 4, 0);
    76.  
    77. controlsLayout->addWidget(minimumSpinBoxJ1, 2, 1);
    78. controlsLayout->addWidget(maximumSpinBoxJ1, 3, 1);
    79. controlsLayout->addWidget(valueSpinBoxJ1, 4, 1);
    80. controlsLayout->addWidget(minimumSpinBoxJ2, 2, 2);
    81. controlsLayout->addWidget(maximumSpinBoxJ2, 3, 2);
    82. controlsLayout->addWidget(valueSpinBoxJ2, 4, 2);
    83. controlsGroup->setLayout(controlsLayout);
    84. }
    85.  
    86.  
    87. void Window::setMinimum1(int value)
    88. {
    89. valueSpinBoxJ1->setMinimum(value);
    90. }
    91.  
    92. void Window::setMaximum1(int value)
    93. {
    94. valueSpinBoxJ1->setMaximum(value);
    95. }
    96.  
    97. void Window::setMinimum2(int value)
    98. {
    99. valueSpinBoxJ2->setMinimum(value);
    100. }
    101.  
    102. void Window::setMaximum2(int value)
    103. {
    104. valueSpinBoxJ2->setMaximum(value);
    105. }
    To copy to clipboard, switch view to plain text mode 

    What I have to do is this:

    a) fetch the value of SpinBoxJ1 and assign it to a variable (say varJ1)
    b) fetch the value of SpinBoxJ2 and assign it to another variable (say varJ2)
    c) Make a calculation (say valJ1+varJ2) and assign the result to another variable (say var J12)
    d) Send that result to a new QLCD or elsewhere

    Obviously, varJ1 and varJ2 (and varJ12) should change every time I change any of the two spinboxes.

    Sorry for the long post (I tried to include only the essential parts of the code), waiting for your feedback.

    X-man
    Last edited by wysota; 26th September 2007 at 18:53. Reason: Changed quote to code

  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

    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

  4. #4
    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

  5. #5
    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

  6. #6
    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

  7. #7
    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

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

    X-man (28th September 2007)

  9. #8
    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

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

    X-man (28th September 2007)

  11. #9
    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.