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 use addItem method of SplitView ?
Forum Updated to NodeBB v4.3 + New Features

How to use addItem method of SplitView ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.4k Views 2 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.
  • T Offline
    T Offline
    Tucky
    wrote on last edited by Tucky
    #1

    Dear All,

    I want to add an Item to a splitview on some event. I took the SplitView example from the documentation and added a MouseArea which call the addItem method with a Rectangle item. The problem is that I get an error message like:
    split.qml:71 Expected token `)' which correspond to the line splitview.addItem( Rectangle { Text { text: "new" } } )
    I don't understand what happens. Here is my code:

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.1
    
    SplitView {
    
        id: splitview
    
        width: 800
        height: 800
        orientation: Qt.Horizontal
    
        Rectangle {
            width: 200
            Layout.maximumWidth: 400
            color: "lightblue"
            Text {
                text: "View 1"
                anchors.centerIn: parent
            }
        }
        Rectangle {
            id: centerItem
            Layout.minimumWidth: 50
            Layout.fillWidth: true
            color: "lightgray"
            Text {
                text: "View 2"
                anchors.centerIn: parent
            }
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    splitview.addItem( Rectangle { Text { text: "new" } } )
                }
            }
        }
        Rectangle {
            width: 200
            color: "lightgreen"
            Text {
                text: "View 3"
                anchors.centerIn: parent
            }
        }
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      From what I've gathered, it should be something like

      Component {
         id: itemToAdd
         Rectangle {
             Text {
                  text: "new"
             }
         }
      }
      
      Rectangle {
              id: centerItem
      //your code
          onClicked: {
              var newItem = itemToAdd.createObject()
              splitView.addItem(newItem)
          }
      }
      

      Hope it helps

      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
      • T Offline
        T Offline
        Tucky
        wrote on last edited by
        #3

        Thanks, it helps indeed.
        You gave a solution, now for the explanation, if I understand correctly it’s you have to use the createObject method when you are in a javascritpt part.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          It's not because of the javascript part. createObject is used to create new objects dynamically the way you want it. What you where doing is declaring a Rectangle not instantiating a new one when calling addItem.

          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

          • Login

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