add quick item to quick view dynamically

This commit is contained in:
王劲鹏 2021-02-19 15:43:09 +08:00 committed by osborn
parent df8b98bc6c
commit 7ced952a57
15 changed files with 97 additions and 20 deletions

View File

@ -14,17 +14,32 @@ void DoricDemoBridge::navigate(QVariant route) {
QString script = DoricUtils::readAssetFile("/doric/bundles", name);
QQuickView *view = new QQuickView();
view->setWidth(450);
view->setHeight(800);
QColor blue("blue");
view->setColor(blue);
view->show();
{
const QUrl url(QStringLiteral("qrc:/doric/qml/view.qml"));
view->setSource(url);
view->setWidth(450);
view->setHeight(800);
}
DoricPanel *panel = new DoricPanel();
panel->setParent(view);
panel->setParentItem(view->rootObject());
panel->setWidth(450);
panel->setHeight(800);
panel->config(script, name, NULL);
QQmlEngine *engine = view->engine();
QQmlComponent component(engine);
const QUrl empty(QStringLiteral("qrc:/doric/qml/panel.qml"));
component.loadUrl(empty);
QQuickItem *childItem = qobject_cast<QQuickItem *>(component.create());
if (childItem == nullptr) {
qCritical() << component.errorString();
return;
}
childItem->setParentItem(view->rootObject());
view->show();
break;
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.14.0, 2021-02-19T10:48:54. -->
<!-- Written by QtCreator 4.14.0, 2021-02-19T15:04:39. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>

View File

@ -1,13 +1,19 @@
#include "DoricShaderPlugin.h"
#include <QDebug>
#include "../shader/DoricRootNode.h"
#include "DoricShaderPlugin.h"
void DoricShaderPlugin::render(QJSValue jsValue, QString callbackId) {
qDebug() << getContext();
getContext()->getDriver()->asyncCall(
[this, jsValue] {
QString viewId = jsValue.property("id").toString();
getContext()->getDriver();
DoricRootNode *rootNode = getContext()->getRootNode();
if (rootNode->getId().isEmpty() && jsValue.property("type").toString() == "Root") {
rootNode->setId(viewId);
rootNode->blend(jsValue.property("props"));
}
},
DoricThreadMode::UI);
}

View File

@ -1,8 +1,7 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>qtquickcontrols2.conf</file>
<file>stack.qml</file>
<file alias="main.qml">./resources/main.qml</file>
<file alias="qtquickcontrols2.conf">./resources/qtquickcontrols2.conf</file>
</qresource>
<qresource prefix="/doric">
<file alias="doric-sandbox.js">../../doric-js/bundle/doric-sandbox.js</file>
@ -14,4 +13,9 @@
<file alias="Snake.js">../../doric-demo/bundle/src/Snake.js</file>
<file alias="Snake.es5.js">../../doric-demo/bundle/src/Snake.es5.js</file>
</qresource>
<qresource prefix="/doric/qml">
<file alias="view.qml">./resources/view.qml</file>
<file alias="panel.qml">./resources/panel.qml</file>
<file alias="stack.qml">./resources/stack.qml</file>
</qresource>
</RCC>

View File

@ -0,0 +1,8 @@
import QtQuick 2.12
import QtQuick.Controls 2.5
Rectangle {
width: 100
height: 100
color: 'red'
}

View File

@ -0,0 +1,6 @@
import QtQuick 2.12
import QtQuick.Controls 2.5
StackView {
}

View File

@ -3,7 +3,7 @@
QQuickItem *DoricStackNode::build() {
QQmlComponent component;
const QUrl url(QStringLiteral("qrc:/stack.qml"));
const QUrl url(QStringLiteral("qrc:/doric/qml/stack.qml"));
component.loadUrl(url);
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());

View File

@ -3,14 +3,13 @@
#include "DoricGroupNode.h"
class DoricStackNode : public DoricGroupNode
{
class DoricStackNode : public DoricGroupNode {
public:
using DoricGroupNode::DoricGroupNode;
using DoricGroupNode::DoricGroupNode;
QQuickItem *build() override;
QQuickItem *build() override;
void blendLayoutConfig() override;
void blendLayoutConfig() override;
};
#endif // DORICSTACKNODE_H

View File

@ -4,11 +4,14 @@
#include "DoricViewNode.h"
class DoricSuperNode : public DoricViewNode {
protected:
void blendSubLayoutConfig(DoricViewNode *viewNode);
public:
using DoricViewNode::DoricViewNode;
bool mReusable = false;
};
#endif // DORICSUPERNODE_H

View File

@ -1,5 +1,22 @@
#include "DoricViewNode.h"
#include "../utils/DoricUtils.h"
#include "DoricSuperNode.h"
void DoricViewNode::blendLayoutConfig(){
void DoricViewNode::blendLayoutConfig() {}
void DoricViewNode::setLayoutConfig(QJSValue layoutConfig) {}
void DoricViewNode::init(DoricSuperNode *superNode) {
if (DoricUtils:: instanceof <DoricSuperNode *>(this)) {
DoricSuperNode *thiz = dynamic_cast<DoricSuperNode *>(this);
thiz->mReusable = superNode->mReusable;
}
this->mSuperNode = superNode;
this->mView = build();
}
QString DoricViewNode::getId() { return mId; }
void DoricViewNode::setId(QString id) { mId = id; }
void DoricViewNode::blend(QJSValue jsValue) { qDebug() << jsValue.toString(); }

View File

@ -5,20 +5,34 @@
#include "../utils/DoricContextHolder.h"
class DoricSuperNode;
class DoricViewNode : public DoricContextHolder {
protected:
QQuickItem *mView;
DoricSuperNode *mSuperNode;
virtual QQuickItem *build() = 0;
virtual void blendLayoutConfig();
void setLayoutConfig(QJSValue layoutConfig);
private:
QString mId;
QString mType;
public:
using DoricContextHolder::DoricContextHolder;
void init(DoricSuperNode *superNode);
QString getId();
void setId(QString id);
void blend(QJSValue jsValue);
};
#endif // DORICVIEWNODE_H

View File

@ -20,6 +20,11 @@ public:
return content;
}
template <typename Base, typename T>
static inline bool instanceof (const T *) {
return std::is_base_of<Base, T>::value;
}
};
#endif // UTILS_H