1

I want to write a data to a file. I am formatting that Data using C++'s iomanip function, setw to set width of Text Field. Source code is as below:

std::ofstream dataFile;
dataFile.open(m_fileAbsolutePath.toStdString().c_str(),std::fstream::out);
dataFile << "|";
SET_WIDTH(dataFile,DEV_NAME_FIELD);
dataFile << std::right <<TS_HEADER;

The formatting looks good in Linux Text Editor "gedit". But, when I try to read this file and show in QT's QTextEdit, the formatting becomes Mess.

The width that was set using "C++'s setw" function, does not show up properly. Why is it so ? Do we need to do some configuration for QTextEdit ?

TextEdit is set as:

 ui->textEdit->setLineWrapMode(QTextEdit::NoWrap);

Code to read file:

std::ifstream dataFile (m_filePath.toStdString().c_str(), std::ifstream::in);

    if(dataFile.is_open()){

        while (!dataFile.eof()) {

            char line[1024] = {0};
            dataFile.getline(line,1024);
            int numberOfBytes = dataFile.gcount();
            if(0 < numberOfBytes){
                QString result(line);
                Q_EMIT resultReady(result);
            }
        }
        dataFile.close();
    }

Thanks in advance.

Coder
  • 845
  • 1
  • 10
  • 20

1 Answers1

1

Thanks Joachim Pileborg. Resolved by setting Fixed width font. This solution works only with QT 5.2 and above.

Code:

    const QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
Coder
  • 845
  • 1
  • 10
  • 20