Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] StyleSheet with absolute path
Forum Updated to NodeBB v4.3 + New Features

[Solved] StyleSheet with absolute path

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 4.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    clepto
    wrote on 3 Jun 2012, 20:24 last edited by
    #1

    Hello, can i use setStyleSheet(); with absolute path? for example in linux "/home/chris/Projects/Yamp/src/main.css" ?

    because i get NullPointerException

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on 4 Jun 2012, 05:05 last edited by
      #2

      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());@

      1 Reply Last reply
      0
      • C Offline
        C Offline
        clepto
        wrote on 4 Jun 2012, 08:45 last edited by
        #3

        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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on 4 Jun 2012, 09:41 last edited by
          #4

          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());@

          1 Reply Last reply
          0
          • C Offline
            C Offline
            clepto
            wrote on 4 Jun 2012, 10:58 last edited by
            #5

            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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sam
              wrote on 4 Jun 2012, 11:28 last edited by
              #6

              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 :)

              1 Reply Last reply
              0
              • C Offline
                C Offline
                clepto
                wrote on 4 Jun 2012, 17:30 last edited by
                #7

                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 Reply Last reply
                0

                1/7

                3 Jun 2012, 20:24

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved