How to display the output of the program mentioned below using a textBrowser ?



  • This program displays the output in a message box but instead I want it in a TextBrowser...

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QTextStream>
    #include <QMessageBox>
    #include <QFileDialog>
    #include <QDebug>
    #include "dia.h"
    #include"dai2.h"

    MainWindow::MainWindow(QWidget* parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow() {
    delete ui;
    }

    #include <QFile>

    void CompareRule1(int cline,QString OnePara ) {

    // check the rules
    if (OnePara.contains("//"))
    QMessageBox::warning(NULL, "My Application", QString("line %1 -ERROR.\nRule 1 Violated !").arg(cline) );

    }
    void CompareRule2(int cline,QString OnePara ) {

    // check the rules
    if (OnePara.contains("hai"))
    QMessageBox::warning(NULL, "My Application", QString("line %1 -ERROR.\nRule 2 Violated !").arg(cline) );

    }

    void MainWindow::on_pushButton_clicked()
    {
    QString fileName = QFileDialog::getOpenFileName(this, tr("Opentext File"),
    QDir::currentPath(),
    tr("text Files (*.txt)"));
    if (fileName.isEmpty()) {
    return;
    }
    else
    {
    dia d;
    d.setModal(false);
    d.exec();

    }
    QFile inputFile(fileName);
    QString Para;
    int lineCount = 0;
    if (inputFile.open(QIODevice::ReadOnly)) {
      QTextStream in(&inputFile);
      while (!in.atEnd())
      {
           lineCount++;
        QString line = in.readLine();
        qDebug() << lineCount << line;
        Para += line;
        qDebug()<<line;
        qDebug()<<Para;
        if (line!=NULL) { // we found blank line, so test and clear
                 (CompareRule1(lineCount,line));
                  (CompareRule2(lineCount,line));
    
    
    
                            qDebug() << "ParaRead: " << Para;
          Para = ""; //clear
    
      }
      }
      inputFile.close();
    }
    

    }



  • Hi

    You can write the messages you display with QMessageBox into a QTextDocument and display this QTextDocument with QTextBrowser

    QTextBrowser *browser = new QTextBrowser;
    //...
    browser->setDocument(new QTextDocument("<your message here>");
    

Log in to reply
 

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