How to separate file name and file path in QT - c++

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"

Related

Qt - How to copy file with QFile::copy using QFileDialog?

QString filename = QFileDialog::getOpenFileName(this,tr("Pdf files"), "C:/", "books(*.pdf)");
I want to get the selected file from QFileDialog and copy it to my desktop. Can I use something like this?
QFile::copy(filename,"desktop");
You need to get the path to the desktop using QStandardPaths, and then use that path in your call to QFile::copy.
Assuming you want to preserve the file name while copying, your code will look something like this:
QString filePath = QFileDialog::getOpenFileName(this ,
QObject::tr("Pdf files"),
"C:/", "books(*.pdf)");
QFileInfo fi(filePath);
QString fileName= fi.fileName();
QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
QString destinationPath= desktopPath+QDir::separator()+fileName;
if(QFile::copy(filePath, destinationPath))
qDebug() << "success";
else
qDebug() << "failed";

Change main window title in Qt

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());

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 C++ writing data in a file , unexpected output

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..