Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How QFile can read/write 8 different outputs/inputs using for loop.
QtWS25 Last Chance

How QFile can read/write 8 different outputs/inputs using for loop.

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 496 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.
  • MijazM Offline
    MijazM Offline
    Mijaz
    wrote on last edited by
    #1

    I will give inputs at Run-time from GUI LineEdit, as follows

    Take input from these lineEdits:

    ui->lineEdit_1->text();
    ui->lineEdit_2->text();
    ui->lineEdit_3->text();
    ui->lineEdit_4->text();
    ui->lineEdit_5->text();
    ui->lineEdit_6->text();
    ui->lineEdit_7->text();
    ui->lineEdit_8->text();

    Take outputs from these lineEdits;
    ui->lineEdit_11->text();
    ui->lineEdit_12->text();
    ui->lineEdit_13->text();
    ui->lineEdit_14->text();
    ui->lineEdit_15->text();
    ui->lineEdit_16->text();
    ui->lineEdit_17->text();
    ui->lineEdit_18->text();

    My Target:
    // I want to update same register(out_altvoltage1_TX_LO_frequency) for 8 different inputs (lineEdit_1 to 8) and display outputs in 8 different lineEdits(11 to 18) accordingly.

    // for single operation code is given below which is working correctly.

    ui->lineEdit_1->text();
    //write value
    QFile f1("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
    if (f1.open(QIODevice::WriteOnly | QIODevice::Text)) {
    f1.write(ui->lineEdit_1->text().toLatin1());
    f1.close();
    }
    //read value
    QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
    if (f.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    QByteArray ba = f.readAll();
    ui->lineEdit_11->setText(QString(ba));
    f.close();
    }

    jsulmJ 1 Reply Last reply
    0
    • MijazM Mijaz

      I will give inputs at Run-time from GUI LineEdit, as follows

      Take input from these lineEdits:

      ui->lineEdit_1->text();
      ui->lineEdit_2->text();
      ui->lineEdit_3->text();
      ui->lineEdit_4->text();
      ui->lineEdit_5->text();
      ui->lineEdit_6->text();
      ui->lineEdit_7->text();
      ui->lineEdit_8->text();

      Take outputs from these lineEdits;
      ui->lineEdit_11->text();
      ui->lineEdit_12->text();
      ui->lineEdit_13->text();
      ui->lineEdit_14->text();
      ui->lineEdit_15->text();
      ui->lineEdit_16->text();
      ui->lineEdit_17->text();
      ui->lineEdit_18->text();

      My Target:
      // I want to update same register(out_altvoltage1_TX_LO_frequency) for 8 different inputs (lineEdit_1 to 8) and display outputs in 8 different lineEdits(11 to 18) accordingly.

      // for single operation code is given below which is working correctly.

      ui->lineEdit_1->text();
      //write value
      QFile f1("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
      if (f1.open(QIODevice::WriteOnly | QIODevice::Text)) {
      f1.write(ui->lineEdit_1->text().toLatin1());
      f1.close();
      }
      //read value
      QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
      if (f.open(QIODevice::ReadOnly | QIODevice::Text))
      {
      QByteArray ba = f.readAll();
      ui->lineEdit_11->setText(QString(ba));
      f.close();
      }

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

      @Mijaz said in How QFile can read/write 8 different outputs/inputs using for loop.:

      for single operation code is given below which is working correctly.

      And what exactly is the problem putting this code into a loop? If you can do it for one you can do it for many. Please at least try to put it in a loop by yourself.

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

      MijazM 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Mijaz said in How QFile can read/write 8 different outputs/inputs using for loop.:

        for single operation code is given below which is working correctly.

        And what exactly is the problem putting this code into a loop? If you can do it for one you can do it for many. Please at least try to put it in a loop by yourself.

        MijazM Offline
        MijazM Offline
        Mijaz
        wrote on last edited by Mijaz
        #3
        This post is deleted!
        jsulmJ JonBJ 2 Replies Last reply
        0
        • MijazM Mijaz

          This post is deleted!

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

          @Mijaz The code you posted does not make any sense!
          QFile opens a file (file path as string) not an integer!
          You don't have to open the file inside the loop!
          Even your for loops are wrong - you should learn C++.
          PLEASE post your code as TEXT and not scrennshots!
          It's really not that hard:

          QFile f("HERE YOUR PATH");
          if (f.open(...)) {
              for (int i = 0; i < 9; ++i) {
                  f.write(...);
              }
              f.close();
          }
          

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

          1 Reply Last reply
          7
          • MijazM Mijaz

            This post is deleted!

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Mijaz
            @jsulm's comment above is the most important.

            But what are the 16 statements like ui->Pro1_Freq->text() doing? What do they achieve? The text() method returns the text, so these statements are pointless, and I don't know what you intend them for. If I didn't know better, I'd wonder if they are all supposed to be setText(""), but who knows....

            1 Reply Last reply
            0
            • J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

              @Mijaz

              because it pains my soul:

              but it sill makes little sense to me.
              What are you actually trying to do?

              //Inputs
                 const QVector<QLabel*> inputs{
                              ui->Pro1_Freq,
                              ui->Pro2_Freq,
                              ui->Pro3_Freq,
                              ui->Pro4_Freq,
                              ui->Pro5_Freq,
                              ui->Pro6_Freq,
                              ui->Pro7_Freq,
                              ui->Pro8_Freq,
                              ui->Pro9_Freq
                  };
                  // for outputs
                 const QVector<QLabel*> outputs{
                      ui->Pro1_status,
                      ui->Pro2_status,
                      ui->Pro3_status,
                      ui->Pro4_status,
                      ui->Pro5_status,
                      ui->Pro6_status,
                      ui->Pro7_status,
                      ui->Pro8_status
                  };
              
                  QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
                  if (f.open(QIODevice::WriteOnly | QIODevice::Text)) {
                      for(QLabel *l : inputs) {
                              f.write(l->text().toLatin1());
                      }
                  }
                  f.close();
              
                  //this makes little sense, but it is, what you wrote
                  f.setFileName("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
                  if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
                      for(QLabel *l : outputs) {
                              l->setText(f.readAll());
                              l->setStyleSheet("background-color: rgb(0,0,0)");
                      }
                  }
                  
                  f.close();
              

              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.

              JonBJ 1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                @Mijaz

                because it pains my soul:

                but it sill makes little sense to me.
                What are you actually trying to do?

                //Inputs
                   const QVector<QLabel*> inputs{
                                ui->Pro1_Freq,
                                ui->Pro2_Freq,
                                ui->Pro3_Freq,
                                ui->Pro4_Freq,
                                ui->Pro5_Freq,
                                ui->Pro6_Freq,
                                ui->Pro7_Freq,
                                ui->Pro8_Freq,
                                ui->Pro9_Freq
                    };
                    // for outputs
                   const QVector<QLabel*> outputs{
                        ui->Pro1_status,
                        ui->Pro2_status,
                        ui->Pro3_status,
                        ui->Pro4_status,
                        ui->Pro5_status,
                        ui->Pro6_status,
                        ui->Pro7_status,
                        ui->Pro8_status
                    };
                
                    QFile f("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
                    if (f.open(QIODevice::WriteOnly | QIODevice::Text)) {
                        for(QLabel *l : inputs) {
                                f.write(l->text().toLatin1());
                        }
                    }
                    f.close();
                
                    //this makes little sense, but it is, what you wrote
                    f.setFileName("/sys/bus/iio/devices/iio:device1/out_altvoltage1_TX_LO_frequency");
                    if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
                        for(QLabel *l : outputs) {
                                l->setText(f.readAll());
                                l->setStyleSheet("background-color: rgb(0,0,0)");
                        }
                    }
                    
                    f.close();
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @J-Hilk
                Above your second loop, I think you mean QIODevice::ReadOnly rather than QIODevice::WriteOnly.

                J.HilkJ 1 Reply Last reply
                1
                • JonBJ JonB

                  @J-Hilk
                  Above your second loop, I think you mean QIODevice::ReadOnly rather than QIODevice::WriteOnly.

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

                  @JonB absolutely, copy past error, also missed a ;


                  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.

                  mrjjM 1 Reply Last reply
                  2
                  • J.HilkJ J.Hilk

                    @JonB absolutely, copy past error, also missed a ;

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @J-Hilk
                    Thanks it hurt my soul too :)

                    1 Reply Last reply
                    1

                    • Login

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