[Solved] StyleSheet with absolute path
-
wrote on 3 Jun 2012, 20:24 last edited by
Hello, can i use setStyleSheet(); with absolute path? for example in linux "/home/chris/Projects/Yamp/src/main.css" ?
because i get NullPointerException
-
wrote on 4 Jun 2012, 05:05 last edited by
You can write something like:
@QFile styleSheet("/home/chris/Projects/Yamp/src/main.qss"); //path where the file is stored
if (!styleSheet.open(QIODevice::ReadOnly)
{
qWarning("Unable to open :/home/chris/Projects/Yamp/src/main.qss");
return;
}
qApp->setStyleSheet(styleSheet.readAll());@ -
wrote on 4 Jun 2012, 08:45 last edited by
I get "Unable to open :/home/chris/Projects/Yamp/src/main.qss"
the ! in front of styleSheet.open(QIODevice::ReadOnly) it means if it False the qwarning....bla bla....?edit: if i use @setStyleSheet(css.loadStyles("yamp/main.css"));@ where css.loadStyles is this @package yamp.ui;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;public class LoadStyles {
public String loadStyles(String cssPath) { InputStream in = getClass().getClassLoader().getResourceAsStream(cssPath); BufferedReader r = new BufferedReader(new InputStreamReader(in)); StringBuilder s = new StringBuilder(); String line; try { while( (line = r.readLine()) != null ) { s.append(line); } } catch (IOException e) { e.printStackTrace(); } return s.toString();
}
}
@it works.
my guess is that because the path i use is inside the src folder of my application it works
-
wrote on 4 Jun 2012, 09:41 last edited by
Well i tested the same code in windows 7, i created the QFile by providing the absolute path. So you can check the absolute path and also the permissions on the file as you are working on linux.
You got the above message as the if() condition failed. So try a small example where you can create/open a file using QFile.
Also instead of specifying the absolutepath you can add a resource file to your project and then add the .qss file to the resource and write.
@QFile styleSheet(":StyleSheets/files/myStyleSheet.qss");
if (!styleSheet.open(QIODevice::ReadOnly))
{
qWarning("Unable to open :/files/myStyleSheet.qss");
return;
}
qApp->setStyleSheet(styleSheet.readAll());@ -
wrote on 4 Jun 2012, 10:58 last edited by
i can't use resources because what i am trying to do is create a "theming system" so my application can have many themes created by users etc...so i want to use absolute paths for *.css files
did it worked in windows?
PS: i am using qt jambi
-
wrote on 4 Jun 2012, 11:28 last edited by
yes it works on windows, Even for a themes application you can use resources, For my application i used the same as i find it more safe to load the files as they are there in resource folder.
You can have a look into "Embedded Styles example":http://doc.qt.nokia.com/4.7-snapshot/demos-embedded-styledemo.html
Edit : If you are loading the files which are created by the user then you need to use absolute path. So i guess that this issue is solved.
Happy Coding :)
-
wrote on 4 Jun 2012, 17:30 last edited by
i change the code and now its ok!
@public String loadStyles( String cssPath ) throws FileNotFoundException {
FileInputStream fstream = new FileInputStream(cssPath);
DataInputStream in = new DataInputStream(fstream);
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder s = new StringBuilder();
String line;try { while( (line = r.readLine()) != null ) { s.append(line); } } catch (IOException e) { e.printStackTrace(); } return s.toString(); }@
1/7