在处理程序中使用getOpenFileName

时间:2017-07-05 09:34:38

标签: qt handler getopenfilename

我在qt的处理程序中实现了getOPenFileName(更具体地说是when_pushbutton_clicked)。如何将生成的字符串保存在main中的QString而不是处理程序内部?

1 个答案:

答案 0 :(得分:0)

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

public static void main(String[] args) throws SQLException, ClassNotFoundException {
        String hdfsUri = "hdfs://localhost:9000/";
       //String dirName = args[0];
        String dirName=null;
      // String filename = args[1];
        String filename;

        if(args.length<=0) dirName = "ekbana"; filename = "text.csv";

        URL url = null;
        BufferedReader in = null;
        FileSystem hdfs = null;
        FSDataOutputStream outStream = null;
        HttpURLConnection conn = null;
        List<Map<String, String>> flatJson;
        Configuration con = new Configuration();
        try {
            url = new URL("http://crm.bigmart.com.np:81/export/export-sales-data.php?sdate=2016-12-01&edate=2016-12-02&key=jdhcvuicx8ruqe9djskjf90ueddishr0uy8v9hbjncvuw0er8idsnv");
        } catch (MalformedURLException ex) {
        }

        try {
            con.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
            con.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());
            hdfs = FileSystem.get(URI.create(hdfsUri), con); // this is line 44 
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            System.out.println(hdfs.mkdirs(new Path(hdfsUri + "/" + dirName)));
        } catch (IOException e) {
            e.printStackTrace();
        }

在您的main函数中,您无法通过简单的连接处理主类中的事件:

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);

另一种方式,它将文件保存在私有变量中,并设置一个getter:

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

然后从main访问变量。