【问题标题】:error in qml type converting while qml stylingqml 样式转换时 qml 类型转换错误
【发布时间】:2019-07-13 15:11:23
【问题描述】:

首先,我找到了两个帖子,想试试他们的可换肤想法如下:

我几乎复制了他们的代码,但失败并出现错误:

while code running , it reports error showing "Unable to assign AbstractStyle_QMLTYPE_37 to AbstractStyle_QMLTYPE_0".

这是我的代码:

// 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"
 }
 ...
}

另外:开发环境是windows的vs2015和Ubuntu的qtcreator,都使用Qt 5.10.1

【问题讨论】:

  • 在 Linux 上使用 Qt 5.12.1 我没有发现这个问题
  • 尝试将您的property AbstractStyle currentStyle : ... 行更改为property var currentStyle : ...

标签: qt qml


【解决方案1】:

@eyllanesc @TrebuchetMS 感谢你们俩

嗯,经过一些测试,得出结论:

这适用于 Qt 5.12.0 及更高版本

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

这两个工作在 Qt 5.10.1 及以上版本

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

【讨论】:

  • 看来Qt 5.12在数据类型的识别上有所提升。 :-)
猜你喜欢
  • 2019-10-28
  • 2012-01-08
  • 2022-10-08
  • 1970-01-01
  • 2014-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多