Qt - Load an image from the computer in GUI - c++

I'm developing a gui application in QtCreator and what the gui should do is this:
Upon clicking on the Open Image button, I should be able to browse my computer to find an image file and load it on the ui window.
This is how the window looks like so far:
A pop up dalog box with a windows-like browser would be great.
I'm not showing any of my code because basically it's the initial source files generated when I create a Gui Aplication.
Edit:
I've managed to create a dialog box to get an image from the computer by applying an action listener to the button and using the following block of code:
void MainWindow::on_pushButton_clicked()
{
//MyDialog mDialog;
//mDialog.setModal(true);
//mDialog.exec();
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
dialog.setViewMode(QFileDialog::Detail);
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
"C:/",
tr("Images (*.png *.xpm *.jpg)"));
}
I am now trying to display the image I choose on the right side of the window. Any suggestions?

Please look at the QFileDialog. This shows the open/save file dialog for the system.
From the article:
The QFileDialog class provides a dialog that allow users to select files or directories.
The QFileDialog class enables a user to traverse the file system in order to select one or many files or a directory.
The easiest way to create a QFileDialog is to use the static functions. On Windows, Mac OS X, KDE and GNOME, these static functions will call the native file dialog when possible.
fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));

Related

QT Creator - How can I store/save selected file to project directory

I am trying to create a way to store file's in the project directory. I am using QFileDialog for the dialog and using Qfile::copy() to copy the selected file. I was able to get it to work by storing it outside the project directory (Desktop). But for this, I need it to store in the project directory.
I am more than happy to provide additional information if needed.
Note: I tried storing it in the Resource folder as well without success. BTW, I am new to qt development world.
QString filePath = QFileDialog::getOpenFileName(this, QObject::tr("PDF files"),
"C:/", "PDF (*.pdf) ;; JPG (*.jpg) ;; PNG (*.png)");
QFileInfo file(filePath);
QString fileName= file.fileName();
if(QFile::copy(filePath, ":/PDF/PDF/"+fileName))
qDebug() << "success";
else
qDebug() << "FAILED TO LOAD TO -> :/PDF/PDF/"+fileName;

Qt : How to open a pdf file from the application's resources?

I need to create a push button that will open a .doc or a .pdf file. It only works when I use a QFileDialog like this:
QString docName = QFileDialog::getOpenFileName(this, tr("Arquivos"), "C:/Users/lucas/Desktop/HEMODIALISE/Hemodialise/Resources/Documentos", "All files (*.*)");
QDesktopServices::openUrl(QUrl("file:///"+docName, QUrl::TolerantMode));
But I want to open instantly using just the file inside the resources of the program.
How can I accomplish that?

Show mp3 files but select folder with QFileDialog

Currently, I can select a folder using QFileDialog's getExistingDirectory function. Even thought I managed to let the dialog show the files as well, I would like to filter them, i.e. display only folders and *.mp3 files.
QString folder = QFileDialog::getExistingDirectory(this, "test", "", QFileDialog::ReadOnly);
Is this possible without creating a custom dialog?
QFileDialog d(this);
d.setFileMode(QFileDialog::Directory);
d.setNameFilter("*.mp3");
if (d.exec())
qDebug () << d.selectedFiles();
I am on mobile rt now. Giving a kind of hints only....
On Windows and macOS, this static function will use the native file dialog and not a QFileDialog. However, the native Windows file dialog does not support displaying files in the directory chooser. You need to pass DontUseNativeDialog to display files using a QFileDialog. ( from qfiledialog documentation)
So set the 'QFileDialog::DontUseNativeDialog' option using 'setflags'
Then using file dialog's 'setfilter' set the filter for your MP3 files..

QT C++ - Open Network Drive

I have a UI where there are two buttons:
Set directory - provides a navigation dialog for user to select a directory
Open directory - opens window to set directory
My code below would work for a local directory, but does not work on a networked directory. When I set the directory as the network path //my.network.path.com/jon/ and try to open the directory, window directory does not appear and QT output logs ShellExecute '//my.network.path.com/jon' failed (error 2). Does QT not have access to Network drives?
QDir mSaveDirectory;
void setDirectory()
{
QWidget *widget;
QString directoryPath = QFileDialog::getExistingDirectory(widget, "Select Directory", "/", QFileDialog::ShowDirsOnly);
mSaveDirectory.setPath(directoryPath);
}
void openDirectory() const
{
QUrl url;
url.setScheme("file");
url.setPath(mSaveDirectory.absolutePath());
QDesktopServices::openUrl(url);
}

In the Qt how to open QFileDialog::getOpenFileNames in user home

I'm using Qt QFileDialog::getSaveFileName and QFileDialog::getOpenFileNames for the user to select where to save files and what files to open in my app.
The third parameter of this function is the path where you want the window to open by default. In linux, How can I get the dialog to open in the user home, and in windows how can I get the dialog to open in the user user folder in win 7 or in 'My Documents' in win xp?
Currently I'm using the dialog like this: QFileDialog::getOpenFileNames(this, "Select a file to open...", HOME); where HOME is a preprocessor macro that in UNIX is ~ and in windows is C:\
The Unix one does not work and opens the dialog in the same folder where the binary is.
Use QDir::homePath.
QFileDialog::getOpenFileNames(this, "Select a file to open...", QDir::homePath())
Also if you want to apply a filter on existing files, you can try this:
QString filter = "File Description (*.extention)";
// For example: "Mpeg Layer 3 music files (*.mp3)"
QFileDialog::getOpenFileName(this, "Select a file...", QDir::homePath(), filter);
And then once user selected a file, absolute address of that file is returned by QFileDialog::getOpenFileName function.