0
我已经在qt中的处理程序中实现了getOPenFileName
(更具体地,在when_pushbutton_clicked
中)。如何将生成的字符串保存在主要的QString
中,而不是保存在处理程序中?在处理程序中使用getOpenFileName
我已经在qt中的处理程序中实现了getOPenFileName
(更具体地,在when_pushbutton_clicked
中)。如何将生成的字符串保存在主要的QString
中,而不是保存在处理程序中?在处理程序中使用getOpenFileName
您可以使用信号连接来保存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中获取变量。
非常感谢! @Mohammed B. B. –