This example shows how to use two other static methods in QFileDialog for the user to select a directory or file name.
#include <QtCore>
#include <QtGui>
class MainWindow : public QWidget
{
public:
MainWindow();
private:
void setExistingDirectory();
void setOpenFileName();
QLineEdit *dir_text;
QLineEdit *file_text;
};
This declaration for MainWindow declares two slot methods and two line edit widgets which will be updated by the corresponding slot methods.
MainWindow::MainWindow()
{
setMinimumSize(700, 350);
QPushButton *dir_pb = new QPushButton("Directory Name");
dir_text = new QLineEdit();
QPushButton *file_pb = new QPushButton("File Name");
file_text = new QLineEdit();
QPushButton *close_pb = new QPushButton();
close_pb->setText("Close");
QGridLayout *grid1 = new QGridLayout();
grid1->setContentsMargins(75, 45, 75, 25);
grid1->setHorizontalSpacing(20);
grid1->setVerticalSpacing(45);
grid1->addWidget(dir_pb, 0, 0);
grid1->addWidget(dir_text, 0, 1);
grid1->addWidget(file_pb, 1, 0);
grid1->addWidget(file_text, 1, 1);
QHBoxLayout *layout1 = new QHBoxLayout();
layout1->addStretch();
layout1->addWidget(close_pb);
layout1->addStretch();
QVBoxLayout *layoutMain = new QVBoxLayout(this);
layoutMain->addLayout(grid1);
layoutMain->addSpacing(75);
layoutMain->addLayout(layout1);
connect(dir_pb, &QPushButton::clicked,
this, &MainWindow::setExistingDirectory);
connect(file_pb, &QPushButton::clicked,
this, &MainWindow::setOpenFileName);
connect(close_pb, &QPushButton::clicked,
this, &QWidget::close);
}
The setup for this code is very similar to the last few examples. The user interface will display two push buttons with two associated line edits.
Signal / Slot Connections
Lines 36 and 39 set up what action should be taken when either the “Directory Name” or “File Name” push buttons are clicked.
void MainWindow::setExistingDirectory()
{
QFileDialog::FileDialogOptions options =
QFileDialog::DontResolveSymlinks |
QFileDialog::ShowDirsOnly;
QString directory =
QFileDialog::getExistingDirectory(this,
"Directory Name", dir_text->text(), options);
if (! directory.isEmpty()) {
dir_text->setText(directory);
}
}
void MainWindow::setOpenFileName()
{
QString fileName =
QFileDialog::getOpenFileName(this,
"File Name", file_text->text(),
"All Files (*);;Text Files (*.txt)");
if (! fileName.isEmpty()) {
file_text->setText(fileName);
}
}
This block of code shows the implementation of the two slot methods specified in the call to connect(). Line 8 calls the static method QFileDialog::getExistingDirectory() which is implemented in the CsGui library. When the “Directory Name” button is clicked a new dialog box will be displayed with a system file dialog. When the user selects a directory and then clicks the Select Folder button, the path will be displayed in the line edit. The passed parameters set up the Window title, a prompt for the user, the initial directory, and a filter which limits the type of folders displayed.
Line 19 calls the static method QFileDialog::getOpenFileName() which is also implemented in the CsGui library. When the “File Name” button is clicked a new dialog box will be displayed where the user can select the name of an existing file. In the passed parameters on line 21 you can pass a string indicating what type of files should be displayed. This string also populates the drop down box restricting the types of files shown. In this code we are allowing either *.* or *.txt.
For both of these, when the user selects the directory or file a verification is made to ensure the value is not empty before updating the line edit widget.
Main Function
Since the source code for main() has not changed there is no need to show it again. Refer to example 3 or download the full source for this example.
Running the Example
To build and run this example use the same CMake build file and commands as we showed for the first example. The only suggested modification is on line 2 of the CMakeLists.txt file, change “example_1” to “example_13”.