qt - QFileDialog : get file name -
i using qt 5 , qfiledialog. want restrict user give forward slash (/) in file name.
i have below code qfiledialog save file name.
qfiledialog saveasdialog(this); qstring filename = saveasdialog.getsavefilename(this, tr("save file"), ".", tr("files (*.csv)"));
in dialog, if user gives file name "abc.csv" in "download" folder "getsavefilename" returns "/home/user/downloads/abc.csv" correct.
but question when user give forward slash in file name (/) not behaving correctly.
e.g. if user want give file name "abc/xyz.csv" not getting correct file name.
how correct file name "abc/xyz.csv" when user click "ok"?
please watch on wikipedia link.
as can see, /
character prohibited in file name. file name abc/xyz.csv
incorrect.
also can check wich current directory in file dialog, , based on information can track selected file name. try this:
qfiledialog saveasdialog(this); qstring curdir(qdir('.').absolutepath()); connect(&saveasdialog, &qfiledialog::directoryentered, [&curdir](const qstring& dir) { curdir = dir; }); // cann't use static member getsavefilename //qstring filename = saveasdialog.getsavefilename(this, tr("save file"), ".", tr("files (*.csv)")); //here manual saveasdialog setup ........... if (saveasdialog.exec() == qdialog::accepted) { qstring filename = saveasdialog.selectedfiles().first(); //add check list not empty!!! qstring f = filename.replace(curdir, ""); }
Comments
Post a Comment