[question] how to copy a file from folder and paste it to other folder using buttons and treeview
-
This post is deleted!
-
Hi and welcome to devnet,
Take a look at QFileSystemModel + QTreeView. You have a good starting point from there to do what you want.
-
@SGaist Hi,
Please see and help,I already have made two buttons and there action i.e; make dir and delete dir .But I don't know how this copy paste will work.
ALso I already saw QFileSystemModel but didn't get much.#include "treeviewdirmodeldialog.h"
#include "ui_treeviewdirmodeldialog.h"TreeViewDirModelDialog::TreeViewDirModelDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::TreeViewDirModelDialog)
{
ui->setupUi(this);// Create and populate our model model = new QDirModel(this); // Enable modifying file system model->setReadOnly(false); // Setup sort criteria // directory first, ignore case, // and sort by name model->setSorting(QDir::DirsFirst | QDir::IgnoreCase | QDir::Name); // QTreeView::setModel(QAbstractItemModel * model) // Reimplemented from QAbstractItemView::setModel(). ui->treeView->setModel(model); // Set initial selection QModelIndex index = model->index("C:/"); // Set initial view of directory // for the selected drive as expanded ui->treeView->expand(index); // Make it scroll to the selected ui->treeView->scrollTo(index); // Highlight the selected ui->treeView->setCurrentIndex(index); // Resizing the column - first column ui->treeView->resizeColumnToContents(0);
}
TreeViewDirModelDialog::~TreeViewDirModelDialog()
{
delete ui;
}void TreeViewDirModelDialog::on_pushButton_clicked()
{
// Make directory
QModelIndex index = ui->treeView->currentIndex();
if(!index.isValid()) return;QString name = QInputDialog::getText(this, "Name", "Enter a name"); if(name.isEmpty()) return; model->mkdir(index, name);
}
void TreeViewDirModelDialog::on_pushButton_2_clicked()
{
// Delete directory// Get the current selection QModelIndex index = ui->treeView->currentIndex(); if(!index.isValid()) return; if(model->fileInfo(index).isDir()) { // directory model->rmdir(index); } else { // file model->remove(index); }
}
void TreeViewDirModelDialog::on_pushButton_3_clicked()
{}
void TreeViewDirModelDialog::on_pushButton_4_clicked()
{}
-
QFile::copy for that part.
But again, use QFileSystemModel, QDirModel has been obsoleted.
-
What did you write using QFile ?
-
void TreeViewDirModelDialog::on_pushButton_3_clicked()
{
QFile::copy("path_file", "copy");
}Sir if it is wrong,please guide me.
-
How do you determine the source and the target ?
-
I'm bit confused though,"index" is the source but don't know how to select "target".
Please code here :http://pastebin.com/1WnGkc5V -
You can use e.g. a QFileDialog for that
-
Already tried,It just gave the pop up window just like the file upload.
-
You should use QFileDialog::getExistingDirectory when you want to copy something somewhere.
But when doing such a design with buttons you would usually have two views like TotalCommander so the user can select the source and target there.