How to create a dialog window to choose a file path - c++

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.

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.

Set a default UNC path for QFileDialog

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);

How to modify the -webkit-scrollbar styles in a QWebView

Webkit provides special css properties for styling scrollbars, for example:
::-webkit-scrollbar-track {
background-color:white;
}
Normally I'd put these inside a <style> tag inside <head>, but unfortunately QWebElement doesn't seem to be able to modify anything inside <head>. I can use the setHtml() function to specify initial styling, but not modify it later. Is there an alternative way to apply CSS styles to the scrollbars in a QWebFrame?
Is possible using QWebSettings::setUserStyleSheetUrl, see example:
const QString path = PATH_OF_CSS_FILE;
QWebSettings *settings = QWebSettings::globalSettings();
settings->setUserStyleSheetUrl(QUrl(path));
Example
If dynamic CSS is a string, you can create a method and use QTemporaryFile, like this:
void MainWindow::setStyle(const QString data)
{
QTemporaryFile file;
if (file.open()) {
const QString path = file.fileName();
QWebSettings *settings = QWebSettings::globalSettings();
settings->setUserStyleSheetUrl(QUrl(path));
}
}
Usage:
setStyle("::-webkit-scrollbar-track { background-color:white;}")
If needs load dynamic file, you can create an alternative method like this:
void MainWindow::setStyle(const QUrl url)
{
QWebSettings *settings = QWebSettings::globalSettings();
settings->setUserStyleSheetUrl(url);
}
Using QRC
Is part of the answer is just a tip for other implementations;
You can use resources (QRC) in your project for put default stylesheet for all QWebViews, see example:
Click right button in your project > Add New ... > Qt > Qt Resource File > Put name "resources.qrc"
Click right button in "resources.qrc" > Open in Editor
Put a CSS file with name scrollbar.css (css file must be in the same folder as your project).
Put this in your "main.cpp":
#include <QWebSettings>
#include <QUrl>
...
const QString path = "qrc:/scrollbar.css";
QWebSettings *settings = QWebSettings::globalSettings();
settings->setUserStyleSheetUrl(QUrl(path));

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

Importing the entries to be filled in the UI, from a file, in qt4

I created a UI in qt4. Now I should be giving an option to the user to fill the entries in the UI, from an existing file on system,which the user can browse for.
Now, Iam able to set the line edit entries in my UI from the file that the user specifies but I am not able to set the highlighted text in the comboboxes to what the file has. This might be very vague, Iam not able to explain this properly.Here is the code snippet i used:
//browsing for the file
path = QFileDialog::getOpenFileName(
this,
"Choose a file to import data from",
QString::null);
QFileInfo fi(path);
ui->lineEdit_21->setText( path );
//opening the file specified by user, for reading
name = fi.fileName();
dir = fi.path();
QDir::setCurrent(dir);
QFile read(name);
QString str;
if (!read.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&read);
while (!in.atEnd())
{
QString line = in.readLine();
//filling the UI from the file
if(line.contains("AP SSID :", Qt::CaseInsensitive))
{
str = line.section(':', 1, 1);
ui -> lineEdit->setText(str);
}
}
This works fine but now how do I change the selected entry in a combobox, in accordance with the file?
if(line.contains("FREQUENCY :", Qt::CaseInsensitive))
{
str = line.section(':', 1, 1);
ui -> comboBox_2->setEditText(str);
}
I tried this but this is not working. My combobox_2 has two frequencies 2.4GHz and 5GHz. If the user selected file has 2.4GHz then I want the combobox to update itself such that 2.4GHz entry is highlighted. Hope I have made my point. Please help.
Thanks
You have to use the findText function in order to get the index of the given text in the combobox.
int frequencyIndex = ui->comboBox_2->findText(str);
if (frequnecyIndex != -1)
ui->comboBox_2->setCurrentIndex(frequencyIndex);