Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: how connection variable in functin with spinBox

  1. #1
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default how connection variable in functin with spinBox

    Good afternoon !
    I am beginner in qt.I have a problem .How connection variable i in function with my spinBox.
    This my function :
    Qt Code:
    1. void DataPlot::insertCurve(Qt::Orientation o,
    2. const QColor &c, double base)
    3. {
    4. QwtPlotCurve *curve = new QwtPlotCurve();
    5. curve->setPen(QPen(Qt::blue,3));
    6.  
    7.  
    8. double i ;
    9. double x[10];
    10. double y[sizeof(x) / sizeof(x[0])];
    11.  
    12. for ( uint k = 0; k < sizeof(x) / sizeof(x[0]); k++ )
    13. {
    14. double v = i+k * 1.5;
    15. if ( o == Qt::Horizontal )
    16. {
    17. x[k] = v;
    18. y[k] = base;
    19. }
    20. else
    21. {
    22. x[k] = base;
    23. y[k] = v;
    24. }
    25. }
    26.  
    27. curve->setData(x, y, sizeof(x) / sizeof(x[0]));
    28. curve->attach(this);
    29. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how connection variable in functin with spinBox

    You want the i variable to get the spin box value?Sorry, but i don't understand what you meant by connection.

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

    Default Re: how connection variable in functin with spinBox

    Qt Code:
    1. double y[sizeof(x) / sizeof(x[0])];
    To copy to clipboard, switch view to plain text mode 

    looking wroong. what is the reason for dividing size of array on size of array element?

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how connection variable in functin with spinBox

    @GreenScape: It's the C style way of find out the size (number of elements) of an C-style array, but you are right it's completely useless (since the array size is known to be 10)

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

    Default Re: how connection variable in functin with spinBox

    no way man! sizeof(x) returns 10, but not 40! and sizeof(x) / sizeof(x[0]) == 2.5 ! maybe the old C-style way to find out the array size you mean? then sizeof(x) * sizeof(x[0]), because when array is local and not dynamucally allocated, sizeof() allways returns number of elements.

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how connection variable in functin with spinBox

    @GreenScape: I have to contradict you: qDebug() << sizeof(x); //should print 40 in our case, and if you make 40/4 you get 10 (number of elements, the multiply wouldn't make sense)

    I think (not 100% positive about this one)... it might fail if you use a pointer: like int *ptr = new int[10]; sizeof(ptr); returns 4(size of a pointer)

    Anyway in this particular case it is not necessary because the number of elements it's known.
    Last edited by Zlatomir; 4th August 2010 at 11:48.

  7. #7
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how connection variable in functin with spinBox

    Execuse me for me .I want that variable i changed with spinBox.help me,please )

  8. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how connection variable in functin with spinBox

    Is the function: void DataPlot::insertCurve(Qt::Orientation o, const QColor &c, double base) in the same object with QSpinBox?
    If yes, just use:
    Qt Code:
    1. double i = spinBoxObjectName.value();
    To copy to clipboard, switch view to plain text mode 
    If not tell us more "design" options, this function (void DataPlot::insertCurve(...)) you want to execute whenever the QSpinBox chage value, or just at some point (like user click a button, and the insertCurve executes with the current value of QSpinBox)
    In this second case you will need signals and slots, but how to connect them depents on how/when you need the QSpinBox value, and in what object spinBox is... so we need a little more info.

    And a little advice, you can use QVector (or std::vector) or QList instead of C-Style arrays, you have some facilities (like they know their size...)

  9. #9
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how connection variable in functin with spinBox

    Thank you very much ,but I want that user can change in spinbox my function redraw !)) Execuse me for my English !)

  10. #10
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how connection variable in functin with spinBox

    QSpinBox has a signal called valueChanged witch is emitted when the value changes.

    You can connect that signal to the slot/slots that update what you need (if you connect with the functions you wrote make sure that you declare them with "public slots:" access specifier)

    LE: depends on your design, but another idea is to have an int (or double) variable as a member in your class and write setter and getter functions for that (declare the setter function slot and connect the valueChanged signal to the setter slot <witch updates the member variable's value)
    Last edited by Zlatomir; 5th August 2010 at 13:06.

  11. #11
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how connection variable in functin with spinBox

    how write this coonect! Execuse maybe we write examples !))

  12. #12
    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: how connection variable in functin with spinBox

    Quote Originally Posted by Sergey19 View Post
    how write this coonect! Execuse maybe we write examples !))
    One is allowed to read the documentation: QObject::connect().

    EDIT: also SignalsandSlots ( too late...)
    Last edited by Lykurg; 5th August 2010 at 13:19. Reason: updated contents

  13. #13
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how connection variable in functin with spinBox

    Documentation for signals and slots (you can find examples there)

  14. #14
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how connection variable in functin with spinBox

    I read documentation....and write next code .but I have a mistale
    View code:
    Qt Code:
    1. void DataPlot::strob(Qt::Orientation o,
    2. const QColor &c, double base)
    3. {
    4. QSpinBox*spinBox2 = new QSpinBox;
    5. spinBox2->setRange(0,699);
    6. // spinBox2->setValue(43);
    7. spinBox2->show();
    8. double i = spinBox2->value();
    9. QObject::connect(spinBox2,SIGNAL(valueChanged(int)),i,SLOT(value));
    10. QwtPlotCurve *curve = new QwtPlotCurve();
    11. curve->setPen(QPen(Qt::blue,3));
    12. // double i ;
    13. double x[10];
    14. double y[sizeof(x) / sizeof(x[0])];
    15.  
    16. for ( uint k = 0; k < sizeof(x) / sizeof(x[0]); k++ )
    17. {
    18.  
    19. double v = i+k * 1.5;
    20. if ( o == Qt::Horizontal )
    21. {
    22. x[k] = v;
    23. y[k] = base;
    24. }
    25. else
    26. {
    27. x[k] = base;
    28. y[k] = v;
    29. }
    30. }
    31. curve->setData(x, y, sizeof(x) / sizeof(x[0]));
    32. curve->attach(this);
    33. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    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: how connection variable in functin with spinBox

    Obviously you don't have read the documentation carefully! First you really should use layouts, and further, i is no object its a integer which you cant use in a connect statement as a receiver!

    If you want to redraw your function when the value of i changes make three functions: the first sets up the ui (only one time), next is a slot you call when your spinbox changes, and a third is your paint function depending on your local variable i. You also can combine the 2nd and 3rd function by skiping the local variable and using spinBox2->value() directly in the slot. But rally first set up your gui and don't do it all in one function because that won't work.

  16. #16
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how connection variable in functin with spinBox

    what mean ui !? I don't understand what function you mean.I understand that first is change spinbox and what about others !?
    Last edited by Sergey19; 5th August 2010 at 14:16.

  17. #17
    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: how connection variable in functin with spinBox

    How should
    Qt Code:
    1. QObject::connect(spinBox2,SIGNAL(valueChanged(int)),i,SLOT(value));
    To copy to clipboard, switch view to plain text mode 
    work, if i is a double? the receiver in a connect statement has to be a QObject. (As it is written in the documentation!)

  18. #18
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how connection variable in functin with spinBox

    I understand this ,but I don't know what receiver I must have in this connect )

  19. #19
    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: how connection variable in functin with spinBox

    Any QObject derived class with a proper slot! And in your situation nothing will fit since your design is...

  20. #20
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how connection variable in functin with spinBox

    Help me create slot .Thank you very much ))

Similar Threads

  1. Replies: 1
    Last Post: 2nd April 2010, 07:42
  2. a spinbox problem
    By tyhj2000 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 26th February 2010, 03:44
  3. Getting the value from a spinbox
    By X-man in forum Newbie
    Replies: 14
    Last Post: 28th September 2007, 02:27
  4. spinbox
    By mickey in forum Newbie
    Replies: 5
    Last Post: 26th July 2006, 20:09
  5. spinbox
    By mickey in forum Newbie
    Replies: 2
    Last Post: 22nd May 2006, 09:35

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.