PDA

View Full Version : how connection variable in functin with spinBox

Sergey19
3rd August 2010, 13:39
Good afternoon !
I am beginner in qt.I have a problem .How connection variable i in function with my spinBox.
This my function :
void DataPlot::insertCurve(Qt::Orientation o,
const QColor &c, double base)
{
QwtPlotCurve *curve = new QwtPlotCurve();
curve->setPen(QPen(Qt::blue,3));

double i ;
double x[10];
double y[sizeof(x) / sizeof(x[0])];

for ( uint k = 0; k < sizeof(x) / sizeof(x[0]); k++ )
{
double v = i+k * 1.5;
if ( o == Qt::Horizontal )
{
x[k] = v;
y[k] = base;
}
else
{
x[k] = base;
y[k] = v;
}
}

curve->setData(x, y, sizeof(x) / sizeof(x[0]));
curve->attach(this);
}

Zlatomir
3rd August 2010, 17:15
You want the i variable to get the spin box value?Sorry, but i don't understand what you meant by connection.

GreenScape
3rd August 2010, 18:57
double y[sizeof(x) / sizeof(x[0])];

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

Zlatomir
3rd August 2010, 23:21
@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)

GreenScape
4th August 2010, 08:23
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.

Zlatomir
4th August 2010, 10:40
@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.

Sergey19
4th August 2010, 14:52
Execuse me for me .I want that variable i changed with spinBox.help me,please )

Zlatomir
4th August 2010, 15:36
Is the function: void DataPlot::insertCurve(Qt::Orientation o, const QColor &c, double base) in the same object with QSpinBox?
If yes, just use:
double i = spinBoxObjectName.value();
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 (http://doc.qt.nokia.com/4.6/qvector.html) (or std::vector) or QList (http://doc.trolltech.com/4.6/qlist.html) instead of C-Style arrays, you have some facilities (like they know their size...)

Sergey19
5th August 2010, 11:50
Thank you very much ,but I want that user can change in spinbox my function redraw !)) Execuse me for my English !)

Zlatomir
5th August 2010, 11:59
QSpinBox has a signal called valueChanged (http://doc.trolltech.com/4.6/qspinbox.html#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)

Sergey19
5th August 2010, 12:09
how write this coonect! Execuse maybe we write examples !))

Lykurg
5th August 2010, 12:16
how write this coonect! Execuse maybe we write examples !))
One is allowed to read the documentation: QObject::connect().

EDIT: also SignalsandSlots (:p too late...)

Zlatomir
5th August 2010, 12:18
Documentation for signals and slots (http://doc.trolltech.com/4.6/signalsandslots.html) (you can find examples there)

Sergey19
5th August 2010, 12:43
I read documentation....and write next code .but I have a mistale
View code:
void DataPlot::strob(Qt::Orientation o,
const QColor &c, double base)
{
QSpinBox*spinBox2 = new QSpinBox;
spinBox2->setRange(0,699);
// spinBox2->setValue(43);
spinBox2->show();
double i = spinBox2->value();
QObject::connect(spinBox2,SIGNAL(valueChanged(int) ),i,SLOT(value));
QwtPlotCurve *curve = new QwtPlotCurve();
curve->setPen(QPen(Qt::blue,3));
// double i ;
double x[10];
double y[sizeof(x) / sizeof(x[0])];

for ( uint k = 0; k < sizeof(x) / sizeof(x[0]); k++ )
{

double v = i+k * 1.5;
if ( o == Qt::Horizontal )
{
x[k] = v;
y[k] = base;
}
else
{
x[k] = base;
y[k] = v;
}
}
curve->setData(x, y, sizeof(x) / sizeof(x[0]));
curve->attach(this);
}

Lykurg
5th August 2010, 12:49
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.

Sergey19
5th August 2010, 13:03
what mean ui !? I don't understand what function you mean.I understand that first is change spinbox and what about others !?

Lykurg
5th August 2010, 13:10
How should
QObject::connect(spinBox2,SIGNAL(valueChanged(int) ),i,SLOT(value));work, if i is a double? the receiver in a connect statement has to be a QObject. (As it is written in the documentation!)

Sergey19
5th August 2010, 13:24
I understand this ,but I don't know what receiver I must have in this connect )

Lykurg
5th August 2010, 13:48
Any QObject derived class with a proper slot! And in your situation nothing will fit since your design is...

Sergey19
5th August 2010, 13:58
Help me create slot .Thank you very much ))

Zlatomir
5th August 2010, 14:07
Isn't it better to put the 'i' variable in the class definition? (not a local variable to that function)
And declare a function (a slot, with "public slots:" access specifier) and that function will look like:

void YourClass::set_i(double input) { i = input;} //i is your member variable i
and connect this slot with the SpinBox valueChanged signal:
(in your class constructor if the spinbox2 is member of the same class!)

connect(spinBox2,SIGNAL(valueChanged(double)), this, SLOT(set_i(double));

Note: You can do it with double or int, but the QSpinBox doesn't have a valueChanged(double) only int
QDoubleSpinBox (http://doc.trolltech.com/4.6/qdoublespinbox.html) has valueChanged(double) not int

And we can't create a complete design, we can only give you advices, because we don't know the actual problem (don't even know the design of your class)

sherbs
5th August 2010, 15:27
As others have mentioned, you need to spend some time doing your homework:

http://doc.trolltech.com/4.6/signalsandslots.html

Read through this documentation and you should easily be able to accomplish what you want to do... As of now, your design/code reflects a fairly fundamental misunderstanding of how Qt works.

Sergey19
9th August 2010, 09:13
Isn't it better to put the 'i' variable in the class definition? (not a local variable to that function)
And declare a function (a slot, with "public slots:" access specifier) and that function will look like:

void YourClass::set_i(double input) { i = input;} //i is your member variable i
and connect this slot with the SpinBox valueChanged signal:
(in your class constructor if the spinbox2 is member of the same class!)

connect(spinBox2,SIGNAL(valueChanged(double)), this, SLOT(set_i(double));

Note: You can do it with double or int, but the QSpinBox doesn't have a valueChanged(double) only int
QDoubleSpinBox (http://doc.trolltech.com/4.6/qdoublespinbox.html) has valueChanged(double) not int

And we can't create a complete design, we can only give you advices, because we don't know the actual problem (don't even know the design of your class)
I create a line ....And I want when user change value in spinBox this line move left or right .