首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Qt -how从同一文件中的另一个函数获取变量值

Qt -how从同一文件中的另一个函数获取变量值
EN

Stack Overflow用户
提问于 2018-06-18 02:24:57
回答 1查看 252关注 0票数 0

Qt新手。还在学呢。我有clone.ui,clone.h和clone.cpp。克隆ui有两个按钮。

  1. 浏览按钮->选择目标路径
  2. 添加按钮->克隆(拷贝)文件

Clone.h

QString destination_path;
QFileDialog *fdialog;

Clone.cpp有

   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)  

 }`  

在相同的文件Clone.cpp中

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

    }

我敲了一下,有没有漏掉什么?任何想法/建议都非常有用。

EN

回答 1

Stack Overflow用户

发布于 2018-06-18 04:24:46

您已经创建了一个具有数据成员QString destination_path的类(Clone)。

因为它是一个成员变量,所以它有类作用域(就像在中一样,对于相同的Clone对象,可以在任何Clone::成员函数中访问相同的变量)。

问题是您通过在Clone::on_pushButton__Browse_clicked()中声明another QString destination_path来隐藏它。

void Clone::on_pushButton__Browse_clicked()
{  
    ...

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

    ...
} 

解决方案是从行首删除QString,这意味着您现在将赋值给类对象的数据成员。

void Clone::on_pushButton__Browse_clicked()
{  
    ...

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

    ...
} 

稍后,您可以在Clone::on_btn_Add_clicked()中访问destination_path,并在Clone::on_pushButton__Browse_clicked中为其赋值

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50899361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档