Create model QML Dialog from C++
-
I have a simple QML window with a table view in it.
... QQmlApplicationEngine engine; PropertyTable propertyTable(&engine); engine.rootContext()->setContextProperty("propertyModel", propertyTable.getModel()); engine.load(QUrl("qrc:/main.qml")); QList<QObject*> temp = engine.rootObjects(); QObject *topLevel = temp.value(0); QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel); QObject *tableView = window->findChild<QObject*>("tableView1"); QObject::connect(tableView, SIGNAL(rowDoubleClicked(int)), &propertyTable, SLOT(onRowDoubleClicked(int))); if ( !window ) { qWarning("Error: Your root item has to be a Window."); return -1; } // Display the main.qml window->show(); ...
This works so far. The window is shown and i get the doubleclick events in my PropertyTable class.
But now i would like to start a modal QML dialog (for editing the table cells, show extended information and so on) from my C++ PropertyTable class.Can someone give me an advice how this is possible?
Is there also a smaller way to show the main window instead of all the code above? -
I have a simple QML window with a table view in it.
... QQmlApplicationEngine engine; PropertyTable propertyTable(&engine); engine.rootContext()->setContextProperty("propertyModel", propertyTable.getModel()); engine.load(QUrl("qrc:/main.qml")); QList<QObject*> temp = engine.rootObjects(); QObject *topLevel = temp.value(0); QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel); QObject *tableView = window->findChild<QObject*>("tableView1"); QObject::connect(tableView, SIGNAL(rowDoubleClicked(int)), &propertyTable, SLOT(onRowDoubleClicked(int))); if ( !window ) { qWarning("Error: Your root item has to be a Window."); return -1; } // Display the main.qml window->show(); ...
This works so far. The window is shown and i get the doubleclick events in my PropertyTable class.
But now i would like to start a modal QML dialog (for editing the table cells, show extended information and so on) from my C++ PropertyTable class.Can someone give me an advice how this is possible?
Is there also a smaller way to show the main window instead of all the code above?Hi @PhTe
- Create a new QML file which will contain the modal
Dialog
. - Use QQmlComponent to load that QML file
- Specify a parent to it using
setParentItem
- Create a new QML file which will contain the modal
-
Hi @PhTe
- Create a new QML file which will contain the modal
Dialog
. - Use QQmlComponent to load that QML file
- Specify a parent to it using
setParentItem
Hi @p3c0
Create a new QML file which will contain the modal Dialog.
OK, i created a simple dialog:
import QtQuick 2.0 import QtQuick.Dialogs 1.2 Dialog { title: "Test dialog" visible: true; contentItem: Rectangle { id: inputDialog width: 320 height: 128 color: "#cdcdcd" radius: 10 border.width: 1 TextInput { id: valueInput x: 29 y: 30 width: 266 height: 20 text: qsTr("") z: 1 font.pixelSize: 12 } } }
Use QQmlComponent to load that QML file
I tried:
QQmlComponent comp(engine, QUrl("qrc:/PropertiesComponentInputDialog.qml"),QQmlComponent::PreferSynchronous); if(comp.isReady()) { QQuickView *view = qobject_cast<QQuickView*>(comp.create()); QQmlEngine::setObjectOwnership(view, QQmlEngine::CppOwnership); view->setParent((QQuickWindow*)engine->rootObjects().value(0)); view->show(); } else { qDebug() << "Dialog not loaded"; }
and some other variations, but i get a segmentation fault if i call setParent.
Do you have an example code of how i can load and show an QQmlComponent as window/dialog? That would really, really help me.
- Create a new QML file which will contain the modal
-
Hi @p3c0
Create a new QML file which will contain the modal Dialog.
OK, i created a simple dialog:
import QtQuick 2.0 import QtQuick.Dialogs 1.2 Dialog { title: "Test dialog" visible: true; contentItem: Rectangle { id: inputDialog width: 320 height: 128 color: "#cdcdcd" radius: 10 border.width: 1 TextInput { id: valueInput x: 29 y: 30 width: 266 height: 20 text: qsTr("") z: 1 font.pixelSize: 12 } } }
Use QQmlComponent to load that QML file
I tried:
QQmlComponent comp(engine, QUrl("qrc:/PropertiesComponentInputDialog.qml"),QQmlComponent::PreferSynchronous); if(comp.isReady()) { QQuickView *view = qobject_cast<QQuickView*>(comp.create()); QQmlEngine::setObjectOwnership(view, QQmlEngine::CppOwnership); view->setParent((QQuickWindow*)engine->rootObjects().value(0)); view->show(); } else { qDebug() << "Dialog not loaded"; }
and some other variations, but i get a segmentation fault if i call setParent.
Do you have an example code of how i can load and show an QQmlComponent as window/dialog? That would really, really help me.
@PhTe
QQuickView *view = qobject_cast<QQuickView*>(comp.create());
Cast it to
QObject
instead. Setmodality
toQt.ApplicationModal
instead of defaultQt.WindowModal
for theDialog
. It doesn’t work on some platforms.If it is crashing at
setParent
then first check whether the casting works and that object is really accessible. -
@PhTe
QQuickView *view = qobject_cast<QQuickView*>(comp.create());
Cast it to
QObject
instead. Setmodality
toQt.ApplicationModal
instead of defaultQt.WindowModal
for theDialog
. It doesn’t work on some platforms.If it is crashing at
setParent
then first check whether the casting works and that object is really accessible. -
@p3c0
If i cast it to QObject, how can i show the dialog? QObject has no function called show or similiar.@PhTe I think you are misunderstanding the concept.
Dialog
is not a QML viewer its just a component. Following example might help:QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QQuickWindow *itm = qobject_cast<QQuickWindow*>(engine.rootObjects().value(0)); QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/MyDialog.qml"))); QObject *childItem = qobject_cast<QObject*>(component.create()); childItem->setParent(itm);
As soon as it is
create
'ed it is shown.
MyDialog.qml
is same as your example. But it hasmodality
set toQt.ApplicationModal
main.qml
contains just the defaultWindow
component. -
Oh, ok. Now i get it :)
It's simpler then i thought.As soon as it is create'ed it is shown.
I didn't know that.
Thank you