[Qt-interest] Is there a better way to code this to prevent recursive calling?
Sean Harmer
sean.harmer at maps-technology.com
Wed Feb 25 07:25:23 CET 2009
Hi,
Please respond to the list. That way others can benefit from the advice
given...
On Tuesday 24 February 2009 19:37:27 you wrote:
> > This would be better as:
> >
> > void MainWindow::label_HT_Cost_Set(int Num)
> > {
> > ui->label_HT_Cost_2->setNum((Num-10)*10);
> >
> > bool block = ui->spinBox_FP_2->blockSignals(true); // adjust_FP but
> > not the cost of
> > it. ui->spinBox_FP_2->setValue(real_FP + ui->spinBox_HT_2->value() -
> > 10); ui->spinBox_FP_2->blockSignals(block);
> >
> > adjust_Unspent_Points();
> > }
> >
> > i.e. use true not TRUE and restore the original value of blockSignals
> > rather than assuming it was false to start with.
> >
> > ATB,
> >
> > Sean
>
> Can you explain that a bit more. I get it enough to use it but what
> are you really doing and why? I was a bit stumped with the docks over
> this point too. Is the idea here that it could be blocked or !blocked
> by the parent and so you want to preserve this state?
Any QObject can have its signals blocked by calling the blockSignals()
function. All we are doing here is storing the current state when we tell it
that we definitely wish to block its signals so that we can restore it once we
have done what we need to do. This way we will not interfere with any other
code that may be dependent upon this QObject not emitting signals. It's just
good manners to other clients of this QObject I suppose. We are just returning
the object to the state it was in when we found it.
> Bet this could be better too but have not found any setNum for a lineEdit
> yet.
>
> QString text_int;
> ui->lineEdit_Basic_Lift->setText(text_int.setNum( Num * Num / 5 ));
A couple of options:
(i) Use a QSpinBox or QDoubleSpinBox depending on the type of number you wish
to store.
(ii) Use one of the QString::number( x ) functions to convert your number to a
string. Be careful of precision when converting back again though.
HTH,
Sean
More information about the Qt-interest-old
mailing list