Qt remembering the last open folder - c++

I am using QFileDialog::openfilename for taking a file from user as input and I have specified the default folder which is to be shown when user open dialog.
But qt is somehow remembering the last open folder when filedialog is opened multiple times. But I want the default folder to be the the initial folder shown to the user not the last opened folder. In this, I am doing nothing explicitly to store the last opened information anywhere.
Please tell me what is the problem here and how to fix this.

It is clearly documented here. The third parameter to getOpenFileName is dir.
The file dialog's working directory will be set to dir. If dir includes a file name, the file will be selected.

Use this. setDirectory(str); install default path and you never get the last opened directory.
void MainWindow::on_pushButton_clicked()
{
QFileDialog dia;
dia.setDirectory("D:/");//or another default folder
QString path1 = dia.getOpenFileName(this,"Choose file");
}

Try Qsettings with rewritting them down, you'll need a initialize with param path
void camera_index::writesetting_window() {
//camera_index page
QSettings settings("Moose Soft", "Clipper");
settings.setValue("set_FOCUS", ui->camera_focus->value());
}
void camera_index::readsetting_window() {
QSettings settings("Moose Soft", "Clipper");
int FOCUS = settings.value("set_FOCUS").toInt();}
basically just set a fix value would help

Related

Create a TAnimate in Borland 2006

I have the following problem. I am using the Borland 2006 Compiler and I am trying to include an animation in my applicaton. First I added the TAnimate Object and then in the ObjectInspector under "FileName", every time I try to add an .avi, the Compiler says "AVI cannot be opened". Am I doing something wrong or isn't it that simple to just put a .gif or .avi into that Objectproperty?
Edit // Here ist Some CodeExample, everytime i press the button, it throws an exception and tells me that the avi File cannot be opened
void __fastcall THauptmenue_Login::Button1Click(TObject *Sender)
{
Animate1->FileName = ("C:\\Users\\Kevin\\Desktop\\C++ Gifs");
}
The FileName you showed doesn't look complete. It looks more like a path to a folder instead of a file.
In the Object Inspector, beside the FileName text box is a [...] button which brings up a file browser dialog which can add a full path and name to a file.
addendum:
If you are using the Object Inspector to pick a filename from the disk you do not need to specify a FileName property value in the code.
This overwrites any previous FileName property value.
Animate1->FileName = "C:\\Users\\Kevin\\Desktop\\C++ Gifs";

QFileDialog: Selecting directories and files

I'm using the code below to build a qstringlist of filenames:
QStringList filenames = QFileDialog::getOpenFileNames(this,"",QDir::currentPath() );
How can I change this so I can select directories as well?
I looked at:
dialog.setFileMode(QFileDialog::AnyFile);
but I don't get how to use it with my code.
This code snippet linked in the comment above solves my issue.
QFileDialog* _f_dlg = new QFileDialog(this);
_f_dlg->setFileMode(QFileDialog::Directory);
_f_dlg->setOption(QFileDialog::DontUseNativeDialog, true);
// Try to select multiple files and directories at the same time in QFileDialog
QListView *l = _f_dlg->findChild<QListView*>("listView");
if (l) {
l->setSelectionMode(QAbstractItemView::MultiSelection);
}
QTreeView *t = _f_dlg->findChild<QTreeView*>();
if (t) {
t->setSelectionMode(QAbstractItemView::MultiSelection);
}
int nMode = _f_dlg->exec();
QStringList _fnames = _f_dlg->selectedFiles();
I tried this, but the result in my case is a bit strange: I can select a combination of folders and files, as long as the first selected item is a folder.
So when I select a folder, then a file and again a folder, I can proceed clicking the button and retrieving the results: see screenshot in link below.
First folder selected, then file: OK
However, when the first item is a file (followed by a folder, or just a file), the button to proceed is not available... So just selecting one or multiple files is not available to me in this implementation it seems, as can be seen in the other screenshot:
First file selected, then folder: not able to proceed
Is there any way using the same code using QFileDialog that allows you to
select one or more files without selecting folders
selecting one or more folders without selecting files
selecting a combination of files and folders regardless of selection order

