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

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";

Related

How to separate file name and file path in QT

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"

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

Copy path to QString

I need to copy the full filepath, without filename, into a QString from QFileDialog below.
QString fileName = QFileDialog::getOpenFileName(this,
tr("Select app to install"), '/' , tr("APK Files (*.apk)"));
You use QString QFileInfo::absolutePath() const for this. See the documentation for details.
QFileInfo fileInfo(QFileDialog::getOpenFileName(this,
tr("Select app to install"), '/' , tr("APK Files (*.apk)")));
qDebug() << fileInfo.absolutePath();

Qt - copy a file from one directory to another

I am using QT, I am not able to find out how to copy a file from one directory to another? How can I achieve this?
You can use QFile which provides a copy method.
QFile::copy("/path/file", "/path/copy-of-file");
If destination file exist, QFile::copy will not work. The solution is to verify if destination file exist, then delete it:
if (QFile::exists("/path/copy-of-file"))
{
QFile::remove("/path/copy-of-file");
}
QFile::copy("/path/file", "/path/copy-of-file");
The following code works in windows for specified reasons. This will set the path to specified drive and create the folder you created in Under UI Mode. Then copies the file from source to destination. Here the source is installation directory contained some files which are used for plotting curves. this file are not modified by users. They just use it.
hence this works as copy from installation directory to specified folder
void MainWindow::on_pushButton_2_clicked()
{
QString str5 = ui->lineEdit->text();
QString src = "."; QString setpath;
QDir dir(src);
if(!dir.exists()){
return;
}
dir.cdUp();
//dir.cdUp();
setpath = "E://";
dir.setPath(setpath);
QString dst_path = str5 + QDir::separator() ;
dir.mkpath(dst_path);
dir.cd(dst_path);
QString filename = "gnu.plt";
QString filename2 = "Load curve.plt";
QString filename3 = "tube temp.plt";
QFile file(filename);
QFile file1(filename2);
QFile file2(filename3);
file.copy(src+QDir::separator()+filename, setpath+QDir::separator()+str5+QDir::separator()+filename);
file1.copy(src+QDir::separator()+filename2, setpath+QDir::separator()+str5+QDir::separator()+filename2);
file2.copy(src+QDir::separator()+filename3, setpath+QDir::separator()+str5+QDir::separator()+filename3);
}

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.