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. Simple Qt Quick program
Forum Updated to NodeBB v4.3 + New Features

Simple Qt Quick program

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
16 Posts 6 Posters 3.8k 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by
    #1

    Please take a look at this section. I created a Qt Quick Application (without ui.forum.qml), added two .cpp files named Transformation.qml and ClickableImage.qml. And populate them this way: (But how to use them in main.qml?)

    // ClickableImage.qml
    // Simple image which can be clicked
    
    import QtQuick 2.6
    
    Image {
        id: root
        signal clicked
    
        MouseArea
        {
            anchors.fill: parent
            onClicked: root.clicked
        }
    }
    
    // transformation.qml
    
    
    import QtQuick 2.6
    
    Item {
        // set width based on given background
        width: bg.width
        height: bg.height
    
        Image {  // nice background image
            id: bg
            source: "background.png"
        }
    
        MouseArea {
            id: backgroundClicker
         // needs to be before the images as order matters
         // otherwise this mousearea would be before the other elements
         // and consume the mouse events
            anchors.fill: parent
            onClicked: {
                  // reset our little scene
    
                circle.x = 84;
                box.rotation = 0
                triangle.rotation = 0
                triangle.scale = 1.0
            }
        }
    
        ClickableImage {
            id: circle
            x: 84; y: 68
            source: "/circle_blue.png"
            antialiasing: true
            onClicked: // increase the x-position on clicked
                x += 20
        }
    
        ClickableImage {
            id: box
            x: 164; y: 68
            source: "/box_green.png"
            antialiasing: true
            onClicked:  // increase the rotation on click
             rotation += 15
        }
    
        ClickableImage {
            id: triangle
            x: 248; y: 68
            source: "/triangle_red.png"
            antialiasing: true
            onClicked: { // several transformations
             rotation += 15
                scale += 0.05
        }
      }
    
        function _test_transformed() {
                circle.x += 20
                box.rotation = 15
                triangle.scale = 1.2
                triangle.rotation = -15
            }
    
        function _test_overlap() {
                circle.x += 40
                box.rotation = 15
                triangle.scale = 2.0
                triangle.rotation = 45
            }
    
    }
    

    main.qml:

    import QtQuick 2.6
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Transformation")
    
    }
    

    Then added those three .png images into the folder of the project and also in qml.qrc.
    Now the program shows nothing on running. I know it's because we haven't used the qml files in main.qml. But how to do that? It hasn't been pointed to in that online book.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by
      #2

      Hi @tomy

      On the top of your Transformation.qml

      you have to add

      import "ClickableImage.qml"
      

      And on the top of your main.qml, you have to add

      import "Transformation.qml"

      and then add the Transform component, your code should look to something like this:

      main.qml:

      import QtQuick 2.6
      import QtQuick.Window 2.2
      
      import "Transformation.qml"
      
      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Transformation")
      
         Transformation
      {
             id: transformation
      .
      .
      .
      }
      }
      
      tomyT GrecKoG 2 Replies Last reply
      0
      • M mostefa

        Hi @tomy

        On the top of your Transformation.qml

        you have to add

        import "ClickableImage.qml"
        

        And on the top of your main.qml, you have to add

        import "Transformation.qml"

        and then add the Transform component, your code should look to something like this:

        main.qml:

        import QtQuick 2.6
        import QtQuick.Window 2.2
        
        import "Transformation.qml"
        
        Window {
            visible: true
            width: 640
            height: 480
            title: qsTr("Transformation")
        
           Transformation
        {
               id: transformation
        .
        .
        .
        }
        }
        
        tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by tomy
        #3

        @mostefa
        Thanks for the reply.

        import QtQuick 2.6
        import QtQuick.Window 2.2
        
        import "Transformation.qml"
        
        Window {
            visible: true
            width: 640
            height: 480
            title: qsTr("Transformation")
        
            Transformation {
                id: transitions
            }
        
        }
        
        

        I get this error:
        qrc:/main.qml:4 "Transformation.qml": no such directory

        M 1 Reply Last reply
        0
        • tomyT tomy

          @mostefa
          Thanks for the reply.

          import QtQuick 2.6
          import QtQuick.Window 2.2
          
          import "Transformation.qml"
          
          Window {
              visible: true
              width: 640
              height: 480
              title: qsTr("Transformation")
          
              Transformation {
                  id: transitions
              }
          
          }
          
          

          I get this error:
          qrc:/main.qml:4 "Transformation.qml": no such directory

          M Offline
          M Offline
          mostefa
          wrote on last edited by
          #4

          @tomy said in Simple Qt Quick program:

          @mostefa
          Thanks for the reply.

          import QtQuick 2.6
          import QtQuick.Window 2.2
          
          import "Transformation.qml"
          
          Window {
              visible: true
              width: 640
              height: 480
              title: qsTr("Transformation")
          
              Transformation {
                  id: transitions
              }
          
          }
          
          

          I get this error:
          qrc:/main.qml:4 "Transformation.qml": no such directory

          You have to change the name of your qml file from

          transformation.qml to Transformation.qml

          In qml custom components , the first letter must be in uppercase

          http://doc.qt.io/qt-5/qtqml-documents-definetypes.html#defining-an-object-type-with-a-qml-file

          tomyT 1 Reply Last reply
          2
          • M mostefa

            @tomy said in Simple Qt Quick program:

            @mostefa
            Thanks for the reply.

            import QtQuick 2.6
            import QtQuick.Window 2.2
            
            import "Transformation.qml"
            
            Window {
                visible: true
                width: 640
                height: 480
                title: qsTr("Transformation")
            
                Transformation {
                    id: transitions
                }
            
            }
            
            

            I get this error:
            qrc:/main.qml:4 "Transformation.qml": no such directory

            You have to change the name of your qml file from

            transformation.qml to Transformation.qml

            In qml custom components , the first letter must be in uppercase

            http://doc.qt.io/qt-5/qtqml-documents-definetypes.html#defining-an-object-type-with-a-qml-file

            tomyT Offline
            tomyT Offline
            tomy
            wrote on last edited by
            #5

            @mostefa
            I have done it.

            0_1505637566779_Capture.PNG

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              so now it works?
              make sure u really rebuild all, include the resource file.

              tomyT 1 Reply Last reply
              1
              • mrjjM mrjj

                so now it works?
                make sure u really rebuild all, include the resource file.

                tomyT Offline
                tomyT Offline
                tomy
                wrote on last edited by tomy
                #7

                @mrjj

                make sure u really rebuild all, include the resource file.

                I did it. The same error.

                M 1 Reply Last reply
                0
                • tomyT tomy

                  @mrjj

                  make sure u really rebuild all, include the resource file.

                  I did it. The same error.

                  M Offline
                  M Offline
                  mostefa
                  wrote on last edited by
                  #8

                  @tomy said in Simple Qt Quick program:

                  @mrjj

                  make sure u really rebuild all, include the resource file.

                  I did it. The same error.

                  What if you delete completely the build repository and then build again?

                  1 Reply Last reply
                  1
                  • M mostefa

                    Hi @tomy

                    On the top of your Transformation.qml

                    you have to add

                    import "ClickableImage.qml"
                    

                    And on the top of your main.qml, you have to add

                    import "Transformation.qml"

                    and then add the Transform component, your code should look to something like this:

                    main.qml:

                    import QtQuick 2.6
                    import QtQuick.Window 2.2
                    
                    import "Transformation.qml"
                    
                    Window {
                        visible: true
                        width: 640
                        height: 480
                        title: qsTr("Transformation")
                    
                       Transformation
                    {
                           id: transformation
                    .
                    .
                    .
                    }
                    }
                    
                    GrecKoG Offline
                    GrecKoG Offline
                    GrecKo
                    Qt Champions 2018
                    wrote on last edited by
                    #9

                    @mostefa said in Simple Qt Quick program:

                    On the top of your Transformation.qml

                    you have to add import "ClickableImage.qml"

                    And on the top of your main.qml, you have to add import "Transformation.qml"

                    No you don't.
                    Just name them with an uppercase first letter and then you can just use them like so Transformation {} if they are in the same folder as the qml file you are trying to use them (in your case main.qml).

                    1 Reply Last reply
                    4
                    • tomyT Offline
                      tomyT Offline
                      tomy
                      wrote on last edited by
                      #10

                      Thank you, it shows the images on the window but no action is done by clicking them! How to fix it? It seems it's because I haven't used Transformation.qml well in main.qml.

                      Here is main.qml:

                      import QtQuick 2.6
                      import QtQuick.Window 2.2
                      
                      Window {
                          visible: true
                          width: 800
                          height: 600
                          title: qsTr("Transformation")
                      
                          Transformation {
                              id: transformation
                          }
                      }
                      
                      

                      How to do that and what are the raison d'ĂȘtre of those two functions and how to use them? Thanks

                      1 Reply Last reply
                      0
                      • tomyT Offline
                        tomyT Offline
                        tomy
                        wrote on last edited by
                        #11

                        Isn't there any way to make it work?

                        E 1 Reply Last reply
                        0
                        • tomyT tomy

                          Isn't there any way to make it work?

                          E Offline
                          E Offline
                          Eeli K
                          wrote on last edited by
                          #12

                          @tomy I created a similar project and it works for me. Try copying the code once again into your files to make sure they are identical to the example code. You can also try adding some box_green.png file to assets, but other parts work without it for me.

                          The functions are apparently for testing purposes :) You can use them for example in this way:

                          import QtQuick 2.6
                          import QtQuick.Window 2.2
                          import QtQuick.Controls 2.0
                          Window {
                              visible: true
                              width: 640
                              height: 480
                              Column{
                                  spacing: 10
                                  Button {
                                      onClicked: transformation._test_overlap()
                                  }
                                  Button {
                                      onClicked: transformation._test_transformed()
                                  }
                          
                                  Transformation {
                                      id: transformation
                                  }
                              }
                          }
                          
                          1 Reply Last reply
                          1
                          • tomyT Offline
                            tomyT Offline
                            tomy
                            wrote on last edited by tomy
                            #13

                            Thank you for the answer.
                            I created another project that has this main.cpp:

                            #include <QGuiApplication>
                            #include <QQmlApplicationEngine>
                            
                            int main(int argc, char *argv[])
                            {
                                QGuiApplication app(argc, argv);
                            
                                QQmlApplicationEngine engine;
                                engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                                if (engine.rootObjects().isEmpty())
                                    return -1;
                            
                                return app.exec();
                            }
                            

                            By right clicking on the / folder below the qml.qrc in Projects view of Qt Creator and choosing Add New, I added a new C++ source file named ClickableImage.qml. It was added by its first letter in small so by renaming I capitalised it to big C and pasted this:

                            import QtQuick 2.8
                            Image {
                                id: root
                                signal clicked
                            
                                MouseArea
                                {
                                    anchors.fill: parent
                                    onClicked: root.clicked
                                }
                            }
                            

                            The same way for adding Transformation.qml with the code:

                            import QtQuick 2.8
                            Item {
                                width: bg.width
                                height: bg.height
                            
                                Image {
                                    id: bg
                                    source: "/background.png"
                                }
                            
                                MouseArea {
                                    id: backgroundClicker
                                    anchors.fill: parent
                                    onClicked: {
                                        circle.x = 84
                                        box.rotation = 0
                                        triangle.rotation = 0
                                        triangle.scale = 1.0
                                    }
                                }
                            
                                ClickableImage {
                                    id: circle
                                    x: 84; y: 68
                                    source: "/circle_blue.png"
                                    antialiasing: true
                                    onClicked: x += 20
                                }
                            
                                ClickableImage {
                                    id: box
                                    x: 164; y: 68
                                    source: "/box_green.png"
                                    antialiasing: true
                                    onClicked: rotation += 15
                                }
                            
                                ClickableImage {
                                    id: triangle
                                    x: 248; y: 68
                                    source: "/triangle_red.png"
                                    antialiasing: true
                                    onClicked: {
                                        rotation += 15
                                        scale += 0.05
                                    }
                                }
                            
                                function _test_transformed() {
                                    circle.x += 20
                                    box.rotation = 15
                                    triangle.scale = 1.2
                                    triangle.rotation = -15
                                }
                            
                                function _test_overlap() {
                                    circle.x += 40
                                    box.rotation = 15
                                    triangle.scale = 2.0
                                    triangle.rotation = 45
                                }
                            
                            }
                            

                            Finally added four images with the prefix / to the resource.

                            And since I'd capitalised the file names I did: clean all, run qmake, rebuild all. And ran the program.

                            The images are shown with no reaction on clicking them. I'm rather astonished what the problem is that it works for you but not me!

                            Please tell me what part do I have done wrong? And is this process exactly what you too have done?

                            E J.HilkJ 2 Replies Last reply
                            0
                            • tomyT tomy

                              Thank you for the answer.
                              I created another project that has this main.cpp:

                              #include <QGuiApplication>
                              #include <QQmlApplicationEngine>
                              
                              int main(int argc, char *argv[])
                              {
                                  QGuiApplication app(argc, argv);
                              
                                  QQmlApplicationEngine engine;
                                  engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                                  if (engine.rootObjects().isEmpty())
                                      return -1;
                              
                                  return app.exec();
                              }
                              

                              By right clicking on the / folder below the qml.qrc in Projects view of Qt Creator and choosing Add New, I added a new C++ source file named ClickableImage.qml. It was added by its first letter in small so by renaming I capitalised it to big C and pasted this:

                              import QtQuick 2.8
                              Image {
                                  id: root
                                  signal clicked
                              
                                  MouseArea
                                  {
                                      anchors.fill: parent
                                      onClicked: root.clicked
                                  }
                              }
                              

                              The same way for adding Transformation.qml with the code:

                              import QtQuick 2.8
                              Item {
                                  width: bg.width
                                  height: bg.height
                              
                                  Image {
                                      id: bg
                                      source: "/background.png"
                                  }
                              
                                  MouseArea {
                                      id: backgroundClicker
                                      anchors.fill: parent
                                      onClicked: {
                                          circle.x = 84
                                          box.rotation = 0
                                          triangle.rotation = 0
                                          triangle.scale = 1.0
                                      }
                                  }
                              
                                  ClickableImage {
                                      id: circle
                                      x: 84; y: 68
                                      source: "/circle_blue.png"
                                      antialiasing: true
                                      onClicked: x += 20
                                  }
                              
                                  ClickableImage {
                                      id: box
                                      x: 164; y: 68
                                      source: "/box_green.png"
                                      antialiasing: true
                                      onClicked: rotation += 15
                                  }
                              
                                  ClickableImage {
                                      id: triangle
                                      x: 248; y: 68
                                      source: "/triangle_red.png"
                                      antialiasing: true
                                      onClicked: {
                                          rotation += 15
                                          scale += 0.05
                                      }
                                  }
                              
                                  function _test_transformed() {
                                      circle.x += 20
                                      box.rotation = 15
                                      triangle.scale = 1.2
                                      triangle.rotation = -15
                                  }
                              
                                  function _test_overlap() {
                                      circle.x += 40
                                      box.rotation = 15
                                      triangle.scale = 2.0
                                      triangle.rotation = 45
                                  }
                              
                              }
                              

                              Finally added four images with the prefix / to the resource.

                              And since I'd capitalised the file names I did: clean all, run qmake, rebuild all. And ran the program.

                              The images are shown with no reaction on clicking them. I'm rather astonished what the problem is that it works for you but not me!

                              Please tell me what part do I have done wrong? And is this process exactly what you too have done?

                              E Offline
                              E Offline
                              Eeli K
                              wrote on last edited by
                              #14

                              @tomy You should add New File -> Qt -> QML file, not c++ file. But it shouldn't matter, I think, because the objects are visible when you run it. It sounds weird that graphics is well but not mouse clicks. But try again with QML file because it may mess up the project file. You can also show the contents of your .pro file.

                              1 Reply Last reply
                              1
                              • tomyT tomy

                                Thank you for the answer.
                                I created another project that has this main.cpp:

                                #include <QGuiApplication>
                                #include <QQmlApplicationEngine>
                                
                                int main(int argc, char *argv[])
                                {
                                    QGuiApplication app(argc, argv);
                                
                                    QQmlApplicationEngine engine;
                                    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                                    if (engine.rootObjects().isEmpty())
                                        return -1;
                                
                                    return app.exec();
                                }
                                

                                By right clicking on the / folder below the qml.qrc in Projects view of Qt Creator and choosing Add New, I added a new C++ source file named ClickableImage.qml. It was added by its first letter in small so by renaming I capitalised it to big C and pasted this:

                                import QtQuick 2.8
                                Image {
                                    id: root
                                    signal clicked
                                
                                    MouseArea
                                    {
                                        anchors.fill: parent
                                        onClicked: root.clicked
                                    }
                                }
                                

                                The same way for adding Transformation.qml with the code:

                                import QtQuick 2.8
                                Item {
                                    width: bg.width
                                    height: bg.height
                                
                                    Image {
                                        id: bg
                                        source: "/background.png"
                                    }
                                
                                    MouseArea {
                                        id: backgroundClicker
                                        anchors.fill: parent
                                        onClicked: {
                                            circle.x = 84
                                            box.rotation = 0
                                            triangle.rotation = 0
                                            triangle.scale = 1.0
                                        }
                                    }
                                
                                    ClickableImage {
                                        id: circle
                                        x: 84; y: 68
                                        source: "/circle_blue.png"
                                        antialiasing: true
                                        onClicked: x += 20
                                    }
                                
                                    ClickableImage {
                                        id: box
                                        x: 164; y: 68
                                        source: "/box_green.png"
                                        antialiasing: true
                                        onClicked: rotation += 15
                                    }
                                
                                    ClickableImage {
                                        id: triangle
                                        x: 248; y: 68
                                        source: "/triangle_red.png"
                                        antialiasing: true
                                        onClicked: {
                                            rotation += 15
                                            scale += 0.05
                                        }
                                    }
                                
                                    function _test_transformed() {
                                        circle.x += 20
                                        box.rotation = 15
                                        triangle.scale = 1.2
                                        triangle.rotation = -15
                                    }
                                
                                    function _test_overlap() {
                                        circle.x += 40
                                        box.rotation = 15
                                        triangle.scale = 2.0
                                        triangle.rotation = 45
                                    }
                                
                                }
                                

                                Finally added four images with the prefix / to the resource.

                                And since I'd capitalised the file names I did: clean all, run qmake, rebuild all. And ran the program.

                                The images are shown with no reaction on clicking them. I'm rather astonished what the problem is that it works for you but not me!

                                Please tell me what part do I have done wrong? And is this process exactly what you too have done?

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

                                @tomy you need to add brackets to function calls:

                                //ClickableImage
                                Image {
                                    id: root
                                    signal clicked()//added brackets
                                
                                    MouseArea
                                    {
                                        anchors.fill: parent
                                        onClicked:{
                                            console.log("Clicked")//Added Console output
                                            root.clicked()//added brackets
                                        }
                                    }
                                }
                                

                                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.

                                tomyT 1 Reply Last reply
                                3
                                • J.HilkJ J.Hilk

                                  @tomy you need to add brackets to function calls:

                                  //ClickableImage
                                  Image {
                                      id: root
                                      signal clicked()//added brackets
                                  
                                      MouseArea
                                      {
                                          anchors.fill: parent
                                          onClicked:{
                                              console.log("Clicked")//Added Console output
                                              root.clicked()//added brackets
                                          }
                                      }
                                  }
                                  
                                  tomyT Offline
                                  tomyT Offline
                                  tomy
                                  wrote on last edited by tomy
                                  #16

                                  @J.Hilk
                                  Thank you. It solved the issue.

                                  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