Method to create an empty directory with file manager

836 Views Asked by At

I am compiling on Linux with Qt Creator. I already finished the basics of my GUI with the Qt Designer. Now I need to know if there is a method available that opens the file manager and let me create a directory named by me?

I found this:

QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);

(see http://doc.qt.io/archives/qt-4.7/qfiledialog.html#directory)

I have connected a QPushButton with a method and wrote the above into it. Problems are: The file manager does not even open when I push the Button. Furthermore this should only work for creating not existent files (see AnyFile), but I need to create a not existing directory.

I also know this possibility for creating a directory:

QDir("/home/name").mkdir("NewDirectory");

But this does not fulfill my demands since the name and the directory are always the same.

1

There are 1 best solutions below

2
On BEST ANSWER

Here's for solving the problem with the nonexistent folders / files:

You need some Variables:

QString path_trunk;
QString name;

and in the Slot that is being called by your button you'll have to adjust the parameters so that they suit your demands, for. e.g. you may use the parameters being handed down through the button or being manipulated by some internal events. In that case you could of course create folders dynamically through using the variables:

path_trunk = QString("/home/username/");
name = QString("Folder_name");

QDir(path_trunk).mkdir(name);

Another, more user friendly way would be to fetch values from the UI through a line/text edit element which allows the user to enter a custom path. You can read the values through the UI class ( here I just used the standard QT name ui). The name lineEdit can be different in your case if you chose to name it differently. But you can access the data nevertheless and use id dynamically in your underlying Code. Make sure to connect your signals and methods properly.

void MainWindow::on_pushButton_clicked_path_dynamic()
{
    QString temp_path = ui->lineEdit->text();
    QDir().mkdir(temp_path);
}

void MainWindow::on_pushButton_clicked_trunk_plus_dynamic()
{
    QString ext_path = ui->lineEdit->text();
    QDir(path_trunk).mkdir(ext_path);
}

Here's another approach, since from my understanding you want to use some sort of File Manager:

What you actually could do would be to create your own File Manager Widget. The QDir Method basically gives you all the information you need for displaying a folder incl. files in some Item-based QTreeWidget, Navigation through that would also be an easy task since you can use the signals and slots of the QTreeWidget to navigate, you would just have to fill the Tree with your folder Information. Creating a new folder would then only be a user interaction(right mouse button or maybe clicking a QPushButton "Create Folder" in your custom File Manager Widget)