I have a task to save a file to computer. So this is my problem, when i write to file , it writes hex values.. I have no clue, what's wrong with my code. Here it is:
void MainWindow::on_actionSave_triggered()
{
QString filename = QFileDialog::getSaveFileName(
this,
tr("Save Document"),
QDir::currentPath(),
tr("Documents (*.txt)") );
QFile f( filename );
f.open( QIODevice::WriteOnly | QIODevice::Text );
QTextStream out(&f);
out << ui->textEdit->document();
}
QTextEdit's document method return QTextDocument, I think you want to use toPlainText method instead.
QTextEdit::document() will return a QTextDocument* which will be an Hex value (address). That's what you are adding in the file.
To get the contents from the QTextEdit use QString QTextEdit::toPlainText ()
HTH..
Related
I've got a path like "C:\Users\Rick\html.txt". So how can I separate the file path and filename? My main aim is to extract the file path and obtain "C:\Users\Rick" for a text editor app. I'm receiving the path as a QString from QFileDialog::getSaveFileName(). So I want to use that path as the window title and next path if the user tries to open a file
if(!checkTextField()){
QMessageBox::StandardButton reply = QMessageBox::question(this,this->appName,this->document_modified);
if(reply == QMessageBox::Yes){
QString filename = QFileDialog::getSaveFileName(this,"Save the File",QDir::homePath(),this->textFilter);
QFile file(filename);
if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
return;
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.flush();
file.close();
}
}```
You should look in to QFileInfo
QFileInfo info("c:/my/path/to/dir/file.txt");
info1.absoluteFilePath(); // returns "c:/my/path/to/dir/file.txt"
info1.dir(); // returns "c:/my/path/to/dir"
info1.filename(); // returns "file.txt"
How can i Write a new text without losing the previous value
QString mFilename2 = "bin/bin_2.txt";
File_main_Editor.stWrite(mFilename2,okline_Edit);
void stWrite(QString Filename,QString stringtext){
QFile mFile(Filename);
if(!mFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox message_file_Write;
message_file_Write.warning(0,"Open Error"
,"could not to open file for Writing");
return;
}
QTextStream out(&mFile);
out << stringtext;
out.setCodec("UTF-8");
mFile.flush();
mFile.close();
}
Every time that okline_Edit is initialized
stWrite function called a new value in the file is poured .txt previous value is lost.
Or in other words
You need to set QIODevice::Append when you open() the file.
mFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)
Additionally, if you want each append to be on a new line, you will have to insert a \n as well.
You open file with QIODevice::WriteOnly that would start writing...well from start, you need to open it with QIODevice::Append
I am making a text editor in Qt C++ and when I open a txt file I want to change the Title to the name of the file that is open I am aware of the setWindowTitle("title go here"). I was only able to display the path. here is a section of the function that open a new document.
QString fileName = QFileDialog::getOpenFileName(
this,
"TextEditor - Open" ,
"C:\\",
" Text File(*.txt);;All files (*.*)");
QFile file1(fileName);
if((!fileName.isEmpty()))
{
currentFile = fileName;
file1.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream in(&file1);
QString str1 = in.readAll();
ui->plainTextEdit-> setPlainText(str1);
file1.close();
statusBar()-> showMessage(" File successfully loaded! ");
saveRecent(currentFile);
}
setWindowTitle(currentFile);
I formatted your Code and added the Code needed to show the correct Filename including the extension and excluding the Path.
QString fileName = QFileDialog::getOpenFileName(
this,
"TextEditor - Open" ,
"C:\\",
" Text File(*.txt);;All files (*.*)");
QFile file1(fileName);
if(!fileName.isEmpty())
{
currentFile = fileName;
file1.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream in(&file1);
QString str1 = in.readAll();
ui->plainTextEdit-> setPlainText(str1);
file1.close();
statusBar()-> showMessage(" File successfully loaded! ");
saveRecent(currentFile);
}
// Create the FileInfo
QFileInfo file1Info(file1);
// now get the fileName
QString file1Name(file1Info.fileName());
// Set the Title to the fileName
setWindowTitle(file1Name);
See also the documentation of QFileInfo.fileName().
QFileInfo fileInfo(file1);
QString filename(fileInfo.fileName());
I wrote a Program in Qt 5.2.1 that writes some data into a file and now I want to read it and display it. ( in a text edit or any other widget)
Here is my code ( the part I thought is relevant ) -
But i don't get the desires result ... could you look into it and tell me what i an doing wrong
void MainWindow::on_Search_clicked()
{
QString name ;
name = ui->Search_name->text();
QFile readfile("data.txt");
if(!readfile.open(QIODevice::ReadOnly))
{
qDebug() << "error opening file: " << readfile.error();
return;
}
QTextStream instream(&readfile);
QString line = instream.readLine();
// ui->text is a QPlainTextEdit*
ui->text->insertPlainText(line);
readfile.close();
return;
}
You should use
void QPlainTextEdit::appendPlainText ( const QString & text ) [slot]
method, link.
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.