Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to add/edit text from C++ in QML TextInput?
Forum Updated to NodeBB v4.3 + New Features

How to add/edit text from C++ in QML TextInput?

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 3 Posters 12.9k 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.
  • B Offline
    B Offline
    bkamps
    wrote on last edited by
    #1

    Hi,

    How can I control the TextInput QML control from c++? I don' have a touch screen, only hardware buttons so I have to control the TextInput from C++.

    I want to add / edit text, how can this be archieved? I don't see any TextInput (QML) methods that I can use from C++ to do this.

    Thanks!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Aleskey78
      wrote on last edited by
      #2

      You can contol TextInput over these methods:
      select
      selectAll
      selectWord

      and properties:
      cursorPosition
      text

      "QML functions can be called from C++ and vice-versa.":http://doc.trolltech.com/4.7-snapshot/qtbinding.html#calling-functions

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        I think "this":http://doc.trolltech.com/4.7-snapshot/qtbinding.html#modifying-properties may help you. The text is just another property of the object.

        Edit:
        One way to get a reference to the right object (created in QML) from C++ would be to give the object an object name in QML, and then search for that name in C++. Something like (untested!):

        qml:
        @
        TextInput {
        id: textInput
        objectName: textInput
        //...
        }
        @

        c++
        @
        QDeclarativeContext* context = m_declarativeView->rootContext();
        QObject* textInput = context->findChild<QObject*>("textInput");
        if (textInput) { //never simply dereference without checking if the output is valid
        textInput->setProperty("text", QString("My very fancy string!"));
        }
        @

        Of course, if you need to modify the text (or other properties) more than once, then you only need to search for the object once, after which you can manipulate them from code later.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bkamps
          wrote on last edited by
          #4

          I know I can connect to the TextInput QML element from C++ and access properties. But I also want the functionality of the TextInput QML element in C++.

          So when the cursor is set in the middle of the TextInput how can i easily add some text in the middle without having to looking at the current position, copy entered text in a QString, set the text property of TextInput and set the cursor to the new position?

          I guess when using Qt widgets this wouldn't be a problem?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Well, you can check if the QDeclarativeTextInput supports the API you need for your manipulation.

            Some widgets may have enough external API to do this detailed manipulation using external API, but also not all of them.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Aleskey78
              wrote on last edited by
              #6

              I don't know if it is possible to send keyevents to QML app, just an idea...

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bkamps
                wrote on last edited by
                #7

                That would be an idea ... But where do I find the header file of QDeclarativeTextInput? It's not in the Sdk?

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Aleskey78
                  wrote on last edited by
                  #8

                  Header is somewere in "QtSDK\QtSources\4.7.x\src\declarative\graphicsitems".

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    There only is a private header, but you can still inspect it. Name is qdeclarativetextinput_p.h in the folder mentioned by Alexkey78

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      bkamps
                      wrote on last edited by
                      #10

                      Hmm it doesn't feel good to use a private header file to get this to work.. I think i'll make my own class and connect it to the text and cursorPosition properties of the TextInput QML element...

                      Or I can try to find the object like andre told me and then send key-events to it...

                      @
                      TextInput {
                      id: textInput
                      objectName: textInput
                      //...
                      }
                      @

                      Then:

                      @
                      QDeclarativeContext* context = m_declarativeView->rootContext();
                      QObject* textInput = context->findChild<QObject*>("textInput");
                      QApplication::postEvent(textInput, keyevent);
                      @

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        You don't have to use the private header at all. You can just use it see what properties are supported (and what slots and signals there are). You can invoke all of those without using the private header.

                        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