0

New to Qt. Still learning it. I have clone.ui, clone.h and clone.cpp. clone ui has 2 buttons.

  1. Browse button-> to Selection a destination path
  2. Add button -> Clone(copy) a file

Clone.h

QString destination_path;
QFileDialog *fdialog;

Clone.cpp has

   QFileInfo finfo; // Declare outside function to increase scope
   QString destination_name; 

void Clone:: on_pushButton__Browse_clicked()
{  
  /*get the destination path in QString using QFileDialog 
    Got destination_path */

      QString destinatino_path = QFileDialog::getExistingDirectory(....);
      QFile finfo(destination_path);
     // QFileDialog  finfo(destionation_path)  

 }`  

In the same file Clone.cpp

    void Clone:: on_btn_Add_clicked()
   { 
      // how to get the  same destination_path value here... 
      //using QFile or some other way?    

    }

I struck here, Am i missing anything? Any thoughts/suggestion highly useful.

2
  • getExistingDirectory returns a directory, and you want to copy a file, what is the file you want to copy? Commented Jun 17, 2018 at 19:47
  • want to copy executable file. Once got directory path, file path will be "directory path+ executable file" Commented Jun 18, 2018 at 3:43

1 Answer 1

2

You've create a class (Clone) which has a data member QString destination_path.

Since it is a member variable it has class scope (as in you can access the same variable in any Clone:: member function for the same Clone object).

The problem is that you've hidden it by declaring another QString destination_path in Clone::on_pushButton__Browse_clicked().

void Clone::on_pushButton__Browse_clicked()
{  
    ...

    // this *hides* the class member with the same name
    QString destination_path = QFileDialog::getExistingDirectory(....); 

    ...
} 

The solution is to remove QString from the beginning of the line, which means you are now assigning to the class object's data member.

void Clone::on_pushButton__Browse_clicked()
{  
    ...

    // now you're assigning to your object's data member
    destination_path = QFileDialog::getExistingDirectory(....); 

    ...
} 

Later, in Clone::on_btn_Add_clicked() you can access destination_path, and it will have the value assigned to it in Clone::on_pushButton__Browse_clicked

Sign up to request clarification or add additional context in comments.

1 Comment

yes,correct. After removing QString , worked as expected :) Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.