QFileDialog: add suffix after selecting file

I need to add suffix to selected filename in QFileDialog with QFileDialog::AcceptSave accept mode. For example, after selecting "1.txt" file in QFileDialog edit should be select "1_suffix.txt". It should be added before file accepting, because I need the user to have the ability to change the filename before applying file.
code:
m_dialog.setAcceptMode(QFileDialog::AcceptSave);
m_dialog.setWindowModality(Qt::WindowModal);
m_dialog.setFileMode(QFileDialog::AnyFile);
m_dialog.setDefaultSuffix("_suffix");
if(m_dialog.exec() == QFileDialog::Accept)
{
setPath(m_dialog.selectedFiles()[0]);
}
Usually, a QFileDialog is displaying the platform file dialog. To get the behavior you want, you'd need to use platform-specific mechanisms; Qt doesn't implement such functionality.
If you're using the non-native file dialog, you could inspect its structure to find the widget(s) you're after, filter relevant events on them, and inject the behavior you need.
Try extending QFileDialog and subscribe to QFileDialog signals
void fileSelected(QString file)
void currentChanged(QString path)
It can be a start.

QFile: directory not found

I need to write a console application that takes a file, it opens it, and then it calls another procedure based on the information inside the text file.
The only problem is that QFile::errorString() returns:
No such file or directory.
I have been using this implementation in all the programs I had to, and yes, the file exists at that directory.
The code is:
QFile fileName("D:/file.txt");
QString read_from_file;
if(fileName.open(QIODevice::ReadOnly)){
QTextStream in(&fileName);
while(!in.atEnd())
{
read_from_file = in.readLine();
qDebug()<<read_from_file;
}
fileName.close();
}
qDebug()<<fileName.errorString();
Make sure that the file really exists.
QFile::exists("D:/file.txt") – This will return true if the file exists.
QDir("D:/").entryList() – This will return the list of the files and directories located at the specified path; the needed file should be in the list.
As you pointed out in the comments, the problem was the hidden file extensions on Windows.
Open Folder Options by clicking the Start button, clicking Control Panel, clicking Appearance and
Personalization, and then clicking Folder Options.
Click the View tab, and then Advanced settings <...>
To show file name extensions, clear the Hide extensions for known file
types check box, and then click OK.

QFileDialog: using getOpenFileName allow for non-existent files

I want to program a browse button with qt that opens a standard find file dialog. If the user enters a new file name in the dialog I want to create the file. If the file exists I want to open it.
I have a function that given a string will make that decision. However, QFileDialog::getOpenFileName shows the user a error if the file doesn't exist, and QFileDialog::getSaveFileName asks the user for a confirmation to overwrite the file if it does exist (which I wouldn't do anyways, so it should not be showed).
Is there a qt standard implemented that could meet my need without having to create a custom class iheriting from QFileDialog or resorting to another similarly hairy situation?
Here is my current working code, with undesired behavior...
void Login::browseFile() {
QString file = ui->txtFile->text();
if (file.isEmpty()) { file = QDir::homePath(); }
file = QFileDialog::getOpenFileName(this,
tr("Select Monage Database"), file,
tr("Database Files (*.db)"));
if (!file.isEmpty()) { OpenDb(file); }
}
Google failed me, but a few more minutes scrutinizing the docs, and I found this:
QFileDialog::DontConfirmOverwrite 0x00000004 Don't ask for confirmation if an existing file is selected. By default confirmation is requested.
I was able to use this for getSaveFileName to achieve the functionality I desired. I had to specify the option selectedFilter, but just passed the default 0.
Modified code:
void Login::browseFile() {
QString file = ui->txtFile->text();
if (file.isEmpty()) { file = QDir::homePath(); }
file = QFileDialog::getSaveFileName(this,
tr("Select Monage Database"), file,
tr("Database Files (*.db)"), 0,
QFileDialog::DontConfirmOverwrite);
if (!file.isEmpty()) { OpenDb(file); }
}