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 connect qml button to c++ function
Forum Updated to NodeBB v4.3 + New Features

How to connect qml button to c++ function

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 878 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.
  • zhmhZ Offline
    zhmhZ Offline
    zhmh
    wrote on last edited by zhmh
    #1

    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;
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @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)
      

      (Z(:^

      zhmhZ 1 Reply Last reply
      0
      • sierdzioS sierdzio

        @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)
        
        zhmhZ Offline
        zhmhZ Offline
        zhmh
        wrote on last edited by zhmh
        #3

        @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

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

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

          if(writeFlag) {

          Then this should be:

          if(wFlag)  {  
          

          (Z(:^

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jhorns2
            wrote on last edited by
            #5

            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.

            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