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. [SOLVED] Add (and change) a component in a placeholder
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Add (and change) a component in a placeholder

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 2.1k 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.
  • J Offline
    J Offline
    jrlaff
    wrote on last edited by
    #1

    Hi all,

    I am quite new to Qt programming, and I think what I try to achieve is quite simple.

    The application I am porting uses panels (that contain elements such as buttons and labels). Many of the panels are displayed in the center of the screen, and the application can decide to display one panel or another, depending on several actions. Only one panel is visible at any point of time. I thought that I would implement it the following way:

    I would like to define a "placeholder" element in my main QML layout file, let's say:

    MainWindow.qml:
    @Item {
    id: window1
    width: 1366
    height: 768
    objectName: "MainWindow"

    Item {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        width: 490
        height: 445
        objectName: "CenterPanel"
    }
    

    }@

    ... And be able, from the C++ code, to dynamically add (and replace) a child to this container placeholder element each time I want to display a panel. This child would simply be a new component, loaded from a QML file depending on the panel that is required to be displayed:

    PanelA.qml:
    @import QtQuick 2.2
    import QtQuick.Window 2.1
    import QtQuick.Controls 1.2

    Item {
    objectName: "PanelA"
    anchors.fill: parent

    ...
    

    }@

    I expect something like that in my C++ code:

    pPanelA = <LOAD PANEL FROM QML FILE>;
    pCenterPanel->removeChild();
    pCenterPanel->addChild( pPanelA );

    How can I achieve that?

    Thank you.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      Have you looked at this QML component?

      http://qt-project.org/doc/qt-5/qml-qtquick-loader.html

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jrlaff
        wrote on last edited by
        #3

        I saw this one, but how can I change its source from the C++ code? I haven't found any C++ class that matches the Loader QML type...

        1 Reply Last reply
        0
        • T Offline
          T Offline
          t3685
          wrote on last edited by
          #4

          One way is to emit signal from the C++ code and handle the signal in the QML, although that's a bit different than what you're thinking off.

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            Hi,

            If I understood you correctly you will need "qqmlcomponent":http://qt-project.org/doc/qt-5/qqmlcomponent.html to create component from C++ side, then after "beginCreate":http://qt-project.org/doc/qt-5/qqmlcomponent.html#beginCreate cast it to QQuickItem and set it's parent as per your needs.

            157

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jrlaff
              wrote on last edited by
              #6

              [quote author="p3c0" date="1411450523"]Hi,

              If I understood you correctly you will need "qqmlcomponent":http://qt-project.org/doc/qt-5/qqmlcomponent.html to create component from C++ side, then after "beginCreate":http://qt-project.org/doc/qt-5/qqmlcomponent.html#beginCreate cast it to QQuickItem and set it's parent as per your needs.[/quote]

              It seems to be what I was looking for. I just figured out the difference between a parent (in the QObject's parent hierarchy) and a visual parent, and thus use setParentItem().

              Thank you for your help.

              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