Qt 在处理程序中使用getOpenFileName
我已经在qt中的一个处理程序中实现了Qt 在处理程序中使用getOpenFileName,qt,handler,getopenfilename,Qt,Handler,Getopenfilename,我已经在qt中的一个处理程序中实现了getOPenFileName(更具体地说,是在一个按钮单击时实现的)。如何将生成的字符串保存在main中的QString而不是处理程序中?您可以使用信号连接将文件名的路径保存在QString变量中 const QString fileName = QFileDialog::getOpenFileName(0, tr("Select the file"), getLastDirectory(), "Txt Files (*.txt)"); if (fileNa
getOPenFileName
(更具体地说,是在一个按钮单击时实现的)。如何将生成的字符串保存在main中的QString
而不是处理程序中?您可以使用信号连接将文件名的路径保存在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);
在main函数中,您无法通过简单连接处理main类中的事件:
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访问变量