Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [question] how to copy a file from folder and paste it to other folder using buttons and treeview

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 2 Posters 4.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1
    This post is deleted!
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Take a look at QFileSystemModel + QTreeView. You have a good starting point from there to do what you want.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      ? 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Take a look at QFileSystemModel + QTreeView. You have a good starting point from there to do what you want.

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #3

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

        }

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          QFile::copy for that part.

          But again, use QFileSystemModel, QDirModel has been obsoleted.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          ? 1 Reply Last reply
          0
          • SGaistS SGaist

            QFile::copy for that part.

            But again, use QFileSystemModel, QDirModel has been obsoleted.

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What did you write using QFile ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by A Former User
                #7

                void TreeViewDirModelDialog::on_pushButton_3_clicked()
                {
                QFile::copy("path_file", "copy");
                }

                Sir if it is wrong,please guide me.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  How do you determine the source and the target ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by A Former User
                    #9

                    I'm bit confused though,"index" is the source but don't know how to select "target".
                    Please code here :http://pastebin.com/1WnGkc5V

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      You can use e.g. a QFileDialog for that

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          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.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved