2017-07-05 33 views

回答

0

您可以使用信号连接来保存QString变量中文件名的路径。

const QString fileName = QFileDialog::getOpenFileName(0, tr("Select the file"), getLastDirectory(), "Txt Files (*.txt)"); 
if (fileName.isEmpty()) { 
    // No file was selected 
    return; 
} 
// then emit the signal 
emit fileWasSelected(fileName); 

在你的主要功能,你不能用一个简单的连接处理的主类的事件:

QObject::connect(yourClass, &YourClass::fileWasSelected, [&](const QString& filename) { 
    // Now, do what you want with your path 
}): 

的另一种方式,它保存在一个私有变量的文件,并成立了一个getter :

class MyClass { 
    .... 
public: 
    inline QString path() const { return _path; } 
private: 
    QString _path; 
} 

然后从main中获取变量。

+0

非常感谢! @Mohammed B. B. –