13

My setup is Qt5.0.2 MinGW 32-bit.

I am looking for { Qt5.0.2 / QML / QtQuick2.0 / C++ } code project examples (not Qt Quick 1.0) that have actual C++ classes or at least a main.cpp .

I ran through the web, browsed all examples I could possibly find:

but they were either old (Qt Quick 1.0) or pure QML+ QtQuick 2.0 or did not compile at all; or if they could compile, they showed me empty dialogs (for example, the "Status Shout!" in the Nokia code examples).

Any advice would be highly appreciated.

iviv
  • 161
  • 1
  • 1
  • 7
  • I tested some of the examples and some of them work. Not all, but Qt is a little slow in this regard, both the documentation and the examples on QtQuick2 are still incomplete. Keep in mind QuQuick1 is almost entirely source compatible with QtQuick2, so the old resources are perfectly good for learning. – dtech Apr 17 '13 at 20:01
  • Could you please recommend examples that you have found useful? I need a complete project that sets and gets properties and data from QML, hopefully something that is Qt Quick 2.0 compatible. – iviv Apr 17 '13 at 20:14
  • BTW those code examples from nokia are for symbian, they require different components, so no wonder they don't work for you. – dtech Apr 17 '13 at 21:13
  • oh, I see :) I really do not need Symbian – iviv Apr 17 '13 at 21:40

2 Answers2

16

There aren't really all that much detailed resources on QML, most of what is available is just short snippet examples and documentation examples. This is a problem for people who are new to programming, because such materials don't really give an idea of how to put together something that is useful in practice.

This is true even more so for QtQuick2, which is brand new, and even the documentation and many of the official examples are still incomplete. And I know from experience how frustrating it is to follow a tutorial, type everything, expect it to work, and get something unexpected, with no idea what really went wrong and how to fix it.

That being said, there are a few examples of complete, albeit trivial games, that are implemented in QtQuick1. This is not that big of an issue since QtQuick2 elements are backward compatible and the code will work with QtQuick2 with little to no modifications at all.

The official examples, while occasionally broken or incomplete, can also be of help, plus they will likely be fixed soon (it's about time):

Last but not least, QML snippets from the Qt project website wiki:

EDIT: To add another good resource for learning QML: http://qmlbook.org

dtech
  • 47,916
  • 17
  • 112
  • 190
  • Alas, still no complete project with the files I neeed. But from what I am getting here as answers, it seems that there are really no sample projects that match my question { Qt5.0.2 / QML / QtQuick2.0 / C++ } , (not .JS code!) with setting and getting a property – iviv Apr 18 '13 at 13:03
2

A rather minimal example would be:

main.cpp

#include <QGuiApplication>
#include <QQuickView>

int main(int argc, char** argv)
{
    QGuiApplication app(argc, argv);
    QQuickView view;
    view.resize(800, 480);
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    view.setSource(QUrl("qrc:///foo.qml"));
    view.show();
    return app.exec();
}

foo.qml (here bundled as resource):

import QtQuick 2.0

Rectangle {
    color: "lightsteelblue"

    Text {
        anchors.centerIn: parent
        text: "Hello"
    }
}
Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • 1
    The OP said he is looking for project examples, not hello world... – dtech Apr 17 '13 at 20:21
  • Can this part ' QQuickView view; view.resize(800, 480); view.setResizeMode(QQuickView::SizeRootObjectToView); view.setSource(QUrl("qrc:///foo.qml")); view.show();' be moved to a class outside main.cpp? I tried your minimalistic code example but when I move it to a class "Dialog"(inherits QObject) and call it in its constructor, the dialog does not show up at all (or appears and immediately disappears, I cannot tell for sure/no errors show up). – iviv Apr 17 '13 at 20:22
  • 1
    ddriver: To me the request was anything that actually compiles/shows something – Frank Osterfeld Apr 17 '13 at 20:28
  • iviv: In principle you can move it, but then you can't create the view object on the stack, because it would be destroyed right away when leaving the constructor (or other method the view is created in). Creation of widgets on the stack only works in this case, because everything in main() lives as long as the application does. – Frank Osterfeld Apr 17 '13 at 20:30
  • @iviv - moving the code in the constructor of a dialog? Why would you put a QQuickView inside a widget? Sounds like you need to take a step back and start at the very beginning. – dtech Apr 17 '13 at 20:32
  • @ddriver, I just need the code to be in a class outside of main.cpp. From what you are saying, it looks like I am doing it completely wrong. But for some reason, I have done this successfully in a class Dialog which inherits QDialog like this 'view = new QDeclarativeView(); view->rootContext()->setContextProperty("Dialog", this); view->setSource(QUrl("qml/Kiosk/QTKiosk.qml")); view->setResizeMode(QDeclarativeView::SizeRootObjectToView);' – iviv Apr 17 '13 at 21:50
  • @ddriver, I was also thinking that it gets created and then destroyed immediately, however, when I debugged the code right after the view.show(); , the dialog did not show up at all. – iviv Apr 17 '13 at 21:50
  • @iviv - the main function is where you create your QApplication, or QCoreApplication, and in the same manner, it is where you create your QQuickView. Everyone is doing it this way. Naturally, you could do stuff like put QML in a widget, put widgets in QML, but those are rare cases. – dtech Apr 17 '13 at 21:53
  • I want to separate the UI code and put it into a thread, this is why I want it to be outside of the main function. – iviv Apr 17 '13 at 22:06
  • @iviv UI code must execute in the main thread. If you want to do multi-threading, it should be your other processing that gets put into a different thread. – cgmb Apr 17 '13 at 22:58
  • @iviv - you can just as well start a new thread in the main function, and as Slavik81 said, in Qt the UI is always the main thread. If you want heavy calculations, then you create a worker thread that you manage from the main thread, so it can be free and not lock up and block the UI. – dtech Apr 17 '13 at 23:41
  • Thank you, @ddriver and Slavik81 ! Very helpful advices indeed – iviv Apr 18 '13 at 06:28
  • Thanks. This example made it for me. – Zane Jan 18 '14 at 21:56