Results 1 to 8 of 8

Thread: Qml in QGraphicsWidget

  1. #1

    Default Qml in QGraphicsWidget

    Is it possible to insert QML file into QGraphicsWidget? (Eg. insert QML file into Layout?)

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qml in QGraphicsWidget

    Yes, fetch the object created by QDeclarativeComponent and do whatever you want with it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3

    Default Re: Qml in QGraphicsWidget

    I've tried something like this:
    Qt Code:
    1. QDeclarativeEngine *engine = new QDeclarativeEngine;
    2. QDeclarativeComponent component(engine, QUrl("qrc:/qml/main.qml"));
    3. QGraphicsLayoutItem *object =qobject_cast<QGraphicsLayoutItem *>(component.create());
    4. QGraphicsLinearLayout* layout = new QGraphicsLinearLayout();
    5. layout->addItem(object);
    To copy to clipboard, switch view to plain text mode 

    But I'm getting:
    QGraphicsLinearLayout::insertItem: cannot insert null item
    When I cast it to QGraphicsObject I'm getting not null object, but I can't add this to layout.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qml in QGraphicsWidget

    You need to place the object in a QGraphicsWidget and insert that graphics widget into the layout.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5

    Default Re: Qml in QGraphicsWidget

    It started working, but I have another issue. My qml file code is:
    Qt Code:
    1. import QtQuick 1.0
    2. Rectangle {
    3. id: page
    4. width: 500; height: 200
    5. color: "lightgray"
    6.  
    7. Text {
    8. id: helloText
    9. text: "Hello world!"
    10. y: 30
    11. anchors.horizontalCenter: page.horizontalCenter
    12. font.pointSize: 24; font.bold: true
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    And when I load this file, put in QGraphicsWidget, it only renders rectangle without text inside.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qml in QGraphicsWidget

    Does it work if you show the qml file in a separate view?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7

    Default Re: Qml in QGraphicsWidget

    Probably I'm doing something wrong with constructing QGraphicsWidget from QGraphicsItem.
    When I do this:

    Qt Code:
    1. QDeclarativeEngine *engine = new QDeclarativeEngine();
    2. QDeclarativeComponent component(engine, QUrl("qrc:/main.qml"));
    3. QObject *myObject = component.create();
    4.  
    5. QGraphicsObject* graphicsObject =qobject_cast<QGraphicsObject *>(myObject);
    6. scene.addItem(graphicsObject);
    To copy to clipboard, switch view to plain text mode 

    Everything is ok, but when I'm trying to do it in this way (I need to add qml content to QGraphicsWidget):

    main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include <QTextEdit>
    3. #include <QGraphicsWidget>
    4. #include <QGraphicsLinearLayout>
    5. #include <QGraphicsView>
    6. #include "testqml.h"
    7.  
    8. int main(int argc, char* argv[])
    9. {
    10. QApplication a(argc, argv);
    11.  
    12. view.setScene(&scene);
    13.  
    14. QGraphicsWidget mainWidget;
    15.  
    16. QGraphicsLinearLayout layout(Qt::Vertical);
    17.  
    18. TestQML* tkuml = new TestQML();
    19. layout.addItem(tkuml);
    20.  
    21. mainWidget.setLayout(&layout);
    22.  
    23. scene.addItem(&mainWidget);
    24. view.show();
    25.  
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    testqml.h
    Qt Code:
    1. #include <QDeclarativeEngine>
    2. #include <QDeclarativeComponent>
    3. #include <QGraphicsWidget>
    4.  
    5. class TestQML : public QGraphicsWidget
    6. {
    7. Q_OBJECT
    8. QGraphicsObject* graphicsObject;
    9. public:
    10. TestQML()
    11. {
    12. QDeclarativeEngine *engine = new QDeclarativeEngine(this);
    13. QDeclarativeComponent component(engine, QUrl("qrc:/main.qml"));
    14. QObject *myObject = component.create();
    15.  
    16. graphicsObject =qobject_cast<QGraphicsObject *>(myObject);
    17. }
    18.  
    19. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    20. graphicsObject->paint(painter, option, widget);
    21. }
    22.  
    23. QRectF boundingRect() {
    24. return graphicsObject->boundingRect();
    25. }
    26. };
    To copy to clipboard, switch view to plain text mode 

    It only renders gray rectangle, but without any content. (QML file in above post, stored in resource file).

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qml in QGraphicsWidget

    No, it definitely won't work this way. Instead you should set the qml-originated graphics object as a child of QGraphicsWidget (you don't need to subclass it) and put that widget in a layout.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Positioning QGraphicsWidget
    By jasper_ferrer in forum Qt Programming
    Replies: 3
    Last Post: 22nd September 2009, 15:34
  2. grabMouse() on QGraphicsWidget()
    By wagmare in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2009, 08:27
  3. QGraphicsWidget - How does it work?
    By been_1990 in forum Qt Programming
    Replies: 2
    Last Post: 31st July 2009, 14:15
  4. QGraphicsWidget vs. QGraphicsRectItem
    By Bill in forum Newbie
    Replies: 3
    Last Post: 27th July 2009, 10:02
  5. QwebPage within a QGraphicsWidget
    By Osprey in forum Qt Programming
    Replies: 0
    Last Post: 16th June 2009, 18:32

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.