Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 在处理程序中使用getOpenFileName_Qt_Handler_Getopenfilename - Fatal编程技术网

Qt 在处理程序中使用getOpenFileName

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

我已经在qt中的一个处理程序中实现了
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访问变量