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. Is it actually possible to use a C++ QList as a model for a QML PathView?
QtWS25 Last Chance

Is it actually possible to use a C++ QList as a model for a QML PathView?

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

    I've already asked this question on stack overflow, but I thought I'd ask here where the real Qt experts are.

    "http://stackoverflow.com/questions/15753419/qt-qml-data-model-doesnt-seem-to-work-with-c":http://stackoverflow.com/questions/15753419/qt-qml-data-model-doesnt-seem-to-work-with-c

    The executive summary is that if I have a QML ListModel, I can set that as the model for either a QML ListView or a QML PathView. If I have C++ objects in a QList<>, which is what some of the QML data model examples do, I can create a working model for QML ListViews, but not QML PathViews. There seems to be something that ListView has that QList<MyClass> doesn't - some glue that gets PathView working but is missing when I make my QList.

    I'm using Qt 4.8.4 on Windows. Any ideas? Does anyone know of a sample or an example that does this? Something that populates a PathView with a C++ model? Any C++ model, QList-based or QAbstractListModel-based, would be fine.

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

      So I tried out a horrible hack - here it is:

      main.cpp:
      @
      ...
      dataList.append( new DataObject( "c++ entry6 (purple)", "purple" ) );

      QmlApplicationViewer viewer;
      QDeclarativeContext * context = viewer.rootContext();
      context->setContextProperty( "cppModel", QVariant::fromValue(dataList) );
      context->setContextProperty( "cppModelCount", QVariant::fromValue(dataList.count()) );
      viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
      ...
      

      @

      cpptest.qml:

      @
      ...
      interactive: true
      //model: qmlModel
      model: cppModel

          delegate: Rectangle {
              width: 100
              height: 100
              color: colour
              Text {
                  anchors.centerIn: parent
                  text: name
              }
          }
      
      
          path: Path {
              startX: - my_path_view.width * cppModelCount / 2 + my_path_view.width / 2
              startY: my_path_view.height / 2
              PathLine {
                  x: my_path_view.width * cppModelCount / 2 + my_path_view.width / 2
                  y: my_path_view.height / 2
              }
          }
      ...
      

      @

      This actually produced the expected result. But its a terrible hack. Why is this not working properly? I can't believe that this is as-designed - it really does seem to be a Qt bug in the Path object, or the C++ bindings. cppModel.count, according to my debug printouts in the qml, is never available - in fact, the object that cppModel is bound to has no members at all.

      I've build Qt before and I have the source - is there any place that anyone could point me to where I could start debugging this?

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tedmiddleton
        wrote on last edited by
        #3

        I got some help from a couple of experts on IRC (#qt-qml) last night and my example is working now. I've answered the StackOverflow question that I linked to above if anyone is interested.

        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