Focus does not change on button click



  • Hello,
    This seems like a problem that must have been covered already, but I cannot find anything on it.

    I have a GUI with some TextField 's and a submit button, and I want to check that all fields are completed before executing on the submit button function. I have used the onEditingFinished signal to set variables in c++, which happens whenever the user hits enter or the TextField loses focus.

    However, I have found that in the rare case that a user deletes everything in one of the text box's and hits submit (i.e. without hitting enter, or apparently changing focus), the program does not register that the value of the TextField has been changed. From what I can tell this is because the TextField does not lose focus when the button is clicked.

    Any ideas on solutions? Thanks.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Something's not completely clear, are you mixing QML and C++ ? Can you show a minimal example that helps reproduce the behavior ?



  • Thanks. Yes, I am mixing c++ and qml. Below is an example. Please note that the only way to get the program to print the current number is to hit enter - because there is no way to change focus. My more complex program is similar in that if a user were to hit the submit button directly after editing the TextField (without hitting enter), the new value would not be updated because the focus would not change.



  • set_text.h:
    @

    class set_number_object : public QObject {
    Q_OBJECT
    public slots:
    void set_number(int number);
    void print_number();
    public:
    //Constructor
    set_number_object();
    private:
    int number_;
    };
    @

    main.cpp:
    @

    set_number_object::set_number_object(){
    //Constructor
    }

    void set_number_object::set_number(int number){
    number_=number;
    }

    void set_number_object::print_number(){
    cout<<"The number is: "<<number_<<endl;
    }

    int main(int argc, char *argv[])
    {

    set_number_object set_number_c;
    
    QApplication app(argc, argv);
    
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    engine.rootContext()->setContextProperty("set_number_q",&set_number_c);
    return app.exec&#40;&#41;;
    

    }
    @

    main.qml:
    @

    ApplicationWindow {
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true

    MainForm {
        anchors.fill: parent
    }
    

    }
    @

    MainForm.ui.qml:
    @

    Item {
    width: 640
    height: 480

    property alias button1: button1
    
    TextField {
        id: text1field
        objectName: "text1field_object"
        x: 100;
        y: 100;
        width: 400;
        height: 75;
        text: qsTr("")
        font.pixelSize: 26
        onEditingFinished: set_number_q.set_number(text)
    
    }
    RowLayout {
        anchors.centerIn: parent
    
        Button {
            id: button1
            text: qsTr("Press Me 1")
            onClicked: set_number_q.print_number();
        }
    }
    

    }
    @



  • Well, if there are no ideas as of yet, let me throw out a few ideas I've had (but haven't been able to implement, mind you).

    1. Is there possibly a way that I could access the text in all of my TextFields when the submit button is clicked? For example, in my c++ code I use the setProperty signal to update text in the GUI from c++. Is there something analagous to this that could get a property? Perhaps then I could sweep in all of the current text values in the TextFields and not worry about where the focus is.

    2. Is there a way to change focus based on where the mouse is (i.e. hover)? In that case focus would change to the submit button every time the user clicked the button, and thus the onEditingFinished call would always be made for the last TextField. However, I'm not sure if this would work if the user tabbed to the submit button instead of using the mouse, as I do not know if tab changes focus.

    Any ideas are appreciated!



  • Just checking again to see if there are any ideas. Thanks!


  • Moderators

    what about something like this (not tested though):
    @
    Item {
    width: 640
    height: 480

        property alias button1: button1
     
        function evaluateTextField(textField)
        {
             console.debug(textField.objectName);
             // do evaluation of text field
             if( evaluated )
                  set_number_q.set_number(text)
        }
    
        TextField {
            id: text1field
            objectName: "text1field_object"
            x: 100;
            y: 100;
            width: 400;
            height: 75;
            text: qsTr("")
            font.pixelSize: 26
            onEditingFinished: evaluateTextField(text1field)
     
        }
        RowLayout {
            anchors.centerIn: parent
     
            Button {
                id: button1
                text: qsTr("Press Me 1")
                onClicked: set_number_q.print_number();
            }
        }
    }
    

    @


Log in to reply
 

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