Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [Solved] How to read sysfs entries from qt and put it into qprogressbar?
QtWS25 Last Chance

[Solved] How to read sysfs entries from qt and put it into qprogressbar?

Scheduled Pinned Locked Moved Mobile and Embedded
sysfsqt 5.2.1read
6 Posts 2 Posters 4.6k 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.
  • A Offline
    A Offline
    Ajith_P_V
    wrote on 15 Sept 2015, 12:18 last edited by Ajith_P_V
    #1

    Hi,
    I have a small application in which I need to get the sysfs (say example '/sys/class/power_supply/modelgauge_battery/capacity') details and put that one into QProgressbar.
    Since, sysfs entries are treated as files under linux, I have tried with accessing the sysfs entries using QFile.
    i.e. QFile file("/sys/class/power_supply/modelgauge_battery/capacity");
    Now, I want to read the value of the file and that value I need to pass into QProgressbar's value as below
    progressBar->setValue(<value_read_from_file>)

    I tried some experiments, but nothing yield to the desired result. Could anyone can help me to figure out how to achieve this.
    PS: the value which I need in this case is integer (setValue( ) accepts integer).

    Looking forward to your reply.
    Thank you in advance,
    Ajith P V

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 15 Sept 2015, 12:45 last edited by
      #2

      Can you post your code?
      What is the content of /sys/class/power_supply/modelgauge_battery/capacity?
      Reading from a file is as simple as file.readAll() and converting the QByteArray to what ever you need.
      If the content of the file is an integer number then you can do: file.readAll().toInt()

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

      A 1 Reply Last reply 16 Sept 2015, 04:59
      0
      • J jsulm
        15 Sept 2015, 12:45

        Can you post your code?
        What is the content of /sys/class/power_supply/modelgauge_battery/capacity?
        Reading from a file is as simple as file.readAll() and converting the QByteArray to what ever you need.
        If the content of the file is an integer number then you can do: file.readAll().toInt()

        A Offline
        A Offline
        Ajith_P_V
        wrote on 16 Sept 2015, 04:59 last edited by
        #3

        @jsulm Thank you for your reply.
        /sys/class/power_supply/modelgauge_battery/capacity shows the battery percentage in number when we run it from a console (terminal) with cat command. I would like to know whether I can take the 'sysfs' entries like how I'm doing.
        Please see my code snippet below (I'm using Qt Creator):

        QFile file("/sys/class/power_supply/modelgauge_battery/capacity");
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
        QDataStream in(&file);
        int battery_value;
        in >> battery_value;
        ui->progressBar->setValue(battery_value);

        I also tried file.readAll( ).toInt( ) earlier but the Progressbar shown 0 always (meaning not taking or invalid value I guess) in all cases.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 16 Sept 2015, 05:09 last edited by
          #4

          Well, there is probably a new-line at the end of the file content, try this:
          file.readAll( ).trimmed().toInt( );

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

          A 1 Reply Last reply 16 Sept 2015, 05:21
          1
          • J jsulm
            16 Sept 2015, 05:09

            Well, there is probably a new-line at the end of the file content, try this:
            file.readAll( ).trimmed().toInt( );

            A Offline
            A Offline
            Ajith_P_V
            wrote on 16 Sept 2015, 05:21 last edited by
            #5

            @jsulm said:

            file.readAll( ).trimmed().toInt( );

            Wow! just wow! It got solved :)

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 16 Sept 2015, 06:02 last edited by
              #6

              Nice to hear that it works now :-)

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

              1 Reply Last reply
              0

              1/6

              15 Sept 2015, 12:18

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved