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. Showing items to qml from .cpp
Qt 6.11 is out! See what's new in the release blog

Showing items to qml from .cpp

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

    i would like to show an item from my cpp to qml, and editing the width of the item but it only shows a black screen,

    here is my code

    @
    //main.cpp
    #include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include <QDeclarativeEngine>
    #include <QDeclarativeComponent>
    #include <QDeclarativeProperty>
    #include <QDebug>
    #include <QObject>
    #include <QGraphicsObject>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QDeclarativeView view;
    view.setSource(QUrl::fromLocalFile&#40;"qml1.qml"&#41;);
    view.show();
    QObject *object = view.rootObject();
    object->setProperty("width", 500);
    QDeclarativeProperty(object, "width").write(500);
    
    return app.exec();
    

    }
    @

    @
    //qml1.qml
    import QtQuick 1.0

    Rectangle{
    id: object
    width: 360
    height: 360
    color: "#d73c3c"
    Item {
    x: 46
    y: 51
    width: 254
    height: 100
    anchors.rightMargin: 60
    anchors.bottomMargin: 72
    anchors.leftMargin: 46
    anchors.topMargin: 51
    anchors.fill: parent
    }
    }
    @

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cmer4
      wrote on last edited by
      #2

      "CPP 2 QML Bindings":http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html

      I think you need to take a closer look at the doc above to make it work...

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chuck Gao
        wrote on last edited by
        #3

        Notice QObject is not a widget. Have a look at "QML C++ Extensions":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeexamples.html#c-extensions if you want to create a widget in C++ side

        Chuck

        1 Reply Last reply
        0
        • G Offline
          G Offline
          guoqing
          wrote on last edited by
          #4

          is the qml1.qml file in the same folder with your executable ?

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jr_jags
            wrote on last edited by
            #5

            yes they were in the same folder

            1 Reply Last reply
            0
            • C Offline
              C Offline
              changsheng230
              wrote on last edited by
              #6

              I copied your code, and run in my Qt simulator, showing a red rectangle. Your code should be all right
              [quote author="jr_jags" date="1311644078"]yes they were in the same folder[/quote]

              Chang Sheng
              常升

              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