How read a file and print values in Qt5 ?



  • Hi all,

    I did a C++ program wich read a text file, transform it in "int" and print values.
    My question is how to do the same with Qt using Signal and Slots ?
    I don't have any idea of how to do it.

    This is my C++ code:

    @
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <stdlib.h>
    #include <unistd.h>

    #define TEMP_PHYSIC "/sys/class/hwmon/hwmon1/device/temp1_input"
    #define TEMP_CORE_1 "/sys/class/hwmon/hwmon1/device/temp2_input"
    #define TEMP_CORE_2 "/sys/class/hwmon/hwmon1/device/temp3_input"
    #define TEMP_CORE_3 "/sys/class/hwmon/hwmon1/device/temp4_input"
    #define TEMP_CORE_4 "/sys/class/hwmon/hwmon1/device/temp5_input"

    using namespace std;

    class Temp
    {
    public:

        string tempPhys();
        string tempCore1();
        string tempCore2();
        string tempCore3();
        string tempCore4();
    

    };

    string Temp::tempPhys()
    {
    ifstream fichierPhys(TEMP_PHYSIC);
    if(fichierPhys)
    {
    string lignePhys;
    while(getline(fichierPhys, lignePhys))
    {
    return lignePhys;
    }
    }
    }

    string Temp::tempCore1()
    {
    ifstream fichierCore1(TEMP_CORE_1);
    if(fichierCore1)
    {
    string ligneCore1;
    while(getline(fichierCore1, ligneCore1))
    {
    return ligneCore1;
    }
    }
    }

    string Temp::tempCore2()
    {
    ifstream fichierCore2(TEMP_CORE_2);
    if(fichierCore2)
    {
    string ligneCore2;
    while(getline(fichierCore2, ligneCore2))
    {
    return ligneCore2;
    }
    }
    }

    string Temp::tempCore3()
    {
    ifstream fichierCore3(TEMP_CORE_3);
    if(fichierCore3)
    {
    string ligneCore3;
    while(getline(fichierCore3, ligneCore3))
    {
    return ligneCore3;
    }
    }
    }

    string Temp::tempCore4()
    {
    ifstream fichierCore4(TEMP_CORE_4);
    if(fichierCore4)
    {
    string ligneCore4;
    while(getline(fichierCore4, ligneCore4))
    {
    return ligneCore4;
    }
    }
    }

    int main()
    {
    while(true)
    {
    Temp temp;
    int c0, c1, c2, c3, c4;

        c0 = atoi(temp.tempPhys().c_str());
        c1 = atoi(temp.tempCore1().c_str());
        c2 = atoi(temp.tempCore2().c_str());
        c3 = atoi(temp.tempCore3().c_str());
        c4 = atoi(temp.tempCore4().c_str());
    
        cout << "Temp. Physic : " << c0/1000 << " °c" << endl;
        cout << "====================" << endl;
        cout << "Temp. Core 1 : " << c1/1000 << " °c" << endl;
        cout << "Temp. Core 2 : " << c2/1000 << " °c" << endl;
        cout << "Temp. Core 3 : " << c3/1000 << " °c" << endl;
        cout << "Temp. Core 4 : " << c4/1000 << " °c" << endl;
        cout << "====================" << endl;
    
        usleep(100000);
        system&#40;"clear"&#41;;
    }
    return 0;
    

    }
    @

    Thanks a lot for your help.

    Regards,

    Chris.


  • Lifetime Qt Champion

    Hi,

    QObject for the base class
    QFile for the file reading part
    QTimer to trigger it at regular interval

    If you are using C++11 you can even use directly a lambda

    On the other hand you could also try QSocketNotifier so you should get notifications when there's new data to read

    Hope it helps


Log in to reply
 

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