Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

How to connect qml button to c++ function



  • I want the data to be written to a text file if the qml button is clicked, I do this but it does not work properly, Is there another simple way?
    main .cpp:

    QQmlApplicationEngine qmlengine;
    
       const QUrl url(QStringLiteral("qrc:/main.qml"));
       QObject::connect(&qmlengine, &QQmlApplicationEngine::objectCreated,
                        &app, [url](QObject *obj, const QUrl &objUrl) {
           if (!obj && url == objUrl)
               QCoreApplication::exit(-1);
       }, Qt::QueuedConnection);
    
    
    
       TestClass test();
    
    
       qmlengine.rootContext()->setContextProperty("TestClass",&test);
       qmlengine.load(url);
    

    main.qml:

    Label {
                       id: label1
                       x: 264
                       text: qsTr("Atten:")
                   }
    
                   SpinBox {
                       id: atten
                       x: 354
                       value: TestClass.atten()
                   }
    
                   Button {
                       id: write
                       text: qsTr("Write")
                       onClicked: TestClass.writeFlag()
    
                   }
    

    TestClass :

    public:
        explicit TestClass(QObject *parent = nullptr);
    
        int  writeNewRecord(QString value1){//open file and write in it};
    
    public slots:
       void atten(){ //....}
       void valueChanged( double getData) { 
          //doing some process on getData
    
          if(writeFlag)  {                  //here is my problem and always return true
             writeNewRecord(QString::number(getData));
             wFlag=false;
    }
    };
     bool writeFlag(){
           wFlag =true;
           return wFlag;
    };
    
    private:
      bool wFlag =false;
    

  • Moderators

    @zhmh said in How to connect qml button to c++ function:

    I do this but it does not work properly

    How does it fail? Is writeFlag() not called?

    I guess what you mean to ask about is how to make sure atten spin box changes TestClass value when it's value is changed, right? There are many ways to do it, the easiest is to do this in QML:

    onValueChanged: TestClass.valueChanged(value)
    


  • @sierdzio said in How to connect qml button to c++ function:

    @zhmh said in How to connect qml button to c++ function:

    I do this but it does not work properly

    How does it fail? Is writeFlag() not called?

    Always write data in the file without pressing the button

    I guess what you mean to ask about is how to make sure atten spin box changes TestClass value when it's value is changed, right? There are many ways to do it, the easiest is to do this in QML:

    No I want the data to be written to a text file if the qml button is clicked and data is provided from valueChanged function


  • Moderators

    @zhmh said in How to connect qml button to c++ function:

    if(writeFlag) {

    Then this should be:

    if(wFlag)  {  
    


  • What zhmh is saying is you have a typo. You put the name of the function where you should have put the variable.

    I'm sure you know this, but just in case.


Log in to reply