Tracking when focus leaves a TextField



  • This is a quick note on how I ended up solving the problem I outlined earlier in this "thread":http://qt-project.org/forums/viewthread/15444/. I am not entirely happy with the solution, but in case someone else comes down the same path looking for a solution, at least it is one possible way of doing it.

    The problem definition is this: I want to be notified when the user leaves a field, presumably after editing it. The page displays a parametric equation so that the other fields may then be computed based on the modified value.

    In my TextField definition, I do the following:

    @
    TextField {
    id: x
    onActiveFocusChanged: {calcXYObject.toggleExitingField();
    calcXYObject.setX(x.text)}
    }
    @

    In my CalcXY C++ class, I put this in the setX (and setY and setZ) members:

    @
    if (exitingField && (newX != x)) {
    x=newX
    }
    @

    where exitingField is a private variable that toggles with every focus change.

    As I said, I don't like it, and I hope that there is a better way but this allows me to get something in place until I find a better solution. Ideally, there should be an onExitField event that we can trap.

    Regards.



  • You should use Qt Property System !
    exemple :
    @
    Field {
    value: calcXYObject.valueA
    onActiveFocusChanged: calcXYObject.reset()
    }

    Field {
    value: calcXYObject.valueB
    onActiveFocusChanged: calcXYObject.reset()
    }

    Field {
    value: calcXYObject.valueC
    onActiveFocusChanged: calcXYObject.reset()
    }

    //================ IN CPP

    void CalcXYObject::reset()
    {

    valueA = .....
    valueB = ......
    valueC = ......

    emit valueAChanged();
    emit valueBChanged();
    emit valueCChanged();

    }

    //==================== IN H

    class CalcXYObject : public QObject
    {
    Q_PROPERTY(int valueA READ getValueA WRITE setValueA NOTIFY valueAChanged)
    Q_PROPERTY(int valueB READ getValueB WRITE setValueA NOTIFY valueBChanged)
    Q_PROPERTY(int valueA READ getValueC WRITE setValueC NOTIFY valueCChanged)

    void getValueA(){return mValueA;}
    void getValueB(){return mValueB;}
    void getValueC(){return mValueC;}

    void setValueA(int val){mValueA = val;}
    void setValueB(int val){mValueB = val;}
    void setValueC(int val){mValueC = val;}

    signals:
    void valueAChanged();
    void valueBChanged();
    void valueCChanged();

    }

    @



  • Thanks!

    I'll give this a try. Makes more sense.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.