Results 1 to 6 of 6

Thread: Connecting Qml signals to Qt

  1. #1
    Join Date
    Aug 2010
    Posts
    6
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Connecting Qml signals to Qt

    Hi, I'm trying to use a qml-grid view in my code. I'm trying to couple it with my C++ code. I've dynamically created a list view model and passed across the qml file. It works fine. However, I'm facing trouble when I want to connect a Qml signal to Qt/c++ code. I've handled mouseArea in my Qml-rectangle and emitting a signal from there.

    I'm trying to connect to the signal as follows:

    Qt Code:
    1. QDeclarativeView *pQMLContainer = NULL;
    2. TempWidget *pTemp = new TempWidget();
    3. pQMLContainer = new QDeclarativeView(pTemp);
    4. pQMLContainer->setResizeMode(QDeclarativeView::SizeRootObjectToView);
    5. pQMLContainer->rootContext()->setContextProperty("imgModel", createModel() );
    6. pQMLContainer->setSource(QUrl("..Temp/qml/gridview-example.qml"));
    7. QObject *rootObject = dynamic_cast<QObject*>pQMLContainer->rootObject();
    8. QObject::connect(rootObject, SIGNAL(keyPressed()), pTemp, SLOT(onKeyPressed()));
    To copy to clipboard, switch view to plain text mode 

    When the connect statement runs, I get an error: cannot connect to "null" object. On debugging, I found I could never get "rootObject" as a valid pointer. Where am I going wrong?

    Thanks

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Connecting Qml signals to Qt

    content is not shown, so most probably Qt optimization prevent creation of any object in QDeclarativeView since it is not needed until it is shown.

    Another thing why cast to QObject? It is base class for all QGraphicsObjects.
    And signal is wrong for sure! Where are the arguments?

  3. #3
    Join Date
    Aug 2010
    Posts
    6
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Connecting Qml signals to Qt

    Hi Marek,
    Thanks for your suggesstions.

    However, I tried to show the QDeclarativeView and then call connect. It still gave me the same error.
    Also, if I try to get QGraphicsObject from QDeclarativeView::rootObject, I still get a null pointer.
    The signals and slots do not have any parameters(void).

    Thanks,
    Mots
    Quote Originally Posted by MarekR22 View Post
    content is not shown, so most probably Qt optimization prevent creation of any object in QDeclarativeView since it is not needed until it is shown.

    Another thing why cast to QObject? It is base class for all QGraphicsObjects.
    And signal is wrong for sure! Where are the arguments?

  4. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Connecting Qml signals to Qt

    Take a look at http://doc.qt.nokia.com/latest/qtbinding.html there is section "Receiving signals".
    C++ code there is similar to what you have presented so apparently your QML code must be wrong.

    Edit:
    PS. Isn't your URL wrong "..Temp/qml/gridview-example.qml"? Or is this just mistype in posting this message?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Connecting Qml signals to Qt

    If OP can see his QML UI, then the url must be right, and also it must have a root object...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Aug 2010
    Posts
    6
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Connecting Qml signals to Qt

    @MarekR22: Yes the URL for qml was correct as I could see my grid-view and all its items.

    I found the solution and it now works fine (I'm getting all the signals).
    The change I made was I added the qml to my 'qrc' and gave the path relative to 'qrc'. I'm not sure why it made a difference even though earlier, it could load the 'qml' from the relative path to the binary.

    Thanks everyone for your help!
    Mots

Similar Threads

  1. Connecting QML signals with Qt slots
    By KIBSOFT in forum Qt Quick
    Replies: 1
    Last Post: 15th November 2010, 09:18
  2. Replies: 8
    Last Post: 18th July 2009, 15:57
  3. Connecting signals and slots help pls
    By bod in forum Qt Programming
    Replies: 9
    Last Post: 1st July 2008, 15:01
  4. QHttp signals not connecting...
    By trobbins0000 in forum Newbie
    Replies: 1
    Last Post: 27th February 2007, 02:45
  5. Connecting signals & slots across different threads
    By jyoti kumar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 12:40

Tags for this Thread

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.