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. error in qml type converting while qml styling

error in qml type converting while qml styling

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 1 Posters 555 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.
  • Q Offline
    Q Offline
    QtBeginnnnner
    wrote on last edited by
    #1

    first , I found two post and want to try their skinnable idea as follows :

    https://stackoverflow.com/questions/36335481/dynamically-change-qml-theme-at-runtime/36399339#36399339

    https://stackoverflow.com/questions/23518071/qt-5-styling-dynamically-load-qml-files/25866188#25866188

    I almost copied their code , but failed and errors occurs:
    Item {
    id : base
    ...
    property AbstractStyle currentStyle : Style.createStyleObject(base, "qrc:/StyleA.qml");

    Text {
    x: 100
    y: 0
    font.pixelSize: currentStyle.textSize
    color: currentStyle.textColor
    text: "Hello World"
    }
    }
    while code running , it reports error showing "Unable to assign AbstractStyle_QMLTYPE_37 to AbstractStyle_QMLTYPE_0".

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      QtBeginnnnner
      wrote on last edited by
      #2

      here is my code:
      // AbstractStyle.qml
      import QtQuick 2.0
      QtObject {
      property int textSize;
      property color textColor;
      }

      // StyleA.qml
      import QtQuick 2.0
      AbstractStyle {
      textSize : 20
      textColor : "red"
      }

      // StyleB.qml
      import QtQuick 2.0
      AbstractStyle {
      textSize : 50
      textColor : "green"
      }

      //componentCreation.js
      var component;
      var sprite;
      function createStyleObject(item, stylePath)
      {
      component = Qt.createComponent(stylePath);
      if( component.status == Component.Ready ){
      sprite = component.createObject(item);
      console.log("[!!!OK!!!]componentCreation.js:" + stylePath + " component ready");
      }
      else
      console.log("componentCreation.js:" + stylePath + " component not ready");
      if (sprite === null)
      console.log("componentCreation.js: error creating " + stylePath + " object");
      else
      return sprite;
      }

      //main.cpp
      int main(int argc, char *argv[]) {
      QGuiApplication app(argc, argv);

      QQuickView *view = new QQuickView;
      view->setResizeMode(QQuickView::SizeRootObjectToView);
      view->setSource(QUrl(QStringLiteral("qrc:///main.qml")));
      view->show();
      
      return app.exec();
      

      }

      //main.qml
      import "qrc:/styles/componentCreation.js" as Style
      Item {
      id : base
      ...
      property AbstractStyle currentStyle : Style.createStyleObject(base, "qrc:/StyleA.qml");

      Text {
      x: 100
      y: 0
      font.pixelSize: currentStyle.textSize
      color: currentStyle.textColor
      text: "Hello World"
      }
      }

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QtBeginnnnner
        wrote on last edited by QtBeginnnnner
        #3

        well , after some help and tests , it comes to conclusion:

        this works in Qt 5.12.0 and above

        property AbstractStyle currentStyle : Style.createStyleObject(base, "qrc:/StyleA.qml");

        these two works in Qt 5.10.1 and above

        property QtObject currentStyle : Style.createStyleObject(base, "qrc:/StyleA.qml");
        property var currentStyle : Style.createStyleObject(base, "qrc:/StyleA.qml");

        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