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. CSS modifying the source location for images.
Qt 6.11 is out! See what's new in the release blog

CSS modifying the source location for images.

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.0k Views 3 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I have the following CSS for a QFrame:

    QFrame {background-color: #e9efef;
    	background-image: url("meshtile_4x4.png");
            border: 2px solid #cccccc;
      	border-top-left-radius: 8px;
            border-top-right-radius: 8px;
    	border-bottom-left-radius: 8px;
    	border-bottom-right-radius: 8px;}
    

    This CSS is specified in an XML file which my C++ application reads at startup. The problem is the application doesn't locate the image. I've added the following to my XML file:

        images="~/XMLMPAM/config/images/"
    

    The intention is that this attribute is read by the C++ application and used to resolve any image specified in the CSS. The question is how to I implement / modify any image path to include this?

    I could implement this when reading the XML file, just curious if there is a better way to implement this?

    Kind Regards,
    Sy

    Pl45m4P JonBJ 2 Replies Last reply
    0
    • SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #9

      Thank you, I've since implemented a working solution.

      Kind Regards,
      Sy

      1 Reply Last reply
      0
      • SPlattenS SPlatten

        I have the following CSS for a QFrame:

        QFrame {background-color: #e9efef;
        	background-image: url("meshtile_4x4.png");
                border: 2px solid #cccccc;
          	border-top-left-radius: 8px;
                border-top-right-radius: 8px;
        	border-bottom-left-radius: 8px;
        	border-bottom-right-radius: 8px;}
        

        This CSS is specified in an XML file which my C++ application reads at startup. The problem is the application doesn't locate the image. I've added the following to my XML file:

            images="~/XMLMPAM/config/images/"
        

        The intention is that this attribute is read by the C++ application and used to resolve any image specified in the CSS. The question is how to I implement / modify any image path to include this?

        I could implement this when reading the XML file, just curious if there is a better way to implement this?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #2

        @SPlatten

        You could add one or more qrc resource(s) for your images and then switch between them.

        https://doc.qt.io/qt-5/resources.html


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        1
        • SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #3

          Thank you, although thats really the exact opposite of why I'm doing this. My approach is to not put anything in the build or compiled application, I want to use the XML to give a dynamic approach to the resources so the C++ engine will remain unchanged and the look and feel of the application is supplied in the XML.

          Kind Regards,
          Sy

          Pl45m4P 1 Reply Last reply
          0
          • SPlattenS SPlatten

            Thank you, although thats really the exact opposite of why I'm doing this. My approach is to not put anything in the build or compiled application, I want to use the XML to give a dynamic approach to the resources so the C++ engine will remain unchanged and the look and feel of the application is supplied in the XML.

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #4

            @SPlatten said in CSS modifying the source location for images.:

            I want to use the XML to give a dynamic approach to the resources

            So you want to load images or styles from theoretically everywhere, not only from your app / build directory?


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            0
            • SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #5

              Anywhere on the local file system.

              Kind Regards,
              Sy

              1 Reply Last reply
              0
              • Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #6

                If I got it right, you want to keep this images="~/XMLMPAM/config/images/" variable in your XML / CSS file to define a path for background-image: url("meshtile_4x4.png"); without changing anything in your application or at runtime?!

                I don't know if this is even possible, without parsing and re-writing the same CSS everything you change the path. But I wouldn't consider myself an expert when it comes to XML or CSS ;-)


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                0
                • SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #7

                  The engine I referred to is the C++ which I'm modifying now to look at the CSS attribute and if it finds a URL(, it will then apply the images folder which it has translated into a full path.

                  Kind Regards,
                  Sy

                  1 Reply Last reply
                  0
                  • S Online
                    S Online
                    SimonSchroeder
                    wrote on last edited by
                    #8

                    I guess what your looking for is void QDir::setSearchPaths(const QString &prefix, const QStringList &searchPaths):

                    Sets or replaces Qt's search paths for file names with the prefix prefix to searchPaths.
                    To specify a prefix for a file name, prepend the prefix followed by a single colon (e.g., "images:undo.png", "xmldocs:books.xml"). prefix can only contain letters or numbers (e.g., it cannot contain a colon, nor a slash).

                    I haven't used it myself so far. But it could help with your problem.

                    1 Reply Last reply
                    1
                    • SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by
                      #9

                      Thank you, I've since implemented a working solution.

                      Kind Regards,
                      Sy

                      1 Reply Last reply
                      0
                      • SPlattenS SPlatten

                        I have the following CSS for a QFrame:

                        QFrame {background-color: #e9efef;
                        	background-image: url("meshtile_4x4.png");
                                border: 2px solid #cccccc;
                          	border-top-left-radius: 8px;
                                border-top-right-radius: 8px;
                        	border-bottom-left-radius: 8px;
                        	border-bottom-right-radius: 8px;}
                        

                        This CSS is specified in an XML file which my C++ application reads at startup. The problem is the application doesn't locate the image. I've added the following to my XML file:

                            images="~/XMLMPAM/config/images/"
                        

                        The intention is that this attribute is read by the C++ application and used to resolve any image specified in the CSS. The question is how to I implement / modify any image path to include this?

                        I could implement this when reading the XML file, just curious if there is a better way to implement this?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #10

                        @SPlatten said in CSS modifying the source location for images.:

                        I could implement this when reading the XML file, just curious if there is a better way to implement this?

                        That is how I would do it. Similarly, when I've needed to modify a QSS/CSS file for something which has to be dynamic beyond what QSS/CSS supports statically, I pre-process the stylesheet file before applying it to the application.

                        1 Reply Last reply
                        1

                        • Login

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