user12363705
user12363705

Reputation:

How to open a specific directory with QFileDialog in QT

I wanted to open QFileDialog for a specific path like //DC1/C$/Users/ but when QFileDialog opens it goes to a default path. When QFileDialog opens, I can change directory to the UNC path but I wanted to open this path as default. How can I do that? In the following code, g_absolutePath has been initialized with //DC1/C$/Users/ but it doesn't work.

#include "uploadfm.h"
#include "ui_uploadfm.h"

#include "mainwindow.h"

UploadFM::UploadFM(QWidget *parent) : QMainWindow(parent), ui(new Ui::UploadFM)
{
    ui->setupUi(this);
    QWidget::setFixedSize(600, 175);
}

UploadFM::~UploadFM()
{
    delete ui;
}

void UploadFM::on_pushButtonSource_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, "Choose File", g_absolutePath);

    if(fileName.isEmpty())
        return;

    ui->lineEditSourcePath->setText(fileName);
}

void UploadFM::on_pushButtonDestination_clicked()
{
    QString fileName = QFileDialog::getExistingDirectory(this, "Choose Folder", g_absolutePath);

    if(fileName.isEmpty())
        return;

    ui->lineEditDestinationPath->setText(fileName);
}

void UploadFM::on_pushButtonUpload_clicked()
{
    QString l_sourceFileName = ui->lineEditSourcePath->text();
    QString l_destinationFileName = ui->lineEditDestinationPath->text();

    if(l_sourceFileName.isEmpty() || l_destinationFileName.isEmpty())
        return;

    QFile file(l_sourceFileName);

    if(file.copy(l_destinationFileName))
    {
        statusBar()->showMessage("Upload has been Successful ... ");
    }
    else
    {
        statusBar()->showMessage("Upload has failed ...");
    }
}

Upvotes: 0

Views: 1813

Answers (1)

The Qt Documentation documents how to do this :

void UploadFM::on_pushButtonDestination_clicked()
{
    const QString defaultPath = "//DC1/blabla...";
    QString fileName = QFileDialog::getExistingDirectory(this, "Choose Folder", g_absolutePath, defaultPath);
   ...
}

Upvotes: 0

Related Questions