Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. globally defining colors
Qt 6.11 is out! See what's new in the release blog

globally defining colors

Scheduled Pinned Locked Moved Solved QML and Qt Quick
12 Posts 3 Posters 2.0k 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.
  • mzimmersM mzimmers

    Hi all -

    I've done some searching, and came across this:

    older forum post on defining colors globally

    Is the approach in the final post accurate? And, is this still the best way to do this? My app doesn't even have a qml.qrc file (though that seems easy enough to fix).

    Thanks...

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #2

    @mzimmers seems about right, Objects/Properties of top level items are propagated the the children, as long as there is no shadowing happening.

    My app doesn't even have a qml.qrc

    it should have a qrc file, if you do not want to ship the raw text(qml) files with your project. You can use any qrc file, doesn't have to be named qml


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    1 Reply Last reply
    1
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #3

      I use it for global settings as well. In your qml files, you sometimes hardcode things like height or width of certain items. Your code is cross platforms, but not screens. Better to scale them according to screen sizes and these scaling factors can stored in a similar way.

      1 Reply Last reply
      1
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #4

        I currently have two .qrc files, one for my fonts and one for my icons. Might it be preferable to have a "master" .qrc file, taking the place of these, and allowing me to declare my colors?

        I've never really understood prefixes in the .qrc file. I always thought of it as sort of the QML analog to C++ namespaces. Is this accurate, and would I use prefixes to keep things organized in the file?

        Thanks...

        1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #5

          I have two qrc files: one for images and another for qml files. Image and qml files are grouped in different subdirs. Alias is applied to define short names(no more dir names) for all files and convenient use.

          1 Reply Last reply
          0
          • mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by mzimmers
            #6

            Trying to follow the approach here, but getting an "undefined" error when I try to use one of the colors in main.qml.

            Colors.qml:

            pragma Singleton
            
            import QtQuick 2.0
            import QtQml
            
            Item { // the example calls for QtObject here, but Creator says "unknown component."
                property color accent: "#4147f0"
            ...
            

            qml.qrc:

            <RCC>
                <qresource prefix="/">
                    <file>Colors.qml</file>
                    <file>main.qml</file>
                    <file>qmldir</file>
                </qresource>
            </RCC>
            

            qmldir:

            singleton Colors Colors.qml
            

            All my files are in the same directory. What am I doing wrong?

            EDIT:

            It might have something to do with my inability to use QtObject. It's not clear to me whether I need something that I didn't select in my installation. Where does this component "live?"

            Thanks...

            1 Reply Last reply
            0
            • JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #7

              change item back to QtObject and do not worry about creator says
              register the type in your main.cpp
              qmlRegisterSingletonType( QUrl( "qrc:/Colors.qml" ), "zimmers.stylesheet", 1, 0, "Colors" );
              import zimmers.stylesheet in any qml and
              you can use it Colors.accent

              mzimmersM 1 Reply Last reply
              2
              • JoeCFDJ JoeCFD

                change item back to QtObject and do not worry about creator says
                register the type in your main.cpp
                qmlRegisterSingletonType( QUrl( "qrc:/Colors.qml" ), "zimmers.stylesheet", 1, 0, "Colors" );
                import zimmers.stylesheet in any qml and
                you can use it Colors.accent

                mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #8

                @JoeCFD thanks...that got rid of the warning, but my color isn't taking effect. Do I need to actually create a file zimmers.stylesheet, and if so, what should its contents be?

                JoeCFDJ 1 Reply Last reply
                0
                • mzimmersM mzimmers

                  @JoeCFD thanks...that got rid of the warning, but my color isn't taking effect. Do I need to actually create a file zimmers.stylesheet, and if so, what should its contents be?

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #9

                  @mzimmers zimmers.stylesheet is only package name and can be anything, totally up to you.
                  add console.log in qml file to print its value

                  mzimmersM 1 Reply Last reply
                  1
                  • JoeCFDJ JoeCFD

                    @mzimmers zimmers.stylesheet is only package name and can be anything, totally up to you.
                    add console.log in qml file to print its value

                    mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by
                    #10

                    @JoeCFD what do you want the console.log() to report on?

                    JoeCFDJ 1 Reply Last reply
                    0
                    • mzimmersM mzimmers

                      @JoeCFD what do you want the console.log() to report on?

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by
                      #11

                      @mzimmers Colors.accent

                      1 Reply Last reply
                      1
                      • mzimmersM Offline
                        mzimmersM Offline
                        mzimmers
                        wrote on last edited by
                        #12

                        Actually, it does seem to be working OK. Something in my StackLayout was screwing it up, so I disabled it, and am now seeing the expected color. Thanks!

                        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