setRootPath() in QT FIleSystemModel to a file - qt

I'm building a simple file browser using QT, and I can't seem to get the setRootPath() of my model to be set to a file, rather than just a directory.
Ex:
setRootPath("/Users/Foo/Bar") works, but
setRootPath("/Users/Foo/Bar/readme.txt") simply sets the root path to "."
Not sure what I'm missing. Everything else within my application works fine.

You can do this:
QFileInfo m_FileInfo = QString("C:/Users/Foo/Bar/readme.txt");
setRootPath(m_FileInfo.absolutePath());
What we're doing is using QFileInfo to get the absolutePath() of the file. So it'll set the root path to C:/Users/Foo/Bar.

Related

Adding custom CSS file to Dash in Julia

For Python there is an option to add custom CSS to a Dash app. The method seems quite straightforward, it says
Just create a folder named assets in the root of your app directory
and include your CSS and JavaScript files in that folder. Dash will
automatically serve all of the files that are included in this folder.
By default the url to request the assets will be /assets but you can
customize this with the assets_url_path argument to dash.Dash
source: https://dash.plotly.com/external-resources
However when I try to do so in Julia, nothing happens.
Is this feature a thing in Julia? If not, how can I do so?
Found a hack, have no idea if this is the correct way though...
Essentially tried to find the inputs to app = dash() by going methods(dash)
julia> methods(dash)
# 1 method for generic function "dash":
[1] dash(; external_stylesheets, external_scripts, url_base_pathname, requests_pathname_prefix, routes_pathname_prefix, assets_folder, assets_url_path, assets_ignore, serve_locally, suppress_callback_exceptions, prevent_initial_callbacks, eager_loading, meta_tags, index_string, assets_external_path, include_assets_files, show_undo_redo, compress, update_title) in Dash at C:\Users\<User>\.julia\packages\Dash\Weukk\src\app\dashapp.jl:291
where I noticed a arg assets_folder. Putting in the absolute path seems to work so the full code looks something like this
using Dash
app = dash(assets_folder="/absolute/path/to/assets")

Qmake get parent directory

I try to get the parent directory for PWD.
I try ../$$PWD. It does not work.
Initially the task was to get the address of the parent subdir, but so far I can't even downgrade the directory.
TEMPLATE = app
Parent_path = $$PWD //here i get smt like C:/Myproject/project(here is my project.pro), but i need only C:/Myproject
Change your code to
Parent_path = $$PWD/../
OR
Parent_path = $$clean_path($$PWD/../)
# Where `../` is resolved.
Because .. is a hard link to parent directory. Then you can use your Parent_path variable something like this
message($$Parent_path)

Is it possible to use system environment variables in Qt resource file?

I want to import some of resources from another project without copying into my project directory. Environment variable ANOTHER_PROJECT_DIR stores path to another project.
I've tried to do something like this:
<file>%ANOTHER_PROJECT_DIR%/file</file>
or this:
<file>$$(ANOTHER_PROJECT_DIR)/file</file>
in my qrc file.
So is it possible to use system environment variables in Qt resource file and how can I do it?
It would be great to find crossplatform solution
qrc resource file is a static binary resource link. You cannot do it dynamically. However, you can do something like this from your code
#include <QProcessEnvironment>
...
somefunc(){
QString env = qgetenv("SOME_VAR");
QString filepath = env + "/path/to/file";
...
}
Rcc does not support such functionality. You can use qmake's "substitutes" (still undocumented?) feature instead.
test.pro
myqrc.input = myqrc.in
myqrc.output = myqrc.qrc
QMAKE_SUBSTITUTES += myqrc
myqrc.in
<file>$$(ANOTHER_PROJECT_DIR)/file</file>
Basically, "substitutes" is a builtin preprocessor capable of evaluating/expanding "qmake"-expressions (plus !!IF / !!ELSE / !!ENDIF conditional).

Read File Using Relative Path on Linux Qt

Original one : QFile file("/home/casper/QuickRecorder/about_content.txt"); (Work)
I have tried :
"about_content.txt"
"/about_content.txt"
"~/about_content.txt"
"./about_content.txt"
"QuickRecorder/about_content.txt"
"/QuickRecorder/about_content.txt"
"~/QuickRecorder/about_content.txt"
"~/QuickRecorder/about_content.txt"
No one works.=[
My Questions
What path can I use?
If I register the file "about_content.txt" into Resource, how can I read it into text browser?
The following is the entire code :
About::About(QWidget *parent) :
QDialog(parent),
ui(new Ui::About)
{
ui->setupUi(this);
this->setFixedSize(this->width(),this->height());
QFile file("/home/casper/QuickRecorder/about_content.txt");
if ( !file.open(QIODevice::ReadOnly) )
QMessageBox::information(0, "Error", file.errorString());
QTextStream content(&file);
ui->aboutContentBrowser->setText(content.readAll());
}
Reference : QT C++ GUI Tutorial 27- How to read text file and display file to a textbrowser or textEdit
Thank you for your help.
If you place your file about_content.txt inside build directory you can use:
about_content.txt
./about_content.txt
If you must open file right from this path /home/casper/QuickRecorder/about_content.txt
Use absolute path as you wrote in question.
If you want use relative path
Take a look at differrence of relative path vs absolute path. So if you place your file for example to /home/casper/QuickRecorder/about_content.txt and you know exactly that your build lirectory is /home/casper/app-build -- you can use relative path ../QuickRecorder/about_content.txt
Take a look at QDir class. It already consists lots of useful methods.
If you want to place your file to resources
That procedure is pretty simple. Just add resource you your project and add file to your resource according to The Qt Resource System. For example, you can add your file about_content.txt to .qrc file and use it in your program this way: QFile file(":/about_content.txt");.
You can get the path of the user's home directory using the static function QDir::homePath().
QString homePath = QDir::homePath();
QFile file(homePath + "/QuickRecorder/about_content.txt");
...

pass permanent parameter to a jar file

I have 3 jars: jar1, jar2 and jar3, in the same path who can change in other pc (ex: c:\prova)
When I run jar1, it moves jar2 in the Windows Sturtup folder.
I want that jar2 simply activate jar3 at every windows startup, but of course it doesn't find jar3 who is remained in the first path.
So I want that jar1 pass a reference (in this case the path c:\prova) to the jar2, when moving it, or at least on the first call to it.
I find it difficoult because:
I can't write the path in a text file in jar2: text files in jars aren't writable.
I can't write the text file in the windows Startup folder: it will be opened at every win startup..
I can't pass the path as a parameter, it will be good for the first call but I can't store this value for the succesive calls.
Sorry for my bad english, thanks for any help!
To add the file Path.txt (with jar3's path) in jar2:
Runtime.getRuntime().exec("jar uf jar2.jar Path.txt");
To read the file in jar2 (Startup is my class name):
String s = "/Path.txt";
is = Startup.class.getResourceAsStream(s);
br = new BufferedReader(new InputStreamReader(is));
while (null != (line = br.readLine())) {
list.add(line);
}
Thank me!

Resources