Important: Please read the Qt Code of Conduct -

QML TextArea with scrollbar and write text into from C++

  • Hello,

    I'm writing s mall SIP app for my Ubuntu mobile phone in QML and C++. I'm
    very new to QML, so please be patiant with my question. The app needs a
    scrolled TextArea which I have defined in my QML file as:

            TextArea {
                id: outputBaresip
                objectName: "outputBaresip"
                x: 10; y: 160
                width: 520; height: 740
                text: "Output from baresip:\n"
                font.pointSize: 12
                activeFocusOnPress: false

    and I can write text into it from C++ with reading the current text, appending
    some more lines and writing back the property with this C++ code:

    QObject *outputBaresip = rootObj->findChild<QObject*>("outputBaresip");
    QString currentTextArea = outputBaresip->property("text").toString();
                    outputBaresip->setProperty("text", currentTextArea +
                    "\n" + dialCommand + "\n" +
                    "a few new lines from baresip\nmultilines\n");

    What I do not understand how to do:

    1. The TextArea should never get the focus (to not bring up the
      onscreen keyboard of the mobile if it is touched).
    2. It should have a scrollbar on the right
    3. The text should be appended (without first reading the actual property)
      and the last line should be on the bottom of the TextArea, i.e. it
      should automagically scroll-up.

    Any hints? Thanks in advance.


  • Hi, and welcome to the Qt forum! Shall the text area be read-only?

  • yes. thanks

  • Ok, you almost got the solution. The following works:


    ApplicationWindow {
        visible: true
        width: 600
        height: 400
        TextArea {
            objectName: "outputBaresip"
            width: 200
            height: 50
            readOnly: true
                "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " +
                "sed do eiusmod tempor incididunt ut labore et dolore magna " +
                "aliqua. Ut enim ad minim veniam, quis nostrud exercitation " +
                "ullamco laboris nisi ut aliquip ex ea commodo cosnsequat. ";


        QQmlApplicationEngine engine;
        QObject *outputBaresip = engine.rootObjects().at(0)->findChild<QObject*>("outputBaresip");
        QMetaObject::invokeMethod(outputBaresip, "append", Qt::DirectConnection,
                                  Q_ARG(QVariant, QVariant("Hello!")));

  • Hello,
    Thanks. The property 'readOnly' solved problem 1) and 2). Your C++ code gives the following error:

    QMetaObject::invokeMethod: No such method TextArea_QMLTYPE_12::append(QVariant)

    Perhaps because I use the following methods:

    QObject *rootObj;
    int main(int argc, char *argv[]) {
      QGuiApplication app(argc, argv);
      QQuickView v;
      QUrl file = (QUrl) "myRoot/home/phablet/baresip-app/baresip.qml";
      MyClass myClass;
      rootObj = v.rootObject();


    extern QObject *rootObj;
    QObject *outputBaresip = rootObj->findChild<QObject*>("outputBaresip");
    // this worked:
    // QString currentTextArea = outputBaresip->property("text").toString();
    // outputBaresip->setProperty("text", currentTextArea + "\n" + dialCommand + "\n");
    QMetaObject::invokeMethod(outputBaresip, "append",
         Qt::DirectConnection,Q_ARG(QVariant, QVariant("Hello!")));

    Any idea how to call 'append()' within my form to access the QML objects?

  • The invokeMethod seems to work but it says that your object (TextArea) doesn't have the append method. What's your Qt version? For me it works with Qt 5.7 and QtQuick.Controls 1.4. Maybe check if append is available by adding something like this to your qml file:

    Button {
        text: "append somthing"
        onClicked: { myTextArea.append("something"); }

  • Hi,
    I have added:

    Button {
        text: "append somthing"
        onClicked: { outputBaresip.append("something"); }

    which gives this button and on click:

    file:///home/phablet/myRoot/home/phablet/baresip-app/baresip.qml:34: TypeError: Property 'append' of object TextArea_QMLTYPE_32(0x24fc30, "outputBaresip") is not a function

    The Qt version at runtime (checked with qVersion()) gives 5.4.1.
    How can I check for QtQuick.Controls version? In any case, I can not update the software in the mobile.

  • The used version of the controls is determined by the import statement in your qml file, like: import QtQuick.Controls 1.4 (top of file).

    Ok, your current version of TextArea really does not have the append function, so you need to work around that.

  • I have as imports:

    import QtQuick 2.4
    import Ubuntu.Components 1.3
    // import QtQuick.Controls 1.4

    QtQuick.Controls 1.4 is not installed in the mobile.

    In any case, thanks for your helping hand.

Log in to reply