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. Why it doesnt emit ?
QtWS25 Last Chance

Why it doesnt emit ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 5 Posters 481 Views
  • 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.
  • MontanaroM Offline
    MontanaroM Offline
    Montanaro
    wrote on last edited by Montanaro
    #1

    I m not able to understand why my code doents emit.

    My task is: reading battery level and showing immagine with battery level.

    So this i my code:

    battery.ccp

    Battery::Battery(QObject *parent) :
      QObject(parent),
        timer{ new QTimer(this) }
    {
    
        qDebug()<<"class Battery";
    
    
        connect(timer, &QTimer::timeout, this, &Battery::checkBatteryLevel);
    
        timer->start(std::chrono::milliseconds(2000));
    }
    
    
    
    
    
    
    
    void Battery::checkBatteryLevel()
    {     QFile batteryFile(BATTERY_FILE_NAME);
    
          if (batteryFile.open(QIODevice::ReadOnly | QIODevice::Text))
          {
              qDebug()<<"file open";
          }
          else
          {
              qDebug()<<"file not open";
              return;
    
          }
    
    
        if (QTextStream(&batteryFile).readAll().toInt() < 25)  {
            qDebug() << "first level battery";
            emit batteryLevel(1);
        }
    
        if (QTextStream(&batteryFile).readAll().toInt() > 25)  {
            qDebug()<< "second level battery";
            emit batteryLevel(2);
        }
    }
    

    main.qml

    ApplicationWindow {
        id: applicationWindow
        visible: true
        width: 419
        height: 138
        color: "black"
    
        // the current date and time
        property date now: null
    
    
        
        
        property int index: 0
        property var slidesPath: [
       
            "qrc:/res/icons/low_batt.png",
            "qrc:/res/icons/med_batt.png",
            "qrc:/res/icons/med_high_battery.png",
             "qrc:/res/icons/icons/no_app_found.png"
    
        ]
    
       Image {
            id: ic
            width:  90
            y: 22
            fillMode: Image.PreserveAspectFit
            source: slidesPath[index]  //
            height: width
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.verticalCenter: parent.verticalCenter
    
        }
    
    
         Text {
            y:   60
             text: new Date().toLocaleTimeString(Qt.LocaleDate)
    
             //  new Date().toLocaleDateString(Qt.locale("it_IT"))  //
            anchors.left: ic.right
            anchors.leftMargin:  1
            anchors.top: parent.top
            anchors.topMargin: 10
            anchors.verticalCenter: parent.verticalCenter
            font.bold: true
            font.pointSize: 14
            color: "white"
        }
    
    
         Text {
            y:   60
             text:  new Date().toLocaleDateString(Qt.locale("en_EN"))  //
            anchors.left: ic.right
            anchors.leftMargin: 1
            anchors.top: parent.top
            anchors.topMargin: 30
            anchors.verticalCenter: parent.verticalCenter
            font.bold: true
            font.pointSize: 14
            color: "white"
        }
    
    
         Text {
             id: txt
             text: "nome app variabile"
             height: 50
             font.capitalization: Font.AllUppercase
             color: "white"
             font.bold: true
             font.pointSize: 18 //36
             anchors.left: ic.right
             anchors.leftMargin: 1 //20
             anchors.topMargin: 70
             anchors.verticalCenter: parent.verticalCenter
             verticalAlignment: Text.AlignVCenter
         }
    
    
      Text {
               text: qsTr(".")
               anchors.centerIn: parent
               focus: true
    
      Keys.onPressed: {
                      if (event.key === Qt.Key_Up) {
    
                              if (index < slidesPath.length - 1)
                                  index++;
    
    
                              process.start("c:\\windows\\notepad.exe");
                              process.waitForFinished();
    
                                  index = 0;
    
    
                      } else if (event.key === Qt.Key_Down) {
    
                          if (index > 0)
                              index--;
    
                          else
                              index = 3;
                   }
          }
    
    
      }
    
    
      Battery {
          id: batteryLevel
    
      //  swipeView.currentIndex--;
          onValueChanged: {
              console.log("battery ");
                      switch (percent)
                      {case 1:
                           slidesPath[2];  //  source: "qrc:/res/icons/low_batt.png"
                            console.log("case 1 ");
                        break;
    
                      case 2:
                         applicationWindow.index=3;
                          console.log("case 2 ");
                        //  source: "qrc:/res/icons/med_batt.png"
                      break; }
      }
    

    My logs are:

    class Battery
    file open
    first level battery
    file open
    first level battery
    .........

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by fcarney
      #2

      You are looking at onValueChanged. Shouldn't you be looking at onBatteryLevelChanged? You are emitting batteryLevel(int).

      Edit: Or is it onBatteryLevel? Also, there is slot for QTimer::start that takes int as well as milliseconds.

      C++ is a perfectly valid school of magic.

      MontanaroM 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Beside the good suggestion of @fcarney, you might be in the unlucky case of the battery value being 25 thus nothing will be emitted.

        On a side note, it would make your code way more clean, readable and error proof to read and convert the value once (doing a conversion check would be good as well) and then apply your logic. You currently create two streams and read the file twice which is a bit inefficient and not clean from a maintenance point of view.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        MontanaroM 1 Reply Last reply
        1
        • fcarneyF fcarney

          You are looking at onValueChanged. Shouldn't you be looking at onBatteryLevelChanged? You are emitting batteryLevel(int).

          Edit: Or is it onBatteryLevel? Also, there is slot for QTimer::start that takes int as well as milliseconds.

          MontanaroM Offline
          MontanaroM Offline
          Montanaro
          wrote on last edited by
          #4

          @fcarney

          oh yes, with "onBatteryLevel" it works and I can use the switch.
          Mmm... now I have to know because I dont see slide :D

          yes I used timer of 2 seconds.
          I want to check battery level

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Beside the good suggestion of @fcarney, you might be in the unlucky case of the battery value being 25 thus nothing will be emitted.

            On a side note, it would make your code way more clean, readable and error proof to read and convert the value once (doing a conversion check would be good as well) and then apply your logic. You currently create two streams and read the file twice which is a bit inefficient and not clean from a maintenance point of view.

            MontanaroM Offline
            MontanaroM Offline
            Montanaro
            wrote on last edited by
            #5

            @SGaist
            I dont understand what you suggest.
            can you write example please ?

            @fcarney : ```
            applicationWindow.index= number;
            ` is correct to show slide :D

            jsulmJ 1 Reply Last reply
            0
            • MontanaroM Montanaro

              @SGaist
              I dont understand what you suggest.
              can you write example please ?

              @fcarney : ```
              applicationWindow.index= number;
              ` is correct to show slide :D

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Montanaro said in Why it doesnt emit ?:

              I dont understand what you suggest

              In your code you clearly read the content of the file two times:

              if (QTextStream(&batteryFile).readAll().toInt() < 25)  {
                  qDebug() << "first level battery";
                  emit batteryLevel(1);
              }
              if (QTextStream(&batteryFile).readAll().toInt() > 25)  {
                  qDebug()<< "second level battery";
                  emit batteryLevel(2);
              }
              

              It is enough and also faster to do it once. And toInt() has a bool parameter to check whther the content of the string could be converted to an int (if the string does not contain valid integer number toInt() will fail):

              bool ok{false};
              int value = QTextStream(&batteryFile).readAll().toInt(&ok);
              if (!ok) {
                  // Handle the error here
              }
              if (value < 25)  {
                  qDebug() << "first level battery";
                  emit batteryLevel(1);
              }
              if (value > 25)  {
                  qDebug()<< "second level battery";
                  emit batteryLevel(2);
              }
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              MontanaroM 1 Reply Last reply
              2
              • jsulmJ jsulm

                @Montanaro said in Why it doesnt emit ?:

                I dont understand what you suggest

                In your code you clearly read the content of the file two times:

                if (QTextStream(&batteryFile).readAll().toInt() < 25)  {
                    qDebug() << "first level battery";
                    emit batteryLevel(1);
                }
                if (QTextStream(&batteryFile).readAll().toInt() > 25)  {
                    qDebug()<< "second level battery";
                    emit batteryLevel(2);
                }
                

                It is enough and also faster to do it once. And toInt() has a bool parameter to check whther the content of the string could be converted to an int (if the string does not contain valid integer number toInt() will fail):

                bool ok{false};
                int value = QTextStream(&batteryFile).readAll().toInt(&ok);
                if (!ok) {
                    // Handle the error here
                }
                if (value < 25)  {
                    qDebug() << "first level battery";
                    emit batteryLevel(1);
                }
                if (value > 25)  {
                    qDebug()<< "second level battery";
                    emit batteryLevel(2);
                }
                
                MontanaroM Offline
                MontanaroM Offline
                Montanaro
                wrote on last edited by
                #7

                @jsulm

                oh thanks. you are right :)

                well... all my questions are solved ! thanks to all !

                I tryed to change "unsolved tag" into "solved tag but" but I m not able :/

                J.HilkJ 1 Reply Last reply
                0
                • MontanaroM Montanaro

                  @jsulm

                  oh thanks. you are right :)

                  well... all my questions are solved ! thanks to all !

                  I tryed to change "unsolved tag" into "solved tag but" but I m not able :/

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @Montanaro said in Why it doesnt emit ?:

                  I tryed to change "unsolved tag" into "solved tag but" but I m not able :/

                  bottom right, topic tools -> mark as solved


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  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