Copy path to QString - c++

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

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

How to iterate through only certain type of files using QDirIterator

I want to iterate through only .xml files (all files under selected folder and its sub-directories), is that possible with QDirIterator?
QDirIterator iter( rootDir, QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
QString fileName;
while(iter.hasNext() )
{
qDebug() << iter.next();
fileName = iter.fileName();
// now I have to check if fileName is indeed a .xml extension
}
As can be seen in code above, if my iterator can jump to .xml files only than I don't have to check for file extension in the loop..is it possible?
One of the constructors of QDirIterator allows a nameFilters argument:
QDirIterator::QDirIterator ( const QString & path, const QStringList & nameFilters, QDir::Filters filters = QDir::NoFilter, IteratorFlags flags = NoIteratorFlags )
Constructs a QDirIterator that can iterate over path, using
nameFilters and filters.
The nameFilters argument is not properly documented, but there is a good chance it works like in QDir::setNameFilters.
QDirIterator it(rootdir, QStringList() << "*.xml", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
qDebug() << it.next();
}

Select file/directory path using QFileDialog in Qt

I want to show a dialog to user to select a file/directory in qt.
I tried using QFileDialog methods to get it, but either i can set file mode or directory mode, could not able to set both. if I set QFileDialog::Directory as file mode it shows directories as well as files, but could not able to select any file.
Here is the example code I tried...
QFileDialog dialog;
dialog.setFileMode(QFileDialog::Directory);
dialog.setOption(QFileDialog::DontUseNativeDialog,true);
dialog.setOption(QFileDialog::DontResolveSymlinks);
dialog.setNameFilterDetailsVisible(true);
dialog.setViewMode(QFileDialog::Detail);
QStringList filters;
filters <<"Any files (*)"
<<"Text files (*.txt)"
<<"Image files (*.png *.xpm *.jpg)";
dialog.setOption(QFileDialog::HideNameFilterDetails,false);
dialog.setNameFilters(filters);
int res = dialog.exec();
QDir directory;
QString file = directory.currentPath();
if (res)
{
directory = dialog.selectedFiles()[0];
QStringList filesList = directory.entryList(QDir::Files);
QString fileName;
foreach(fileName, filesList)
{
qDebug() << "FileName " << fileName;
}
}
Is there is any way get the path of selected file or directory ?

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