Getting the name and location of selected file Qt - c++

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

Related

Display a subdirectory (path retrieved from a string) inside a treeView

I have a QString fileName which stores in it a path to a subdirectory (eg. C:/Qt). I also have a treeView inside a dialog box.
Upon starting the application, we browse a file and once the file/folder is selected, I save the path of the file/folder in fileName string. Upon clicking "OK" (QPushButton), I intend to open a new Dialog Box divided into a treeView displaying the subdirectory with the selected folder as the root node of the tree, and a listView displaying all the files/folders within that folder and nothing if empty.
I used the following QFileSystemModel functions to display the C:/ directory (only drive on my pc) :
QString path;
//QFileSystemModel *dirModel;
//QFileSystemModel *fileModel;
//TreeView
dirModel = new QFileSystemModel;
dirModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
dirModel->setRootPath(path);
ui->treeView->setModel(dirModel);
//ListView
fileModel = new QFileSystemModel;
fileModel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
fileModel->setRootPath(path);
ui->listView->setModel(fileModel);
If I set the path to fileName (QString path = fileName;), nothing changes. I'm assuming QDir::AllDirs is the reason for this.
I looked for other QDir filters (like QDir::Dirs or setFilter) but nothing seems to work.
I even tried QDesktopServices::openUrl, but it just opens the file path in a new window instead of the treeView.

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.

How to display the file path in Entry widget in windows path format

I have written a Tkinter program in which the Browse button is used to select a file and the selected file's complete path gets displayed in the Entry widget. But my problem is, it's displaying the path with 'forward'(/) slashes instead of the conventional windows format of 'backward'(\) slashes. This is strange for me since I'm working on windows os.
Why this occurs ? Is there any before hand fix for this, instead of replace string option ?
my code:
def selectfile():
fileName = askopenfilename(parent=root, title='Choose a file', initialdir='C:\\')
custName.set(fileName) #Populate the text field with the selected file
#create the 'filepath' field
custName = StringVar(None)
filepath = Entry(root, width ='50', textvariable=custName).pack(anchor=W)
#Create the 'Browse' button
browseButton = Button(root, text="Browse", relief = 'raised', width=8, command=selectfile, cursor='hand2').place(x=325,y=16)
Expected Output in Entry widget:
c:\data\file.txt
Actual Output in Entry widget:
c:/data/file.txt
You can always use replace() to replace "/" with "\" for string. Here's link with docs about replace() method:
https://docs.python.org/2/library/string.html?highlight=replace#string.replace
This is workaround fix. Try looking more deeply inside Tkinter's docs for actual answer why this occurs.

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.

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.