Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[question] how to copy a file from folder and paste it to other folder using buttons and treeview



  • This post is deleted!

  • Lifetime Qt Champion

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

    }


  • Lifetime Qt Champion

    QFile::copy for that part.

    But again, use QFileSystemModel, QDirModel has been obsoleted.



  • @SGaist I already tried QFile::copy but It also didn't help as I'm new to it.Will you please it make for me,or tell me the way to do as I'm struggling from two days.Please do a favor.


  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

    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


  • Lifetime Qt Champion

    You can use e.g. a QFileDialog for that



  • Already tried,It just gave the pop up window just like the file upload.


  • Lifetime Qt Champion

    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.


Log in to reply