QFileDialog - how to specify home directory - qt

I tried the following to open a file dialog that should display the users home directory:
QString fileName = QFileDialog::getOpenFileName(this,
tr("Select database"), QDir::homePath(),
tr("Database Files (*.db *.sqlite)"));
The problem ist that QFileDialog always starts with the directory from where the application was started. Any ideas what I'm doing wrong ?
BTW: I'm using Qt 5 on Mac OS X.

In Mac OSX, QDir::homePath() returns the content of the HOME environment variable. So if this variable is empty, it returns an empty string.
Ensure the 'Clear system environment' option (under Build Environment from the project settings in Qt Creator) is not checked. When you check this box, Qt will basically overwrite the value of every environment variable for your program, so it would appear to be empty.

Use QDir::homePath() as suggested earlier or consider usage of Qt5's QStandardPaths class.

Related

How to permanently change the default working path of julia?

I was able to temporarily change it with the following command, but after reboot the working path still changes to C drive.
cd("D:\\jl files”)
As it is explained in the Julia Manual Getting Started section the simplest thing to do is to add the:
cd("D:\\jl files")
line to your ~/.julia/config/startup.jl file (this is a Linux path; if the file does not exist then you should create it with this single line). Since you are on Windows then ~ part should be replaced by default is your user profile folder (it should be possible to check it with ENV["USERPROFILE"] command in Julia). However, if you have a custom installation the .julia folder can be placed in some other folder so you need to check it on your system.

Qt Creator could not find the executable, please specify one

I'm trying to run the texteditor.pro file in the QtSDK\Examples\4.7\tutorials\gettingStarted\gsQml directory with Qt Creator. When I try to build the project I get a window that says
Could Not find the executable, please specify one
with three fields to load files. Snapshot of the dialog box.
I'm running windows 7 64bit, with Qt Creator 2.4.1
I've solved the problem it was a missing dll file, but the executable didn't ask for it, so I put all the dlls from Qtcreator -> bin in the same folder with the *.exe file, then I deleted file after another until I figured out what files are required.
* It is a brute force way, but It did the job.
* Here is the file that were missing (libEGL.dll)
This project is not created in the normal 'Qt-executable' kind format. Rather its a 'plugin' kind project. So you cant run it directly like other projects.
[If you will open the project files you wont find a main() function!, which is supposed to be the entrance point usually for a C++ Application. All you have are a couple C++ classes. Take that as a hint]
About this example they have given the complete details here. And I quote:
We need to compile the file dialog C++ plugin before the text editor
can run. To compile, enter the gsQml directory, then run qmake and
compile using make or nmake, depending on your platform. To run,
launch qmlviewer and open the texteditor.qml file.
Else:
You create your own project.
Add these class files and the respective qml files to this project.
Add a main and create the respective objects required.
Make an application viewer and give "texteditor.qml" path as its source.
I had the could not find executable window pop up in my face in Ubuntu 12.10.
Here's how I got the "error":
Created a folder named Project;
Inside it, I ran "qmake -project" and then "qmake";
Created a main.cpp file inside the folder;
Opened the Project.pro file with Qt Creator and added the line "SOURCES += main.cpp" to it;
Pressed Ctrl + R to build and run the project.
Later on I deleted the folder and created it again, but this time creating a main.cpp file before trying to run any commands. I opened the .pro file with Qt Creator, created a main function in the main.cpp file, and pressed Ctrl + R, and it built and ran!

NSIS CreateShortcut insists on using %ProgramFiles% for the icon path

I have the following NSIS code
Function CreateDesktopSC
;Creates Desktop Shortcut
SetShellVarContext current
SetOutPath "$DOCUMENTS\Foo\"
SetShellVarContext all
detailprint "Icon path: $INSTDIR\Bar\icon.ico"
CreateShortCut "$DESKTOP\${productName}.lnk" "$INSTDIR\Bar\binary.exe" "" "$INSTDIR\Bar\icon.ico" 0
FunctionEnd
The install log shows the following (from the detailprint command)
Icon path: C:\Program Files (x86)\Bar\icon.ico
The shortcut is created, but with the icon from the executable.
If I open the lnk file or right click the shortcut and click "Change Icon ...", I get the error "Windows can't find the file %ProgramFiles%\Bar\icon.ico."
If I browse to %ProgramFiles%, it takes me to c:\Program Files, not the x86 version as shown in the detailsprint command. The icon file exists, but in the x86 folder.
It appears that either NSIS or windows is replacing "C:\Program Files (x86)\" with "%ProgramFiles%", which doesn't point to the x86 version.
The actual path to the executable is correct, it's only the icon link which is incorrect.
Any ideas?
The workaround from the thread is to add an second \ to your icon code. I didn't really got why this helps on 64bit systems but it does...
so replace:
CreateShortCut "$SMPROGRAMS\$StartMenuGroup\${PRODUCT_NAME}.lnk" "yourapp.exe" "$INSTDIR\${APPLICATION_ICON}"
with
CreateShortCut "$SMPROGRAMS\$StartMenuGroup\${PRODUCT_NAME}.lnk" "yourapp.exe" "$INSTDIR\\${APPLICATION_ICON}"
After adding the second \ before APPLICATION_ICON the icon will be displayed again
Confusing but it works
NSIS just uses the documented IShellLink interface. There is a thread about it on the NSIS forum (with a workaround you can try). I believe it is a bug in WOW64... (The registry redirector is docmented to change %ProgramFiles% to %ProgramFiles(x86)% behind your back, I suspect IShellLink is missing this hack)
Disable redirection
Load Icon from path
All is done with System plugin. Why to complicate ...
http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.3.4 seems to be wrong, for me what worked was:
CreateShortCut "$SMPROGRAMS\${PRODUCT_PUBLISHER}\${SHORTCUT_NAME}.lnk" "$INSTDIR\${PRODUCT_EXE}" "$INSTDIR\${PRODUCT_EXE}" "$INSTDIR\MyIcon.ico"

Problem in creating text file(Qt application) in installation directory (using CMake to install)

I am new to programming. I am creating a small word jumble game to practice qt programming. In this application I am creating a text file (score.txt) to keep score of player. I have done this by:
QFile scoreFile("score.txt");
if (QFile::exists("score.txt"))
{
scoreFile.open(QIODevice::ReadWrite | QIODevice::Text)
// and update the score.
}
else
{
scoreFile.open(QIODevice::ReadWrite | QIODevice::Text);//create score file
//and write the score to it.
}
this code is working good here. Now I am using CMake to build and install generated binary (I am working on Ubuntu) using this code:
#set project name, version and build code here.
install(TARGETS wordJumbleGame DESTINATION bin)
I build project in /home/myname/project/build/
My source code is in /home/myname/project/src/
CMakeLists.txt is in /home/myname/project/CMakeLists.txt
I installed program using make install.
Till here all things working fine. But now problem is that when I run this program (I run it from terminal giving command wordJumbleGame) It creates score.txt in /home/myname/project/build directory. It is not being created in installation dir bin.
So please help me out, what am I doing wrong. And please also tell me how do I make my program to appear in application->game lists so I can run it from there not from command prompt.
Unless you prefix it with a slash (on unix) or a drive path (Windows), QFile's constructor parameter is a relative path - relative to the current working directory. score.txt is created in the build/ directory because that's probably where you're executing the binary from.
You can't store score.txt in the /usr/bin directory because, typically, you can't write there without root privileges.
What you want to do is get a path to some directory where you can store your score.txt file. To do that, you can use the QDesktopServices class. That will give you directory information on a per-user basis. Here's an example:
#include <QDesktopServices>
// this would go in main(), probably
QCoreApplication::setApplicationName( "word jumble game" );
// now when you want to read/write the scores file:
QString dataPath = QDesktopService::storageLocation( QDesktopService::DataLocation );
QFile scoreFile( dataPath + "score.txt" );
// on my system, this produces: "/home/adam/.local/share/data/word jumble game/score.txt"
// it will produce something similar for Windows and Mac too
You should set your appication name via QCoreApplication::setApplicationName before getting path information to keep the user data directory nice and organised.
As for getting your application in the games list, you'll need to create a menu entry that follows the freedesktop.org spec. I can't help you more with that, but this is a good starting point. Somebody else might have more info for you.
You need to create a .desktop entry file and install it using xdg-desktop-menu install. Here are two resources for you: freedesktop.org menu spec and adding .desktop files using CMake

Missing file [Qt]

I'm successfully going through proccess of building an application in Qt but when I try to run .exe file by double clicking on it (outside of qt) I'm getting an error saying:
"The program can't start because mingwm10.dll is missing from your computer. Try reinstalling the program to fix this problem."
I checked for this file and I found it, so I think there is some path to be set in qt, but I don't know neither where and how to do it. Could anyone explain to me how to do it?
Thank you.
I think you need to add the directory containing mingwm10.dll to your PATH environment variable. I am surprised this was not configured correctly when you installed Qt/MinGW.
Control Panel -> System -> Advanced -> Environment Variables -> PATH
This isn't something that should be set in Qt. Your application is looking for a DLL it can't find, mingwm10.dll. You can either copy the DLL (and it's dependencies if there are any) near your .exe, or add the directory it's in to the environment variable PATH.
You can go to the location where your Qt application is installed and then navigate to "..\Qt\5.11.3\mingw53_32\bin" directory. The search for the missing .dll files, copy them to the location where your .exe file is situated and then re-run the application. If more than one files are missing try the same for them.

Resources