PDA

View Full Version : Passing QSpinBox::value() to a gloabal variable



adonis
4th June 2007, 23:16
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:confused:.What should i do to get the changed value of the QSpinbox?

jacek
5th June 2007, 00:36
QSpinBox will emit the QSpinBox::valueChanged() signal when the value changes. You can connect it to some slot and read the new value there.

adonis
7th June 2007, 23:08
I have connected the QSpinBox::valueChanged() Signal to a slot from my QMainMindow class.This is a part of the code:


double dimx;//The global scope
...
void MainWindow::accepter(double d){
dimx=d;
qApp->aboutQt();
}
//in the main function
...
QApplication app(argc, argv);
MainWindow *mainWin=new MainWindow;
std:: ofstream myfile;

myfile.open ("data.txt");
myfile<<dimx;
myfile.close();


return app.exec();
}

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.:(

marcel
8th June 2007, 20:13
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:


QApplication app(argc, argv);
MainWindow *mainWin=new MainWindow;
mainWin->show();

int execRet = app.exec();

std:: ofstream myfile;
myfile.open ("data.txt");
myfile<<dimx;
myfile.close();

return execRet;


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