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. Defining custom colors in qml
Qt 6.11 is out! See what's new in the release blog

Defining custom colors in qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 4 Posters 11.7k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to devnet,

    You can use a Style singleton like described here

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • vikramgV Offline
      vikramgV Offline
      vikramg
      wrote on last edited by
      #3

      I tried the following based on the singleton example:

      Colors.qml:

      pragma Singleton
      import QtQuick 2.0
      
      QtObject {
          property color tealish_black: "#030b0d"
          property color light_sea_green: "#a1ede8"
      }
      

      qmldir file:

      module CustomStyles
      singleton Colors 1.0 Colors.qml
      

      In main.qml:

      import CustomStyles 1.0
      

      However, QtCreator flags an error saying "QML module not found". If I try to run anyway, the build error is 'module "CustomStyles" is not installed'. The import path section says that the default path does include the current directory. Right now everything I am doing is indeed in one directory, so this should not have been a problem. What am I missing?

      As an aside, what is the right way to create a qmldir file from inside Qt Creator? The New File dialog does not seem to suggest any suitable options.

      1 Reply Last reply
      0
      • benlauB Offline
        benlauB Offline
        benlau
        Qt Champions 2016
        wrote on last edited by benlau
        #4

        @vikramg said:

        However, QtCreator flags an error saying "QML module not found".

        You need to set "QML_IMPORT_PATH" in your .pro/.pri file. And run Tools -> QML/JS -> Reset Code Model in Qt Creator.

        For example:
        quickandroid/quickandroid.pri at master · benlau/quickandroid

        If I try to run anyway, the build error is 'module "CustomStyles" is not installed'. The import path section says that the default path does include the current directory. Right now everything I am doing is indeed in one directory, so this should not have been a problem. What am I missing?

        Is CustomStyles folder located in the same directory as the entry point of your QML application? If not, you need to add the import path to QQmlEngine/ QQmlApplicationEngine.

        As an aside, what is the right way to create a qmldir file from inside Qt Creator? The New File dialog does not seem to suggest any suitable options.

        I usually just use "New File -> General -> Empty File"

        1 Reply Last reply
        1
        • vikramgV Offline
          vikramgV Offline
          vikramg
          wrote on last edited by
          #5

          I originally did not have CustomStyles as a subfolder; all files were in a single flat application directory. Moving the module to a CustomStyles subfolder got rid of the Qt Creator "QML module not found" error. But still stuck at 'module "CustomStyles" is not installed'. I have the following:

          Base application directory QtQTest contains:

          main.cpp
          deployment.pri
          main.qml
          MainForm.ui.qml
          qml.qrc
          QtQTest.pro
          QtQTest.pro.user

          Subfolder CustomStyles contains:

          Colors.qml
          customstyles.qrc
          qmldir

          .pro file has:
          RESOURCES += qml.qrc \
          CustomStyles/customstyles.qrc
          QML_IMPORT_PATH += $$PWD

          qml.qrc:

          <RCC>
              <qresource prefix="/">
                  <file>main.qml</file>
                  <file>MainForm.ui.qml</file>
              </qresource>
          </RCC>
          

          CustomStyles/customstyles.qrc:

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

          Added imports to main.cpp:

              QQmlApplicationEngine engine;
              engine.addImportPath("qrc:/");
              engine.addImportPath("qrc:/CustomStyles/");
          

          Can't get past the 'module "CustomStyles" is not installed' error.

          I feel like I'm taking stabs in the dark. Is there a tutorial that explains how the directories are structured and what files go where?

          benlauB 1 Reply Last reply
          0
          • vikramgV vikramg

            I originally did not have CustomStyles as a subfolder; all files were in a single flat application directory. Moving the module to a CustomStyles subfolder got rid of the Qt Creator "QML module not found" error. But still stuck at 'module "CustomStyles" is not installed'. I have the following:

            Base application directory QtQTest contains:

            main.cpp
            deployment.pri
            main.qml
            MainForm.ui.qml
            qml.qrc
            QtQTest.pro
            QtQTest.pro.user

            Subfolder CustomStyles contains:

            Colors.qml
            customstyles.qrc
            qmldir

            .pro file has:
            RESOURCES += qml.qrc \
            CustomStyles/customstyles.qrc
            QML_IMPORT_PATH += $$PWD

            qml.qrc:

            <RCC>
                <qresource prefix="/">
                    <file>main.qml</file>
                    <file>MainForm.ui.qml</file>
                </qresource>
            </RCC>
            

            CustomStyles/customstyles.qrc:

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

            Added imports to main.cpp:

                QQmlApplicationEngine engine;
                engine.addImportPath("qrc:/");
                engine.addImportPath("qrc:/CustomStyles/");
            

            Can't get past the 'module "CustomStyles" is not installed' error.

            I feel like I'm taking stabs in the dark. Is there a tutorial that explains how the directories are structured and what files go where?

            benlauB Offline
            benlauB Offline
            benlau
            Qt Champions 2016
            wrote on last edited by benlau
            #6

            @vikramg said:

            CustomStyles/customstyles.qrc:

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

            Now you put Colors.qml to qrc:/// instead of qrc:///CustomStyles. In fact, you don't really customstyles.qrc. Instead , you may add Colors.qml and CustomStyles/qmldir to qml.qrc.

            vikramgV 1 Reply Last reply
            0
            • ekkescornerE Offline
              ekkescornerE Offline
              ekkescorner
              Qt Champions 2016
              wrote on last edited by
              #7

              why not simply do it this way:

              main.qml

              ApplicationWindow {
                  id: root
                  property color light_sea_green: "#a1ede8"
              

              .......
              Another.qml

              Label {
                  text: "my light sea green from root"
                  color: root.light_sea_green
              }
              

              ekke ... Qt Champion 2016 | 2024 ... mobile business apps
              5.15 --> 6.9 https://t1p.de/ekkeChecklist
              QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

              vikramgV 1 Reply Last reply
              0
              • benlauB benlau

                @vikramg said:

                CustomStyles/customstyles.qrc:

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

                Now you put Colors.qml to qrc:/// instead of qrc:///CustomStyles. In fact, you don't really customstyles.qrc. Instead , you may add Colors.qml and CustomStyles/qmldir to qml.qrc.

                vikramgV Offline
                vikramgV Offline
                vikramg
                wrote on last edited by
                #8

                @benlau said:

                Now you put Colors.qml to qrc:/// instead of qrc:///CustomStyles. In fact, you don't really customstyles.qrc. Instead , you may add Colors.qml and CustomStyles/qmldir to qml.qrc.

                Ah, because of the <qresource prefix="/">. Although I had originally tried adding CustomStyles/Colors.qml and CustomStyles/qmldir to the top-level qml.qrc and it still doesn't work ('module "CustomStyles" version 1.0 is not installed). There is some progress, the version number is now displayed in the error message, but it still won't run.

                benlauB 1 Reply Last reply
                0
                • ekkescornerE ekkescorner

                  why not simply do it this way:

                  main.qml

                  ApplicationWindow {
                      id: root
                      property color light_sea_green: "#a1ede8"
                  

                  .......
                  Another.qml

                  Label {
                      text: "my light sea green from root"
                      color: root.light_sea_green
                  }
                  
                  vikramgV Offline
                  vikramgV Offline
                  vikramg
                  wrote on last edited by
                  #9

                  @ekkescorner
                  Is there a way to "include" the custom color property's as a file into the main.qml? I would want to use the same set of colors in multiple projects, and I suppose I could copy-paste all the definitions, but would be best to have them in a separate file. Hence the attempt to group them in a Colors.qml.

                  1 Reply Last reply
                  0
                  • vikramgV vikramg

                    @benlau said:

                    Now you put Colors.qml to qrc:/// instead of qrc:///CustomStyles. In fact, you don't really customstyles.qrc. Instead , you may add Colors.qml and CustomStyles/qmldir to qml.qrc.

                    Ah, because of the <qresource prefix="/">. Although I had originally tried adding CustomStyles/Colors.qml and CustomStyles/qmldir to the top-level qml.qrc and it still doesn't work ('module "CustomStyles" version 1.0 is not installed). There is some progress, the version number is now displayed in the error message, but it still won't run.

                    benlauB Offline
                    benlauB Offline
                    benlau
                    Qt Champions 2016
                    wrote on last edited by
                    #10

                    @vikramg

                    @vikramg said:

                    @benlau said:

                    Now you put Colors.qml to qrc:/// instead of qrc:///CustomStyles. In fact, you don't really customstyles.qrc. Instead , you may add Colors.qml and CustomStyles/qmldir to qml.qrc.

                    Ah, because of the <qresource prefix="/">.
                    yes

                    Although I had originally tried adding CustomStyles/Colors.qml and CustomStyles/qmldir to the top-level qml.qrc and it still doesn't work ('module "CustomStyles" version 1.0 is not installed). There is some progress, the version number is now displayed in the error message, but it still won't run.

                    That is a working example, you may use it as a reference:

                    quickandroid/Constants.qml at master · benlau/quickandroid
                    quickandroid/qmldir at master · benlau/quickandroid
                    quickandroid/quickandroid.qrc at master · benlau/quickandroid

                    1 Reply Last reply
                    0
                    • vikramgV Offline
                      vikramgV Offline
                      vikramg
                      wrote on last edited by
                      #11

                      Finally was able to get this to work, using Approach 2 from here:
                      http://wiki.qt.io/QmlStyling

                      All that is needed is the following-

                      ./CustomStyles/Colors.qml:

                      pragma Singleton
                      import QtQuick 2.5
                      
                      QtObject {
                          readonly property color tealish_black: "#030b0d"
                          readonly property color light_sea_green: "#a1ede8"
                      }
                      

                      ./CustomStyles/qmldir:

                      singleton Colors Colors.qml
                      

                      ./main.qml:

                      import "./CustomStyles"
                      

                      ./qml.qrc:

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

                      Now colors from Colors.qml can be accessed thus in main.qml, and the project runs without errors:

                          color: Colors.light_sea_green
                      
                      1 Reply Last reply
                      0

                      • Login

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