Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Focus does not change on button click
Forum Updated to NodeBB v4.3 + New Features

Focus does not change on button click

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jason.parker.uf
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jason.parker.uf
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jason.parker.uf
          wrote on last edited by
          #4

          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();
              }
          }
          

          }
          @

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jason.parker.uf
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jason.parker.uf
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                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();
                        }
                    }
                }
                

                @

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved