Add text in textarea inside class



  • hi
    I want to add some value to the Textarea control inside a function cpp class.
    In the widgets project, it was very easy to do inside the function.

    void MyClass::onNumberChanged(QString Number,int num)
    {
        ui->txt->appendPlainText(Number);
    }
    

    But I'm lost in Qml
    Please guide me
    tnaks...



  • @MrErfan I'm not quite sure what the problem is:

    this

    onNumberChanged: myTextArea.text = myTextArea.text + Number
    

    doesn't work for you?



  • Thanks for the answer, where do I use it?



  • @MrErfan said in add text in textarea inside class:

    Thanks for the answer, where do I use it?

    Well you've been very vague with your description: so a quick example:

    import QtQuick 2.9
    
    Rectangle{
        id:root
         property int number: 0
         onNumberChanged: myText.text = myText.text  + " " + number
    
        Timer{
        interval: 1000
        repeat: true
        running: true
        onTriggered: number = number +1
        }
    
       Text{
        id:myText
        anchors.fill: parent;
        font.pixelSize: 20
        horizontalAlignment: Qt.AlignHCenter
        verticalAlignment:  Qt.AlignVCenter
      }
    
    }
    

    Edit: fixed typo: font.size: 20 =>font.pixelSize: 20



  • I want to create numbers that within the class MyClass,
      Send to TextEdit (qml)

    MyClass::MyClass(QObject *parent) : QObject(parent)
    {
        mThread = new MyThread(this);
        connect(mThread,SIGNAL(NumberChanged(QString,int)),this,SLOT(onNumberChanged(QString,int)));
    }
    
    public slots:
        void onNumberChanged(QString,int);
    void MyClass::onNumberChanged(QString Number,int num)
    {
        //Send Number from myclass to qml textedit
    }
    


  • Hello, see this : http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html
    to create your own QML Type "MyClass" with ' qmlRegisterType<MyClass>("io.qt.myClass", 1, 0, "MyClass"); '

    then you can create a MyClass{ } Item in qml and bind properties :

    MyClass{
       onNumberChanged : txEdit.text += number
    }
    TextEdit{
      id: txEdit
    }
    

    You can also embed c++ object into QML; see here : http://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html // I think this is what you need
    Using this you have nothing to do
    //main.cpp

    MyClass obj;
    engine.rootContext()->setContextProperty("myObject",&obj)
    ```;
    //main.qml
    
    TextEdit{
       text : "your text" +  myObject.myQPrperty  // it will update automatical
    
       //using this way, variables must be QProperties and Methods must be QINVOKABLES to use from QML
    }
    

    LA


Log in to reply
 

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