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 can I add a background image in a QML chart
Forum Update on Monday, May 27th 2025

How can I add a background image in a QML chart

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 5.9k Views
  • 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.
  • B Offline
    B Offline
    Bert59
    wrote on 13 May 2020, 10:13 last edited by
    #1

    Hi,

    I would like to add a background image to a QML chart.
    I found a post on the subject for Widget chart
    https://forum.qt.io/topic/108046/qchart-background-image
    But I couldn't find the equivalent to
    chart->setPlotAreaBackgroundBrush(translated);
    chart->setPlotAreaBackgroundVisible(true);
    in QML

    Has someone an idea?
    Thanks

    1 Reply Last reply
    1
    • B Bert59
      14 May 2020, 09:37

      Thank you for your feedback.

      Here is what I did:

      Window {
      visible: true
      width: 640
      height: 480
      title: qsTr("Hello World")

      ColumnLayout{
          anchors.horizontalCenter: parent.horizontalCenter
          anchors.fill: parent
      
          ChartView {
              id: line
              backgroundColor: "transparent"
              Layout.fillHeight: true
              Layout.fillWidth: true
      
              Image{
                  id: image2
                  anchors.fill: line
                  source: "qrc:/image/Image.jpg"
                  opacity: 0.5
      
              }
      
              LineSeries {
                  name: "LineSeries"
                  XYPoint {
                      x: 0
                      y: 2
                  }
      
                  XYPoint {
                      x: 1
                      y: 1.2
                  }
      
                  XYPoint {
                      x: 2
                      y: 3.3
                  }
      
                  XYPoint {
                      x: 5
                      y: 2.1
                  }
              }
         }
      
          RowLayout{
              Button{
                  text: "Button 1"
                  Layout.fillWidth: true
      
              }
      
              Button{
                  text: "Button 2"
                  Layout.fillWidth: true
              }
          }
      }
      

      }

      But the image is always on top of the ChartView and hides the plots.
      I can see the plots below by setting the opacity to less than 1.
      How can I put the image below the Chart?
      I tried z: 0, but it has no effect
      How can I resize the image to fit inside the axes of the chart?

      Thank you

      K Offline
      K Offline
      KroMignon
      wrote on 14 May 2020, 09:42 last edited by
      #4

      @Bert59 You have place image into the chartview... so it is over it.
      @fcarney Place image before the Chart, so it is behind!

      You can play with Z order to put image behind.

      ChartView {
      ...
             Image {
                  id: image2
                  anchors.fill: parent
                  source: "qrc:/image/Image.jpg"
                  opacity: 0.5
                  z: -1
              }
      ...
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      1
      • F Offline
        F Offline
        fcarney
        wrote on 13 May 2020, 21:14 last edited by
        #2

        Can you set the background to transparent? Is it already transparent? Then place an image that fills the chart area, but is behind the chart?

        Image {
          anchors.fill: chartitem
        }
        Chart {
          id: chartitem
        }
        

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bert59
          wrote on 14 May 2020, 09:37 last edited by
          #3

          Thank you for your feedback.

          Here is what I did:

          Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")

          ColumnLayout{
              anchors.horizontalCenter: parent.horizontalCenter
              anchors.fill: parent
          
              ChartView {
                  id: line
                  backgroundColor: "transparent"
                  Layout.fillHeight: true
                  Layout.fillWidth: true
          
                  Image{
                      id: image2
                      anchors.fill: line
                      source: "qrc:/image/Image.jpg"
                      opacity: 0.5
          
                  }
          
                  LineSeries {
                      name: "LineSeries"
                      XYPoint {
                          x: 0
                          y: 2
                      }
          
                      XYPoint {
                          x: 1
                          y: 1.2
                      }
          
                      XYPoint {
                          x: 2
                          y: 3.3
                      }
          
                      XYPoint {
                          x: 5
                          y: 2.1
                      }
                  }
             }
          
              RowLayout{
                  Button{
                      text: "Button 1"
                      Layout.fillWidth: true
          
                  }
          
                  Button{
                      text: "Button 2"
                      Layout.fillWidth: true
                  }
              }
          }
          

          }

          But the image is always on top of the ChartView and hides the plots.
          I can see the plots below by setting the opacity to less than 1.
          How can I put the image below the Chart?
          I tried z: 0, but it has no effect
          How can I resize the image to fit inside the axes of the chart?

          Thank you

          K 1 Reply Last reply 14 May 2020, 09:42
          0
          • B Bert59
            14 May 2020, 09:37

            Thank you for your feedback.

            Here is what I did:

            Window {
            visible: true
            width: 640
            height: 480
            title: qsTr("Hello World")

            ColumnLayout{
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.fill: parent
            
                ChartView {
                    id: line
                    backgroundColor: "transparent"
                    Layout.fillHeight: true
                    Layout.fillWidth: true
            
                    Image{
                        id: image2
                        anchors.fill: line
                        source: "qrc:/image/Image.jpg"
                        opacity: 0.5
            
                    }
            
                    LineSeries {
                        name: "LineSeries"
                        XYPoint {
                            x: 0
                            y: 2
                        }
            
                        XYPoint {
                            x: 1
                            y: 1.2
                        }
            
                        XYPoint {
                            x: 2
                            y: 3.3
                        }
            
                        XYPoint {
                            x: 5
                            y: 2.1
                        }
                    }
               }
            
                RowLayout{
                    Button{
                        text: "Button 1"
                        Layout.fillWidth: true
            
                    }
            
                    Button{
                        text: "Button 2"
                        Layout.fillWidth: true
                    }
                }
            }
            

            }

            But the image is always on top of the ChartView and hides the plots.
            I can see the plots below by setting the opacity to less than 1.
            How can I put the image below the Chart?
            I tried z: 0, but it has no effect
            How can I resize the image to fit inside the axes of the chart?

            Thank you

            K Offline
            K Offline
            KroMignon
            wrote on 14 May 2020, 09:42 last edited by
            #4

            @Bert59 You have place image into the chartview... so it is over it.
            @fcarney Place image before the Chart, so it is behind!

            You can play with Z order to put image behind.

            ChartView {
            ...
                   Image {
                        id: image2
                        anchors.fill: parent
                        source: "qrc:/image/Image.jpg"
                        opacity: 0.5
                        z: -1
                    }
            ...
            }
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            1
            • B Offline
              B Offline
              Bert59
              wrote on 14 May 2020, 09:52 last edited by
              #5

              Thank you,
              With z: -1 it works.
              I tried z: 0 which was not working. I didn' know that z can have negative values. I started learning QML just a month ago.

              Do you have an idea how I could scale the image to fit inside the axes limits?

              K 1 Reply Last reply 14 May 2020, 10:16
              0
              • B Bert59
                14 May 2020, 09:52

                Thank you,
                With z: -1 it works.
                I tried z: 0 which was not working. I didn' know that z can have negative values. I started learning QML just a month ago.

                Do you have an idea how I could scale the image to fit inside the axes limits?

                K Offline
                K Offline
                KroMignon
                wrote on 14 May 2020, 10:16 last edited by KroMignon
                #6

                @Bert59 said in How can I add a background image in a QML chart:

                Do you have an idea how I could scale the image to fit inside the axes limits?

                I think you can do it with property plotArea.
                Perhaps something like (have not tested it):

                ChartView {
                ...
                       Image {
                            id: image2
                            x: plotArea.x
                            y: plotArea.y
                            width: plotArea.width
                            height: plotArea.height
                            source: "qrc:/image/Image.jpg"
                            opacity: 0.5
                            z: -1
                        }
                ...
                }
                

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                1
                • B Offline
                  B Offline
                  Bert59
                  wrote on 14 May 2020, 11:36 last edited by
                  #7

                  It works.
                  Thank you

                  1 Reply Last reply
                  0

                  1/7

                  13 May 2020, 10:13

                  • 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