diff --git a/src/app/qgsattributesformproperties.cpp b/src/app/qgsattributesformproperties.cpp
index 28bc2ff10a3..c0335a6e1b9 100644
--- a/src/app/qgsattributesformproperties.cpp
+++ b/src/app/qgsattributesformproperties.cpp
@@ -1117,9 +1117,27 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
       layout->addWidget( baseWidget );
 
       QLineEdit *title = new QLineEdit( itemData.name() );
+
+      //qmlCode
       QPlainTextEdit *qmlCode = new QPlainTextEdit( itemData.qmlElementEditorConfiguration().qmlCode );
       qmlCode->setPlaceholderText( tr( "Insert QML code here..." ) );
 
+      QgsQmlWidgetWrapper *qmlWrapper = new QgsQmlWidgetWrapper( mLayer, nullptr, this );
+      qmlWrapper->setQmlCode( qmlCode->toPlainText() );
+      QgsFeature previewFeature;
+      mLayer->getFeatures().nextFeature( previewFeature );
+      qmlWrapper->setFeature( previewFeature );
+      //update preview on text change
+      connect( qmlCode, &QPlainTextEdit::textChanged, this, [ = ]
+      {
+        qmlWrapper->setQmlCode( qmlCode->toPlainText() );
+        qmlWrapper->reinitWidget();
+        QgsFeature previewFeature;
+        mLayer->getFeatures().nextFeature( previewFeature );
+        qmlWrapper->setFeature( previewFeature );
+      } );
+
+      //templates
       QComboBox *qmlObjectTemplate = new QComboBox();
       qmlObjectTemplate->addItem( tr( "Free text..." ) );
       qmlObjectTemplate->addItem( tr( "Rectangle" ) );
@@ -1142,7 +1160,7 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
                                       "    width: 100\n"
                                       "    height: 100\n"
                                       "    color: \"steelblue\"\n"
-                                      "    Text{ text: \"A rectangle\"\n"
+                                      "    Text{ text: \"A rectangle\" }\n"
                                       "}\n" ) );
             break;
           }
@@ -1179,18 +1197,6 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
         qmlCode->insertPlainText( QStringLiteral( "expression.evaluate(\"%1\")" ).arg( expressionWidget->currentText() ) );
       } );
 
-
-      QgsQmlWidgetWrapper *qmlWrapper = new QgsQmlWidgetWrapper( mLayer, nullptr, this );
-      qmlWrapper->setQmlCode( qmlCode->toPlainText() );
-      connect( qmlCode, &QPlainTextEdit::textChanged, this, [ = ]
-      {
-        qmlWrapper->setQmlCode( qmlCode->toPlainText() );
-        qmlWrapper->reinitWidget();
-        QgsFeature f;
-        mLayer->getFeatures().nextFeature( f );
-        qmlWrapper->setFeature( f );
-      } );
-
       layout->addRow( tr( "Title" ), title );
       QGroupBox *qmlCodeBox = new QGroupBox( tr( "QML Code" ) );
       qmlCodeBox->setLayout( new QGridLayout );
diff --git a/src/gui/editorwidgets/qgsqmlwidgetwrapper.cpp b/src/gui/editorwidgets/qgsqmlwidgetwrapper.cpp
index e8164f4e1cb..e203fb88492 100644
--- a/src/gui/editorwidgets/qgsqmlwidgetwrapper.cpp
+++ b/src/gui/editorwidgets/qgsqmlwidgetwrapper.cpp
@@ -61,16 +61,9 @@ void QgsQmlWidgetWrapper::reinitWidget( )
   if ( !mWidget )
     return;
 
-  if ( !mQmlFile.open() )
-  {
-    QgsMessageLog::logMessage( tr( "Failed to open temporary QML file" ) );
-    return;
-  }
-
   mWidget->engine()->clearComponentCache();
-  mWidget->setSource( QUrl::fromLocalFile( mQmlFile.fileName() ) );
 
-  mQmlFile.close();
+  initWidget( mWidget );
 }
 
 
@@ -93,6 +86,9 @@ void QgsQmlWidgetWrapper::setFeature( const QgsFeature &feature )
   if ( mWidget )
   {
     QgsExpressionContext context = layer()->createExpressionContext();
+    context << QgsExpressionContextUtils::globalScope()
+            << QgsExpressionContextUtils::projectScope( QgsProject::instance() )
+            << QgsExpressionContextUtils::layerScope( layer() );
 
     context.setFeature( feature );
 
diff --git a/src/gui/editorwidgets/qgsqmlwidgetwrapper.h b/src/gui/editorwidgets/qgsqmlwidgetwrapper.h
index 63def6bd163..40aca1c3539 100644
--- a/src/gui/editorwidgets/qgsqmlwidgetwrapper.h
+++ b/src/gui/editorwidgets/qgsqmlwidgetwrapper.h
@@ -46,8 +46,6 @@ class GUI_EXPORT QgsQmlWidgetWrapper : public QgsWidgetWrapper
     QTemporaryFile mQmlFile;
 
     QQuickWidget *mWidget = nullptr;
-
-    QgsFeature mFeature;
 };