[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) :
    ui(new Ui::TreeViewDirModelDialog)

    // Create and populate our model
    model = new QDirModel(this);
    // Enable modifying file system
    // Setup sort criteria
    // directory first, ignore case,
    // and sort by name
    model->setSorting(QDir::DirsFirst |
                      QDir::IgnoreCase |
    // QTreeView::setModel(QAbstractItemModel * model)
    // Reimplemented from QAbstractItemView::setModel().
    // Set initial selection
    QModelIndex index = model->index("C:/");
    // Set initial view of directory
    // for the selected drive as expanded
    // Make it scroll to the selected
    // Highlight the selected
    // Resizing the column - first column


    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;
        // directory
        // file


    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

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.