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.
Related
I used a QFileDialog to open a browser.
Here is my code:
QString filePath = QFileDialog::getSaveFileName(this,
"Export Xml", "PluginPythonQt",
"Xml files (*.xml)");
When excute it will show a dialog like this:
I want to disable the "File name:" box in the picture or prevent user to enter a new name. How can i do that ? Thanks.
I believe you can't achieve this — save dialog is about choosing name besides the choosing where to save it. Of course, you might just ignore what user typed and force your name when he hits OK but it will just make the user angry.
Better way, in my opinion, is to use QFileDialog::getExistingDirectory which will allow the user to choose where to save the file but won't allow him to choose the file name. It will be fair, at least.
Similar question was answered in https://forum.qt.io/topic/73973/qfiledialog-with-no-edit-box.
In general, you can hide any element in any widget if you dig a bit into widget's source code to find element's name, when you have a name, you can find the corresponding element via findChild<QWidget *>(elementName).
Usually if you check QSomeWidget.h (Qt is open source!) you can find element names very easily as they are typically listed as the widgets members.
To hide both labels, fileEdit, ComboBox and even buttons, you can use this code:
QFileDialog fileDialog = new QFileDialog;
QWidget * fileNameEdit = fileDialog->findChild<QWidget *>("fileNameEdit");
Q_ASSERT(fileNameEdit);
fileNameEdit->setVisible(false);
QWidget * fileNameLabel = fileDialog->findChild<QWidget *>("fileNameLabel");
fileNameLabel->setVisible(false);
QWidget * fileTypeCombo = fileDialog->findChild<QWidget *>("fileTypeCombo");
Q_ASSERT(fileTypeCombo);
fileTypeCombo->setVisible(false);
QWidget * fileTypeLabel = fileDialog->findChild<QWidget *>("fileTypeLabel");
fileTypeLabel->setVisible(false);
QWidget * fileButtonBox = fileDialog->findChild<QWidget *>("buttonBox");
fileButtonBox->setVisible(false);
Note that even though buttons are hidden, typing Enter on keyboard (or double clicking) would trigger Open button, and dialog might disappear if you haven't done anything in Accept method. So it would also be a good idea to handle state of that button as well if you really wish buttons to be hidden as well.
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.
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); }
}
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
I'm interested in writing a small utility in C++ for Mac OS X to read, parse, save (over)write a file. I don't need any GUI, menus, or windows.
What type of project template do I need to start with in XCode?
How can I access the file that is passed in? (It's passed with Apple Events openFile, right?)
I've done a little C++ but nothing on Mac. Links appreciated, code samples appreciated more.
How do you intend to pass files to your application?
If it's via the command line then you would use the Command Line Tool template and access the command line parameters just as you would on any POSIX platform (argc and argv).
If you want to pass files to your application using Finder, say, by dropping files onto the application icon, then you would use the Cocoa Application template.
The Info.plist file contains your application configuration and supported document types, similar to the registry on Windows.
You configure Info.plist via the "Info" tab of your Project Settings (It's the top-most file in the file navigator in XCode). Click the "Add" button in the lower right, then select "Add Document Type" to add a document type that your application will accept. To accept all documents, set the document name to All and set the extension to *. More info is here.
Add a LSUIElement key in your Info.plist, and set its value to YES to indicate that your application has no UI. This key is also displayed as "Application is agent" in XCode. More info on LSUIElement is here.
In your MainMenu.xib, you can delete the Window and Font Manager objects that are there by default, since you won't be needing them.
Rename the AppDelegate.m file to AppDelegate.mm, so that it's compiled as Objective-C++. This will allow you to use C++ code in that file.
In the applicationDidFinishLaunching: delegate method, add [NSApp terminate:nil]; so that your app quits immediately when it's done its work.
Add the following method to AppDelegate.mm:
- (BOOL)application:(NSApplication*)app openFile:(NSString *)filename
{
NSLog(#"Opening file %#", filename);
char* cFilename = [filename UTF8String];
// Your C++ code goes here
return YES;
}
That's it. The rest is your C++ code. You can add any C++ code to AppDelegate.mm that you want. E.g.:
#include <string>
#include <iostream>
#include "MyCppFileProcessor.h"
- (BOOL)application:(NSApplication*)app openFile:(NSString *)filename
{
std::string cFilename([filename UTF8String]);
std::cout << "Processing file: " << cFilename << std::endl;
MyCppFileProcessor fileProcessor;
fileProcessor.processFile(cFilename);
return YES;
}
This code will run whenever you drop a document onto your Application's icon in Finder.
1)Use the Command Line Tool template. There are several options for this template. You may select C++ from the menu.
2)As far as I know IOstream will work just fine. Also, there's an argument parameter on your main() function, you may get the file name from these args.
http://www.cplusplus.com/reference/iostream/