[SOLVED]integrate QML into existing C++
-
Every tutorial (and all my google searchs) on using QML - demo's using QML from the start. I have an existing QT project with lot's of UI files (actually not my code). I want to be able to code a QML dialog and and then from a c++ menu display it. I'm trying to learn - how to use QML. Below is my code that I added to main.cpp - but it opens a second window (not part of my mainwindow) and does not display correctly - I get a blank white window.
Can someone please explain how to integrate QML into an exist C++ with lots of forms already?
void showTest()
{
QQmlEngine engine;
QQmlContext *context = new QQmlContext(engine.rootContext());QQuickView view(QUrl::fromLocalFile("guiclient/test.qml")); QQmlComponent component(&engine, QUrl::fromLocalFile("/u/Download/postbooks_source/qt-client/guiclient/test.qml"));
/*if (component.status() == QQmlComponent::Ready) {
QObject object = component.create(context);
//object->show();
}
else
qDebug(component.errorString().toLatin1());/view.show();
// removing below prevents the window from opening.
QEventLoop loop;
loop.exec();
return;
} -
It looks like this is not going to work at least not in 5.1.
http://qt-project.org/forums/viewthread/23190Thanks for those who viewed
Johnf
-
Hi,
That functionality was added in Qt 5.1: http://blog.qt.digia.com/blog/2013/02/19/introducing-qwidgetcreatewindowcontainer/
-
I have attempted to use the example but I must be missing something!
I'm guessing that the "widgetLayout->" is the parent?? Since other things did not work.
Could you provide a sample of a function that really works. BTW others are suggesting that it can't be done until 5.2. I know I'm asking a lot but I was about to give up on Qt until I saw your post. I'm still not to sure it works? No I'm not saying it's BS I just don't understand it.
-
Where did you see that suggestion? It can definitely be done in Qt 5.1 -- I'm doing it in a project of mine.
widgetLayout is a QLayout in the parent QWidget. You cannot add a child widget directly into the parent widget; you need to add it to the parent's layout. See the "Layout Management":http://qt-project.org/doc/qt-5.1/qtwidgets/layout.html page for details.
Here's how I did it:
@
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
// Integrate the UI file into C++
setupUi(this);// Create the QML view QQuickView* quickView = new QQuickView(QUrl("MyScene.qml")); // Make the QML view resize when the parent is resized quickView->setResizeMode(QQuickView::SizeRootObjectToView); // Add the QML view to my widget layout (centralVBoxLayout), // which I added in Qt Designer QWidget* quickWidget = QWidget::createWindowContainer(quickView); this->centralVBoxLayout->addWidget(quickWidget);
}
@ -
That seems to work - thanks.
BTW - I got the suggestion from two (2) people (one on Qt private IRC) and the other from a friend trying to do something similar.
As a noob I'm learning at a very high curve (I haven't programmed in C++ in 20 years) and the tutorials do not cover what I'm trying to do (that is integrate QML into an exist C++ Qt program).
-
Please don't be discouraged; we all start as newbies but we all improve with practice :)
There are many tutorials/examples about integrating QML into a C++ program. I think one of the issues is that there's too many tutorials, so it's hard for a newcomer to know what to read! This is probably a good place to start for you: https://qt-project.org/doc/qt-5.1/qtqml/qtqml-cppintegration-interactqmlfromcpp.html
If you have any more questions about a specific task you want to do, feel free to make a new post and someone will point you in the right direction.