I want to move a directory. My selected directory contains many sub directories and files.
How can I implement the same using Qt?
QDir::rename does it in the most cases. Following example moves theDir incl content from source to dest:
QString original = "/home/test/source/theDir";
QString dest = "/home/test/temp";
QDir dir;
if( !dir.rename( original, dest ) ){
throw Exception( "move failed" );
}
Related
I am trying to extract .DAT file using Quazip library in Qt 5.14. I integrated this quazip library into my project and try to extract files using it. .txt file is get extracted but .DAT file is not get extracted. .DAT files are created but it does not contain any data.
Here is my source code
bool fileHelper::extractAll( QString folderPath, QString filePath ) {
QuaZip zip(filePath);
zip.open(QuaZip::mdUnzip);
bool isSuccess = false;
for(bool f=zip.goToFirstFile(); f; f=zip.goToNextFile())
{
// set source file in archive
QString filePath = zip.getCurrentFileName();
QuaZipFile zFile( zip.getZipName(), filePath );
// open the source file
zFile.open( QIODevice::ReadOnly );
// create a bytes array and write the file data into it
//QByteArray ba = zFile.read()
QByteArray ba = zFile.readAll();
// close the source file
zFile.close();
// set destination file
//QFile dstFile( getfileStoreRootDir()+filePath );
QFile dstFile( folderPath+filePath );
qDebug() << "dstFile :" << dstFile;
// open the destination file
dstFile.open( QIODevice::WriteOnly | QIODevice::Text );
// write the data from the bytes array into the destination file
dstFile.write( ba.data() );
//close the destination file
dstFile.close();
//mark extraction sucess
isSuccess = true;
}
zip.close();
return isSuccess; }
Please tell me am I doing something wrong or any other extra flag or something is required for it.
I am trying to list all subdirectories which are inside a directory(which is chosen from combobox). Now I can show list of directories in combobox . I want to select one of those directory and list every sub-dirs( inside this selected dir) in textedit.
I don't know how can I list the subdirectories when I choose a directory from combobox.Here is my current code.
QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home", QFileDialog::ShowDirsOnly| QFileDialog::
DontResolveSymlinks);
ui->comboBox->setMinimumWidth(500);
QStringList files = directory.entryList(QDir::Files);
ui->comboBox->addItems(files);
I would appreciate any help. Thanks
I got the answer .
void Combo_box::on_pushButton_clicked()
{
QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",
QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
ui->comboBox->setMinimumWidth(500);
for(const QFileInfo & finfo: directory.entryInfoList()){
ui->comboBox->addItem(finfo.absoluteFilePath());
}
connect(ui->comboBox, &QComboBox::currentTextChanged,[this](const QString &selectedDirectory) {
QDirIterator it(selectedDirectory,QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
ui->textEdit->clear();
while (it.hasNext()){
ui->textEdit->append(it.next());
}
});
}
I'm looking for the error I made on this code, but I can not find any solution since hours..
This function should simpli save a file to a directory:
void MyClass::saveSettingsToFile(QString file_name)
{
QString path;
path = dir.append(file_name);
QFile my_file(path);
if (!my_file.open(QFile::WriteOnly))
{
qDebug() << "Could not open file for writing";
}
QTextStream out(& my_file);
out << "some text \n"
my_file.flush();
my_file.close();
path = "";
file_name ="";
}
Where dir is a QString containing the directory, file_name is gathered from a lineEdit field.
When I first call the function with, for example file_name = "aaaa.txt", I find this aaaa.txt in the specified directory. All right.
When then I call again the function with file_name = "bbbb.txt", I find in the specified directory this file: aaaa.txtbbbb.txt, instead of I
bbbb.txt
It seems to me a very s****d error, but I cannot find what!
EDITED: there was this mistake QTextStream out(& path); instead of QTextStream out(& my_file);
You are modifying dir variable with QString::append. Variable dir is obviously a class member of MyClass. Try this instead:
void MyClass::saveSettingsToFile(QString file_name)
{
QString path(dir);
path.append(file_name);
QFile my_file(path);
//...
}
The QString::append function modify the parameter value itself as you can see in the documentation: http://doc.qt.io/qt-5/qstring.html#append
Example:
QString x = "free";
QString y = "dom";
x.append(y);
// x == "freedom"
So, what happens is that it keeps appending the content to the dir variable, not only assigning the result to path.
I am trying to make a simple widget which contains a lineedit which shows the file name and a button to open a filedialog.
and now I want to check if the file-extension is valid, in this case, a image file ending with jpg, png or bmp. I solved this with QFileInfo and QList, this code is in my btn_clicked slot:
QString filename = QFileDialog::getOpenFileName(this, tr("Select an image File", "", tr("Image Files (*.bmp *.jpg *.png);; All Files(*)"));
QList<QString> ext_list;
ext_list<<"bmp"<<"jpg"<<"png";
QFileInfo fi(filename);
QString ext = fi.suffix();
if (ext_list.contains(ext)){
// lineedit->setText(filename);
}
else {
QMessageBox msgBox;
msgBox.critical(0, "Error", "You must select a valid image file");
it works, but is there a more simple/elegant way to achieve the goal? Thx for your help.
You might be interested by the setNameFilters function : http://doc.qt.io/qt-5/qfiledialog.html#setNameFilters
Update
If you want to filter images without naming each extensions, you should use QMimeDatabase. This will allow you to define your filter for the QFileDialog and then get a list of extensions to check.
For example with jpeg and png:
QStringList mimeTypeFilters;
mimeTypeFilters << "image/jpeg" << "image/png";
QFileDialog fd;
fd.setFileMode(QFileDialog::ExistingFile);
fd.setMimeTypeFilters(mimeTypeFilters);
fd.exec();
QString filename = fd.selectedFiles().count() == 1 ? fd.selectedFiles().at(0) : "";
QMimeDatabase mimedb;
if(!mimeTypeFilters.contains(mimedb.mimeTypeForFile(filename).name()))
{
// something is wrong here !
}
What I am trying to do I need to split my large style file to small style files
because now it is hard to read the style file(qss file) and add new styling to it.
after that i need to load those small qss files to apply them all
I am loading my Big file by calling function on main I have created
void Utilities::loadEnglishStyle()
{
QFile file(":/EnglishClasses.qss");
file.open(QFile::ReadOnly);
QString StyleSheet = QLatin1String(file.readAll());
qApp->setStyleSheet(StyleSheet);
file.close();
}
I thought about split the big file, add the small files to the resources, open all of them by QFile and after that concatenate them all in one string
but every time when adding new qss file i still need to do the same process again
Is there any efficient way to do this ?!!
Your approach sounds correct. The process is not as complex as it sounds. The only thing you need is to add the "smaller" qss file in the resources, under a specific prefix (eg. stylesheets), and then automatically load and concatenate all these files. Sample code follows:
QDir stylesheetsDir(":/stylesheets");
QFileInfoList entries = stylesheetsDir.entryInfoList();
QString completeStylesheet = "";
foreach (QFileInfo fileInfo, entries)
{
QFile file(":/stylesheets/" + fileInfo.fileName(););
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
continue;
QTextStream in(&file);
completeStylesheet += in.readAll()
}
you means some API shoud add as:
QWidget::addStyleSheet(StyleSheet);
QWidget::clearStyleSheet(StyleSheet);