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. How to set brush in QML code
Forum Updated to NodeBB v4.3 + New Features

How to set brush in QML code

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 5 Posters 1.4k 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.
  • V Offline
    V Offline
    VieiraFilho
    wrote on last edited by
    #1

    Hey guys

    I tried to find an example, but I didn't find anything.
    In ScatterSeries there is a property called brush.
    I want to define a color and fill pattern directly in QML code, such as ("red", Qt.SolidPattern).
    If property exists, I believe there is a way to do this.
    Does anyone have any example of how to set this property?

    raven-worxR 1 Reply Last reply
    0
    • V VieiraFilho

      Hey guys

      I tried to find an example, but I didn't find anything.
      In ScatterSeries there is a property called brush.
      I want to define a color and fill pattern directly in QML code, such as ("red", Qt.SolidPattern).
      If property exists, I believe there is a way to do this.
      Does anyone have any example of how to set this property?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @VieiraFilho said in How to set brush in QML code:

      If property exists, I believe there is a way to do this

      not necessarily. AFAIK QBrush is not contained in the QML engine as a type so you have to use C++.
      You can either pass the ScatterSeries object to an invokable C++ method (accepting a QObject* or QScatterSeries* parameter) and fill it there. Or you create an invokable C++ method which returns a QBrush from your call parameters packed in a QVariant (You must register the QBrush type with the meta type system).
      I am not quite sure if the second approach really works though, but try it.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      eyllanescE 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @VieiraFilho said in How to set brush in QML code:

        If property exists, I believe there is a way to do this

        not necessarily. AFAIK QBrush is not contained in the QML engine as a type so you have to use C++.
        You can either pass the ScatterSeries object to an invokable C++ method (accepting a QObject* or QScatterSeries* parameter) and fill it there. Or you create an invokable C++ method which returns a QBrush from your call parameters packed in a QVariant (You must register the QBrush type with the meta type system).
        I am not quite sure if the second approach really works though, but try it.

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by
        #3

        @raven-worx like this https://stackoverflow.com/a/59009525/6622587

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply
        0
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          @eyllanesc said in How to set brush in QML code:

          @raven-worx like this https://stackoverflow.com/a/59009525/6622587

          :/

          Definitely try the second method (a helper method returning a QBrush) before modifying your UI objects from your business code.

          eyllanescE raven-worxR 2 Replies Last reply
          0
          • GrecKoG GrecKo

            @eyllanesc said in How to set brush in QML code:

            @raven-worx like this https://stackoverflow.com/a/59009525/6622587

            :/

            Definitely try the second method (a helper method returning a QBrush) before modifying your UI objects from your business code.

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by
            #5

            @GrecKo I know it's not fancy, I just wanted to show an example of the first method. It would be more elegant if QML allows you to create those basic types from QML directly,

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            1 Reply Last reply
            0
            • GrecKoG GrecKo

              @eyllanesc said in How to set brush in QML code:

              @raven-worx like this https://stackoverflow.com/a/59009525/6622587

              :/

              Definitely try the second method (a helper method returning a QBrush) before modifying your UI objects from your business code.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @GrecKo said in How to set brush in QML code:

              before modifying your UI objects from your business code

              i was talking about modifying the ScatterSeries object, which actually acts as a model for charts if you want so.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #7

                It's not only a model, it's also a representation of the data. I maintain that you should not modify that in C++.

                The solution to OP's problem is the following:

                Create a class like that in C++

                class BrushFactory : public QObject
                {
                    Q_OBJECT
                
                  public:
                    Q_INVOKABLE QBrush brushFromColor(const QColor& color) // method returning a "fancy" brush
                    {
                      return QBrush(color, Qt::Dense4Pattern);
                    }
                };
                

                Register it as a singleton type to the QML engine, and us it in QML like so:

                ScatterSeries {
                    // ...
                    brush: BrushFactory.brushFromColor("pink")
                    //  ...
                }
                

                Note that if you just want to set a simple color on the series, it already has a color property.

                ndiasN 1 Reply Last reply
                1
                • GrecKoG GrecKo

                  It's not only a model, it's also a representation of the data. I maintain that you should not modify that in C++.

                  The solution to OP's problem is the following:

                  Create a class like that in C++

                  class BrushFactory : public QObject
                  {
                      Q_OBJECT
                  
                    public:
                      Q_INVOKABLE QBrush brushFromColor(const QColor& color) // method returning a "fancy" brush
                      {
                        return QBrush(color, Qt::Dense4Pattern);
                      }
                  };
                  

                  Register it as a singleton type to the QML engine, and us it in QML like so:

                  ScatterSeries {
                      // ...
                      brush: BrushFactory.brushFromColor("pink")
                      //  ...
                  }
                  

                  Note that if you just want to set a simple color on the series, it already has a color property.

                  ndiasN Offline
                  ndiasN Offline
                  ndias
                  wrote on last edited by
                  #8

                  @GrecKo said in How to set brush in QML code:

                  Note that if you just want to set a simple color on the series, it already has a color property.

                  Thanks for this tip. I hadn't noticed that this property was defined in the parent class XYSeries

                  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