QT C++ - Open Network Drive - c++

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

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;

How to open a windows 10 application (downloaded from the store) from QT button?

I manage to open(.exe) file but I can't open a (. lnk) file.
I want to open a windows 10 app that I have downloaded from the store.
The code below opens an .exe file, how can it modify it to open .lnk file?
void MainWindow::on_pushButton_App_clicked()
{ QString program = "SMD.exe";
QStringList arguments;
QProcess *myProcess = new QProcess(this);
myProcess-> start (program,(QStringList) arguments<<"");
return ;
}
QDesktopServices::openUrl opens files with the default handler, which includes opening .lnk files with the shell. You can use that, e.g.:
QString shortcut = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Accessories\\Notepad.lnk";
QDesktopServices::openUrl(QUrl::fromLocalFile(shortcut));

Qt - Load an image from the computer in GUI

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

qtcreator tray icon under window does not show up when deployed

i'am trying to deploy a QT application made in QT Creator which goes back to system tray when closed.
I have made a svg tray icon, when i run it from QT Creator, either in debug and in release mode under Window 7, the tray icon shows up, but when i copy everything to another directory to make a distributable archive from it, the tray icon does not show up anymore.
Of course i search already for solutions, but everything i have found yet, i have made.
So what i have:
trayicon.svg file in project root
qrc file created, adding trayicon.svg into the resource files root
in project .pro file: RESOURCES += resources.qrc
copied binary + necessary dll's to target directory
copied QT plugins imageformats/* to target dir imageformats
added QApplication a(argc, argv); a.addLibraryPath(a.applicationDirPath()); to main.cpp
that is everything i found so far, but still the system tray icon is not showing up
What am i missing?
(current qt 4.8 + current qtcreator by the way)
#netrom
code in MainWindow : QMainWindow constructor:
trayIcon = new QSystemTrayIcon(this);
showAction = new QAction(tr("&Show"), this);
connect(showAction, SIGNAL(triggered()), this, SLOT(show()));
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(showAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon->setContextMenu(trayIconMenu);
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
trayIcon->setIcon(QIcon(":trayicon.svg"));
trayIcon->show();
Create iconegines directory in your app directory (for example: c:\MyApp\iconengines).
Copy qsvgicon.dll to this new directory (for example: c:\MyApp\iconengines\qsvgicon.dll) from qt plugins directory (in my case it's c:\qt\5.4\mingw491_32\plugins\iconengines\qsvgicon.dll).
Copy QtSvg.dll to your app directory (for example: c:\MyApp\Qt5Svg.dll) from Qt bin directory (in my case it's c:\qt\5.4\mingw491_32\bin\Qt5Svg.dll).
P.S. I know I'm late, this answer is for those who will google same problem.
Again, way later for DuckDuckGo-ers:
If your icon is not in .svg you might need another solution. For my .ico system tray icon I had to copy <path-to-qt>\plugins\imageformats into the application folder. Actually only qico.dll was needed in that folder in my case, but you get my drift.

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.