Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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();
    }


  • Lifetime Qt Champion

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



  • This post is deleted!

  • Lifetime Qt Champion

    @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();
    }
    


  • @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....


  • Moderators

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


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


  • Moderators

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


  • Lifetime Qt Champion

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


Log in to reply