Results 1 to 4 of 4

Thread: Passing QSpinBox::value() to a gloabal variable

  1. #1
    Join Date
    Apr 2007
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Passing QSpinBox::value() to a gloabal variable

    hi,
    I am trying to pass the value of a QSpinBox to a global variable which will be passed to other c++ functions.All what i get is the initial value of the spinbox.
    My QSpinBox is a member of a QMainwindow class.
    i ve tried to do the assignment in the main function,in the constructor of the QMainWindow,and as a separate Slot and as a function(both members of the QMainWindow).It doesn t work.What should i do to get the changed value of the QSpinbox?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing QSpinBox::value() to a gloabal variable

    QSpinBox will emit the QSpinBox::valueChanged() signal when the value changes. You can connect it to some slot and read the new value there.

  3. #3
    Join Date
    Apr 2007
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing QSpinBox::value() to a gloabal variable

    I have connected the QSpinBox::valueChanged() Signal to a slot from my QMainMindow class.This is a part of the code:
    Qt Code:
    1. double dimx;//The global scope
    2. ...
    3. void MainWindow::accepter(double d){
    4. dimx=d;
    5. qApp->aboutQt();
    6. }
    7. //in the main function
    8. ...
    9. QApplication app(argc, argv);
    10. MainWindow *mainWin=new MainWindow;
    11. std:: ofstream myfile;
    12.  
    13. myfile.open ("data.txt");
    14. myfile<<dimx;
    15. myfile.close();
    16.  
    17.  
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 
    The qApp->aboutQt() make sure that the Signal /slot connection works.
    What i get in my data.txt file is the initial value of dimx.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing QSpinBox::value() to a gloabal variable

    Is the slot getting called?
    If you do the connection correctly then there should be no problem here.

    You are getting the initial value of dimx because you're printing it too soon.
    You should do something like this to see the value assigned in the slot:
    Qt Code:
    1. QApplication app(argc, argv);
    2. MainWindow *mainWin=new MainWindow;
    3. [B]mainWin->show();
    4. [/B]
    5. int execRet = app.exec();
    6.  
    7. [B]std:: ofstream myfile;
    8. myfile.open ("data.txt");
    9. myfile<<dimx;
    10. myfile.close();
    11.  
    12. return execRet;
    13. [/B]
    To copy to clipboard, switch view to plain text mode 

    This is one way of doing it.
    You were saving the variable right after showing the
    window( show exits immediately), therefore it did not had the chance to get its new value.

    Generally, assigning to a static var should work, but you have to know when/where/if the var was modified prior to using it.

    Regards

  5. The following user says thank you to marcel for this useful post:

    adonis (8th June 2007)

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.