Qt新手。还在学呢。我有clone.ui,clone.h和clone.cpp。克隆ui有两个按钮。
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?
}
我敲了一下,有没有漏掉什么?任何想法/建议都非常有用。
发布于 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
中为其赋值
https://stackoverflow.com/questions/50899361
复制相似问题