Results 1 to 8 of 8

Thread: Can a QML element be displayed inside a QT C++ GUI?

  1. #1
    Join Date
    Apr 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Can a QML element be displayed inside a QT C++ GUI?

    I have a problem in which I need my application to have in the same window the system API controls and some elements I have found only to be available in QML (a hybrid one could say). So I have read here: http://www.qtforum.org/index.php?pag...qtbinding.html

    But it is creating a new window instance instead of showing the element into the normal C++ window instance. In other words, its creating 2 windows when I use QDeclarativeView.

    Here is my rectangle.qml
    Qt Code:
    1. import Qt 4.7
    2.  
    3.  
    4.  
    5. Rectangle {
    6. width: 100
    7. height: 62
    8. radius: 13
    9. gradient: Gradient {
    10. GradientStop {
    11. position: 0
    12. color: "#95eac2"
    13. }
    14.  
    15. GradientStop {
    16. position: 0.5
    17. color: "#4bb0ee"
    18. }
    19.  
    20. GradientStop {
    21. position: 0.97
    22. color: "#7ce0d5"
    23. }
    24.  
    25. GradientStop {
    26. position: 1
    27. color: "#ffffff"
    28. }
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    and the code I added to the main.cpp code to display the rectangle:
    Qt Code:
    1. QDeclarativeView view;
    2.  
    3. view.setSource(QUrl::fromLocalFile("qrc:/rectangle.qml"));
    4. view.show();
    To copy to clipboard, switch view to plain text mode 

    I was wondering if it was possible to insert some sort of frame inside the .ui design that allowed me to later display the qml element inside the .ui interface. In the attached image is the idea of what I want to do. Does anyone know?
    Attached Images Attached Images

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Can a QML element be displayed inside a QT C++ GUI?

    The answer to your titular question is, "Yes". I know one way to go about dropping QML into a ui, but I don't know if this helps you:

    Qt Code:
    1. // inside custom QGraphicsView class...
    2.  
    3. ...
    4.  
    5. QGraphicsObject *qmlThing = getQMLGraphicsObjectFromURL(QUrl("pathToYourURL"));
    6. myScene->addItem(qmlThing);
    7.  
    8. ...
    9.  
    10. QGraphicsObject * MyGraphicsView::getQMLGraphicsObjectFromURL(const QUrl &url) {
    11. QDeclarativeComponent component(new QDeclarativeEngine(), url);
    12. QObject *object = component.create();
    13. return qobject_cast<QGraphicsObject *>(object);
    14. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can a QML element be displayed inside a QT C++ GUI?

    I am not sure what you are asking. You can drag and drop a QDeclarativeView from the "Display Widgets" group into your Designer UI and set its source at your leisure.

  4. #4
    Join Date
    Apr 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can a QML element be displayed inside a QT C++ GUI?

    Quote Originally Posted by ChrisW67 View Post
    I am not sure what you are asking. You can drag and drop a QDeclarativeView from the "Display Widgets" group into your Designer UI and set its source at your leisure.
    That is exactly what I want to do , but I don't see a QDeclarativeView in "Display Widgets". I only see a "Graphics View" and "QWebView", unless you are meaning it can be done with a Graphics View.

    @Urthas

    Thanks. I'll be honest, I don't really know what to do with the code you wrote, meaning I don't know where the class should go in order for me to access it on the main.cpp or how to display the item. I suppose I'll have to store some coordinates in a variable and with some existing function make it display on the spot I want in the UI. Though, I don't know how to do that. I'm limited on time right now because of the studies, but as soon as I finish the current batch of material, I'll take a more calm look at it because I really want to do this.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can a QML element be displayed inside a QT C++ GUI?

    Quote Originally Posted by StealthCentroid View Post
    That is exactly what I want to do , but I don't see a QDeclarativeView in "Display Widgets". I only see a "Graphics View" and "QWebView", unless you are meaning it can be done with a Graphics View.
    No, I mean what I said. I just checked that I wasn't imagining the QDeclarativeView in Designer (Qt 4.7.4 on Linux) but in my recent Windows Qt SDK it is not present.

  6. #6
    Join Date
    Apr 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can a QML element be displayed inside a QT C++ GUI?

    Quote Originally Posted by ChrisW67 View Post
    No, I mean what I said. I just checked that I wasn't imagining the QDeclarativeView in Designer (Qt 4.7.4 on Linux) but in my recent Windows Qt SDK it is not present.
    Perfect then. I'll check it on Linux. The program I want to do is on Linux, but before moving I was testing in Windows because the only code that uses other libraries that are not from QT are available in Linux and Windows. Now that I think about it, it doesn't make much sense to do it that way, but I'm a noob after all and felt more comfortable working with QT Creator in a system I'm more familiar with (you know, until I get the hang of it). Thanks for lining that out, I'll tell you how it goes when I check it.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can a QML element be displayed inside a QT C++ GUI?

    I am surprised it is missing in the Windows Qt SDK given the headlong rush to QML everything. There are QML components for Designer in the SDK but they seem to be only Symbian.

  8. #8
    Join Date
    Apr 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can a QML element be displayed inside a QT C++ GUI?

    Quote Originally Posted by ChrisW67 View Post
    I am surprised it is missing in the Windows Qt SDK given the headlong rush to QML everything. There are QML components for Designer in the SDK but they seem to be only Symbian.
    Yeah, there are others too that are on Linux, but not in Widnows.

    It works on Linux perfectly, look at the attachment. You choose File->New File or Project->Qt Widget Project->QT Gui Application

    Once its created you must edit the .pro file and add under SOURCES:
    QT += declarative

    This step is important, if you don't do it, you won't be able to compile the application.

    Something to note is that QDeclarativeView is not available in Windows by default. In Windows you can insert a Graphics View (under Display Widgets) and after adding the above to the .pro file you can right click it and promote it to QDeclarativeView. However, there won't be a way to specify its source on the designer and from what I've tried it doesn't work either through code.

    Only way I found around this is to make the project on linux and then open it in Windows and even though, you won't see the QDeclarativeView preview on the designer. You will just see a white component in the screen. Anyway, this time you will be able to set its source on the designer on the property segment of the screen all the way to the end. It says source in bold.
    Attached Images Attached Images
    Last edited by StealthCentroid; 15th May 2012 at 04:42.

Similar Threads

  1. Replies: 9
    Last Post: 23rd April 2012, 13:53
  2. Get element in Qgridlayout
    By dieter in forum Newbie
    Replies: 1
    Last Post: 12th February 2012, 09:51
  3. Remove element of xml
    By VitaliBR in forum Newbie
    Replies: 17
    Last Post: 16th February 2011, 13:28
  4. Help with QML Pathview element
    By chetu1984 in forum Newbie
    Replies: 0
    Last Post: 7th February 2011, 04:49
  5. Replies: 1
    Last Post: 23rd January 2010, 13:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.