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); QString script = DoricUtils::readAssetFile("/doric/bundles", name);
QQuickView *view = new QQuickView(); QQuickView *view = new QQuickView();
view->setWidth(450); {
view->setHeight(800); const QUrl url(QStringLiteral("qrc:/doric/qml/view.qml"));
QColor blue("blue"); view->setSource(url);
view->setColor(blue); view->setWidth(450);
view->show(); view->setHeight(800);
}
DoricPanel *panel = new DoricPanel(); DoricPanel *panel = new DoricPanel();
panel->setParent(view); panel->setParentItem(view->rootObject());
panel->setWidth(450); panel->setWidth(450);
panel->setHeight(800); panel->setHeight(800);
panel->config(script, name, NULL); 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; break;
} }
} }

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!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> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

View File

@ -1,13 +1,19 @@
#include "DoricShaderPlugin.h"
#include <QDebug> #include <QDebug>
#include "../shader/DoricRootNode.h"
#include "DoricShaderPlugin.h"
void DoricShaderPlugin::render(QJSValue jsValue, QString callbackId) { void DoricShaderPlugin::render(QJSValue jsValue, QString callbackId) {
qDebug() << getContext(); qDebug() << getContext();
getContext()->getDriver()->asyncCall( getContext()->getDriver()->asyncCall(
[this, jsValue] { [this, jsValue] {
QString viewId = jsValue.property("id").toString(); 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); DoricThreadMode::UI);
} }

View File

@ -1,8 +1,7 @@
<RCC> <RCC>
<qresource prefix="/"> <qresource prefix="/">
<file>main.qml</file> <file alias="main.qml">./resources/main.qml</file>
<file>qtquickcontrols2.conf</file> <file alias="qtquickcontrols2.conf">./resources/qtquickcontrols2.conf</file>
<file>stack.qml</file>
</qresource> </qresource>
<qresource prefix="/doric"> <qresource prefix="/doric">
<file alias="doric-sandbox.js">../../doric-js/bundle/doric-sandbox.js</file> <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.js">../../doric-demo/bundle/src/Snake.js</file>
<file alias="Snake.es5.js">../../doric-demo/bundle/src/Snake.es5.js</file> <file alias="Snake.es5.js">../../doric-demo/bundle/src/Snake.es5.js</file>
</qresource> </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> </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() { QQuickItem *DoricStackNode::build() {
QQmlComponent component; QQmlComponent component;
const QUrl url(QStringLiteral("qrc:/stack.qml")); const QUrl url(QStringLiteral("qrc:/doric/qml/stack.qml"));
component.loadUrl(url); component.loadUrl(url);
QQuickItem *item = qobject_cast<QQuickItem *>(component.create()); QQuickItem *item = qobject_cast<QQuickItem *>(component.create());

View File

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

View File

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

View File

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

View File

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