qtqprocessqtexteditqtembedded

Need to display in qtextEdit real time


I have an arm board with a touchscreen display, where I want to display the output from a certain function, vcm_test(). The output of this function is saved to a file called test.txt . Now I am able to read the contents of the file test.txt and display it in my qtextEdit only if it is less than 50-60 lines. Whereas I have more than 7000 lines in the test.txt . When I try to display 7000 lines the arm board keeps reading and nothing is displayed until reading is complete. Is there any way to read and display after every line or say every 10 lines. I thought of using qProcess in readfile too, but I have no idea how I can do that.

    connect(ui->readfil, SIGNAL(clicked()), SLOT(readfile()));
    connect(ui->VCMon, SIGNAL(clicked()), SLOT(vcm_test()));
    connect(ui->Offloaderon, SIGNAL(clicked()), SLOT(offloader_test()));
    connect(ui->quitVCM, SIGNAL(clicked()),vcmprocess, SLOT(kill()));
    connect(ui->quitoffloader, SIGNAL(clicked()),offloaderprocess, SLOT(kill()));}
    MainWindow::~MainWindow(){
        delete ui;}
    void MainWindow::readfile(){
        QString filename="/ftest/test.txt";
        QFile file(filename);
        if(!file.exists()){
            qDebug() << "NO file exists "<<filename;}
        else{
            qDebug() << filename<<" found...";}
        QString line;
        ui->textEdit->clear();
        if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
            QTextStream stream(&file);
            while (!stream.atEnd()){
                line = stream.readLine();
                ui->textEdit->setText(ui->textEdit->toPlainText()+line+"\n");
                qDebug() << "line: "<<line;}
    }
    file.close();}
    void MainWindow::vcm_test(){
        vcmprocess->start("/ftest/vcm_test_2");}
    void MainWindow::offloader_test(){
        offloaderprocess->start("/ftest/off_test_2");}

Any advice is really appreciated.Thanks.


Solution

  • You could use QApplication::processEvents() after reading every line and appending it to your text edit. But you should be really careful when using this, and I would not recommend doing so. You should also consider using QTextEdit::Append() instead of setText.

    A better solution is to read the file in another thread and use signals and slots to send read data that you want to append to your QTextEdit.