Set a default UNC path for QFileDialog - c++

I wanted to open QFileDialog with a specific path like: //Machine/C$/Users/. I have implemented the following function but it doesn't work.
void DownloadFM::on_pushButtonSource_clicked()
{
QFileDialog o_dialogSource;
o_dialogSource.setDirectory(absolutePath);
QString fileName = QFileDialog::getOpenFileName(this, "Choose File");
if(fileName.isEmpty())
return;
ui->lineEditSource->setText(fileName);
}

For example, if you want to open a dialog at desktop location do as follows:
QString fileName = QFileDialog::getOpenFileName(this, "Choose File",QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
Please notice that you have to #include <QStandardPaths>

This code
QFileDialog o_dialogSource;
o_dialogSource.setDirectory(absolutePath);
and this
QString fileName = QFileDialog::getOpenFileName(this, "Choose File");
are completely independent. The former creates a local dialog object, sets the path in it, and... never shows the dialog. The latter creates another dialog—that does appear on the screen—passing the default value of the third argument as const QString &dir=QString() (see this function documentation), thus not setting the path you wanted.
The proper way is to remove the useless o_dialogSource lines, and then add the necessary argument to the getOpenFileName call:
QString fileName = QFileDialog::getOpenFileName(this, "Choose File", absolutePath);

Related

File Not Being Created in QT C++ using QFileDialog

there I'm trying to make a simple notepad application. I've got a new file menu item. When the user clicks on that, it checks whether the text field is empty or not. If that is not empty then it prompts to save the work. If the user opts for yes then it asks for the location using QFileDialog. However, this code doesn't create the file at the provided destination. Can anyone figure out this code?
if(this->checkTextField()==false){ //checkks whether the textField is empty or not
QMessageBox::StandardButton reply = QMessageBox::question(this,this->appName,this->document_modified);
if(reply == QMessageBox::Yes){
QString filename = QFileDialog::getSaveFileName(this,"Save the File",QDir::homePath(),this->textFilter);
QFile file(filename);
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.flush();
file.close();
}
}
Simply creating a QFile will neither create nor open a file on the filesystem. You have to open it to write something to it/read from it.

QT creator | How to read a directory(path) by using line edit?

Here's the code I am using :
void Dialog::on_pushButton_clicked()
{
QString directory = QFileDialog::getExistingDirectory(this,
tr("Find Files"), QDir::currentPath());
ui->lineEdit->setText(directory);
}
With this, I can display a list of files of a path given directly. How can I make this code display the list of files for a directory/path given by the user?
Declare a QDir like this : QDir dir(directory) and then use QDir::entryList() (which returns a list of the names of all the files and directories in the directory).

How to create a dialog window to choose a file path

I'm new to qt, and I have a button, if I click on it I want to get a dialog box to choose a path where I want to save a file. My question is, how could I create this kind of dialog box, which returns with a string of the path? I use linux, if it matters with qt:)
ps.: I use only gedit, so I'd like to solve it that way. :)
Use QFileDialog which has several useful static member functions including
QString myDir = QFileDialog::getExistingDirectory();
which returns a directory that you select. I think that is what you want, see the docmentation here
http://qt-project.org/doc/qt-5.0/qtwidgets/qfiledialog.html
In addition to the answer by #Muckle_ewe, there is a the static function QFileDialog::getSaveFileName, which will present the standard open / save file dialog and allow the user to both select the path and input a name for the file.
It's definition is this: -
QString QFileDialog::getSaveFileName(QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0)
An example of its usage is: -
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
"/home/untitled.png",
tr("Images (*.png *.xpm *.jpg)"));
As the docs state,
This is a convenience static function that will return a file name
selected by the user. The file does not have to exist.

Getting the name and location of selected file Qt

I have a program in which I have a button to get File Dialog like
How can I select a file, get the file name and location, and save that to a string displayed in the ui.The signalclicked(), emitted from the button, is connected to the slot fileSELECT().
........
void MainThread::fileSELECT(){
QString fileName = QFileDialog::getOpenFileName(this,tr("Select video"),"d:\\BMDvideos",tr("Video files (*.avi)"));
}
so when I select an .avi file, how do I get its location in fileName displayed like
d:\BMDvideo\videFile.avi
so I thinks that I got it now. my first code was completly wrong.
void MainThread::fileSelect(){
QString fileName = QFileDialog::getOpenFileName(this,tr("Select video"),"d:\\BMDvideos",tr("Video files (*.avi)"));
QLabel *testLabel = new QLabel(fileName);
BOX->addWidget(testLabel);
}
I can see now the path of the selected file
To get the folder path, you can use QFileDialog::getExistingDirectory, and to get the file-name use QFileDialog::getOpenFileName

File Input - File not being read QT

I'm very new to C++ and QT. Basically, I just done Java stuff, but we're being thrown in C++ for a module, in which we have to design a Zork game UI. The way I am planning to create the Room object is have the Room constructed with a roomnum.long, .short and .name for the long desc, short desc and name.
For the first room, I'm just going to load the file directly for the moment until I get onto objects etc properly. However, I can't seem to get the file written into a text label. The return "restart game etc" does get written on button press, but not the contents of the file. The Rooms folder is in the same folder as the source file (islandmain.cpp in this case)
Any ideas?
void TheIslandMain::on_startButton_clicked()
{
ui->roomDesc->setText(readFirstRoom("/Rooms/room1.long"));
ui->roomDesc->setWordWrap(true);
}
QString TheIslandMain::readFirstRoom(QString filename)
{
QFile mFile(filename);
if (!mFile.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "Could not open file for reading Line 31";
return "Could not load file. Please restart game.";
}
QTextStream in(&mFile);
QString fileInput = in.readAll();
qDebug() << fileInput;
mFile.close();
return fileInput;
}
check if the file exists using bool QFile::exists(const QString& path) and also if its only the filename you have in your function or the whole path (path is needed)
soo long zai
/Rooms/room1.long points to a file in root, then Rooms, then room1.long as the file. If Rooms is the directory it needs to be relative to the application itself, so if Rooms is in the same directory as your application it'd just be Rooms/room1.long, if it's in a higher level ../Rooms/room1.long or Resources/Rooms/room1.long.