PDA

View Full Version : QMessageBox and Focus



fruzzo
17th November 2008, 20:00
Hi, I've problem with an application...the code is something like this one:

class MyDialog: public QDialog
{
<......other code.......>
public slots:
void angleChanged();
private:
QLineEdit leAngle;

}

MyDialog::MyDialog()
{
<......other code.......>
connect(angle, SIGNAL(editingFinished ()), this, SLOT(angleChanged()));
}

void MyDialog::angleChanged()
{
<......other code.......>
double angle = leAngle.text().toDouble();
if (angle<45)
QMessageBox::Warning(this, "Warning", "Angle less than 45°");
double x = sin(angle);
<......other code.......>
}

When I open one instance of MyDialog, change the leAngle with a string like "30" and when this one lost the focus the QMessageBox Dialog appear 2 times, double x = sin(angle) is exectued only the second time that the QMessageBox is called ....Obviously I don't want this strange behaviour.
I investigate and this behaviour is due to the fact that when the QMessageBox is called for the first time the MyDialog lost the focus and so angleChanged is called another time and so QMessageBox too.
How can I resolve this question without remove the connection with the signal editingFinished?

What the meaning of the QWidget::setFocusProxy function?

spirit
17th November 2008, 20:11
did you specify parent for leAngle? why don't create leAngle on a heap?

fruzzo
18th November 2008, 01:04
This is an semplified example...don't worry, the leAngle is created in the heap in my real code and has a parent! :p
I think that the problem it isn't realated to this factor! ;)

fruzzo
18th November 2008, 19:39
Solved...I don't know if it's the best solution but work.
Before calling a QMessageBox I disconnet every editingFinished signal and just after I re-connect this ones.