How to set the QLineEdit with new line - c++

Currently I am copying the text to a LineEdit and on click PushButton it will write the text to a file which is "data.txt". I have written a readfile() which will read the text from data.txt and on click PushButton it should display the text in new line format at LineEdit.
Here is my code:
void MainWindow::writefile()
{
QString str = ui->lineEdit->text();
QString filename = "data.txt";
QFile file(filename);
file.open(QIODevice::WriteOnly|QIODevice::Text);
QTextStream out(&file);
out<<str<<endl;
file.close();
}
void MainWindow::readfile()
{
QString filename = "data.txt";
QFile file(filename);
file.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream in(&file);
QString str = in.readLine();
ui->lineEdit_2->setText(str);
file.close();
}
void MainWindow::on_pushButton_2_clicked()
{
readfile();
}
void MainWindow::on_pushButton_clicked()
{
writefile();
}
Please suggest how to separate those comma-separated strings and must display in new line format

The documentation of QLineEdit says:
A line edit allows the user to enter and edit a single line of plain text [...]
A related class is QTextEdit which allows multi-line, rich text editing.
Thus, you should use the QTextEdit widget instead of QLineEdit to allow multi-line text. It also has a setText, so you can use it the same way.
To replace commas with new line characters use the replace method:
// ...
QString str = in.readLine();
str = str.replace(",", "\n");
ui->textEdit_2->setText(str);
//...

Related

how to read only one line in a text file which has particular word using Qt

I am reading a .txt file, I want to read only 1 line with particular word. I'm using Qt Creator.
How can I achieve this?
This is my code so far:
QFile file("/home/a1.txt");
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this,"title","file not open");
}
QTextStream in(&file);
QString text = in.readAll();
ui->plainTextEdit->setPlainText(text);
file.close();
If you want to search all lines for a specific word you can do this
QFile file("/home/a1.txt");
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this,"title","file not open");
}
QTextStream in(&file);
QString text;
while (!in.atEnd()) {
text = in.readLine();
if (text.contains(/*Search her for the word you want.*/))
break;
}
ui->plainTextEdit->setPlainText(text);
file.close();
So you loop through all lines. And in every line you check if the word exist. If yes you can stop searching and leave the loop.
I hope this helps!

Qt GUI open a file using push button

I need to create qt gui push button with line edit, where I press the button which leads to browsing a folder to find a textfile I want to import. The textfile will be parsed afterwards. I would prefer to use combobox but I have no idea how to browse the folder through gui. Perhaps something like QDir related stuff should work but please help.
Basically, I want to import/open a textfile using push button/combobox.
What you are looking for is QFileDialog
connect the clicked() signal of your QPushButton to a slot that performs:
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Text file"), "", tr("Text Files (*.txt)"));
Then you can parse the file using for instance QFile and QTextStream:
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
process_line(line);
}
EDIT
If you want to parse a file where each line contains 31 floats that you want to store in a float data[31], I would first create the class:
struct FloatLine { float data[31]; };
Then store all the lines in a QList<FloatLine>, this way:
QList<FloatLine> floatLines;
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
QTextStream lineStream(&line);
floatLines << FloatLine();
for(int i=0; i<31; i++)
lineStream >> floatLines.last().data[i];
}
You might want to use QFileDialog, there are few example at that QtDocument.

change the name file putting the first line of my file C++ & Qt

I want to change the name of my file but I don't know how to do it. I have a .txt with words in two lines,and like to take the first line same as name of my file .txt.
This is my code:
void ventana1::on_ButtonGuardar_clicked()
{
QDir directory("C:/Users/Jaime/Desktop/interfaz/pacientes");
QString mFilename = directory.filePath("paciente.txt");
QFile sFile(mFilename);
if(sFile.open(QFile::WriteOnly | QFile::Text))
{
QTextStream out(&sFile);
out << ui.lineEdit_2->text()<< "\n"
<< ui.lineEdit->text();
sFile.flush();
sFile.close();
}
}
Simply replace
QString mFilename = directory.filePath("paciente.txt");
by
QString mFilename = directory.filePath(ui.lineEdit_2->text());
if you need to have the content of your lineEdit as your filename.

Qt QTextEdit loading just half text file

I have a problem: my project is a very simple one with a QTextEdit and a QSyntaxHighlighter, I'm trying to load a .cpp file and highlighting just the eighth line of that file, but the QTextEdit can't load the entire file if I ask it to highlight the line.
The following image shows the problem:
The relevant code of the application is the following:
void MainWindow::openFile(const QString &path)
{
QString fileName = path;
if (fileName.isNull())
fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), "", "C++ Files (*.cpp *.h)");
if (!fileName.isEmpty()) {
QFile file(fileName);
if (file.open(QFile::ReadOnly | QFile::Text))
editor->setPlainText(file.readAll());
QVector<quint32> test;
test.append(8); // I want the eighth line to be highlighted
editor->highlightLines(test);
}
}
and
#include "texteditwidget.h"
TextEditWidget::TextEditWidget(QWidget *parent) :
QTextEdit(parent)
{
setAcceptRichText(false);
setLineWrapMode(QTextEdit::NoWrap);
}
// Called to highlight lines of code
void TextEditWidget::highlightLines(QVector<quint32> linesNumbers)
{
// Highlight just the first element
this->setFocus();
QTextCursor cursor = this->textCursor();
cursor.setPosition(0);
cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, linesNumbers[0]);
this->setTextCursor(cursor);
QTextBlock block = document()->findBlockByNumber(linesNumbers[0]);
QTextBlockFormat blkfmt = block.blockFormat();
// Select it
blkfmt.setBackground(Qt::yellow);
this->textCursor().mergeBlockFormat(blkfmt);
}
However if you want to test the project with the cpp file I used (in the directory FileToOpen\diagramwidget.cpp), here's the complete source
http://idsg01.altervista.org/QTextEditProblem.zip
I've been trying to solve this for a lot of time and I'm starting to wonder if this isn't a bug or something similar
The QTextEdit can't accept such a big amount of text at one piece. Split it, for example like this:
if (!fileName.isEmpty()) {
QFile file(fileName);
if (file.open(QFile::ReadOnly | QFile::Text))
{
QByteArray a = file.readAll();
QString s = a.mid(0, 3000);//note that I split the array into pieces of 3000 symbols.
//you will need to split the whole text like this.
QString s1 = a.mid(3000,3000);
editor->setPlainText(s);
editor->append(s1);
}
It seems that the QTextEdit control needs time after each loading, setting a QApplication:processEvents(); after setPlainText() solves the problem although it's not an elegant solution.

Convert a multiline QString into a one line QString

I have something like this:
void ReadFileAndConvert ()
{
QFile File (Directory + "/here/we/go");
if(File.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream Stream (&File);
QString Text;
do
{
Text = Stream.readLine();
Text = Text.simplified();
// Here I want to convert the multiline QString Text into a oneline QString
// ...
}
The QString Text consists of a multiline Text that I need to convert into a online Text/QString. How can I achieve this? greetings
Put your text into a QStringList, and use QStringList::join(), e.g.
QStringList doc;
[...]
Text = Stream.readLine();
Text = Text.simplified();
doc << Text;
[...]
QString final = doc.join(" ");
You could use the readAll function of QTextStream in order to get a string containing all your text and then use the replace function of QString in order to remove new lines:
QString oneLineText = Stream.readAll().replace("\n"," ").simplified();
If you have a large file it is better to use the readLine function.