Results 1 to 4 of 4

Thread: QML image not refreshing when evoked in separate method

  1. #1
    Join Date
    Dec 2018
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default QML image not refreshing when evoked in separate method

    Im tring to update a qml Image URL via signal:

    This is working perfectly fine when executed in this method:

    Qt Code:
    1. void Sentinel::startQmlEngine()
    2. {
    3. QQmlApplicationEngine qmlEngine;
    4.  
    5. qmlRegisterType<ImageItem>("ImageItem",1,0,"ImageItem");
    6. qmlEngine.rootContext()->setContextProperty("imageUrlClass", &image);
    7.  
    8. QQmlComponent component(&qmlEngine, "qrc:/main.qml");
    9. object = component.create();
    10. assert(object);
    11.  
    12. image.setUrl(getPathToImage());
    13. }
    To copy to clipboard, switch view to plain text mode 

    As soon as I outsource setting the URL the signal is not emitted

    Qt Code:
    1. void Sentinel::updateGui()
    2. {
    3. image.setUrl(getPathToImage());
    4. }
    To copy to clipboard, switch view to plain text mode 

    How can this be?

    Here my Qml

    Qt Code:
    1. import QtQuick 2.12
    2. import QtQuick.Window 2.12
    3. import ImageItem 1.0
    4.  
    5.  
    6. Rectangle {
    7.  
    8. width: 768
    9. height: 576
    10.  
    11. Connections {
    12. target: imageUrlClass
    13. onUrlChanged: {
    14. image.update();
    15. console.log("image UURL-" + imageUrlClass.imageUrl);
    16. }
    17. }
    18.  
    19. Image {
    20. id: image
    21. objectName: "image"
    22. x: 0
    23. y: 0
    24. width: 768
    25. height: 576
    26. source: imageUrlClass.imageUrl
    27. opacity: 1
    28. clip: true
    29. fillMode: Image.PreserveAspectFit
    30. cache: false
    31. }
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

    ImageItem class header:

    Qt Code:
    1. #pragma once
    2.  
    3. #include <QQuickPaintedItem>
    4.  
    5. //---------------------------------------------------------------------------
    6.  
    7.  
    8. class ImageItem: public QQuickPaintedItem
    9. {
    10. Q_OBJECT
    11. Q_PROPERTY(QVariant imageUrl READ getUrl() WRITE setUrl NOTIFY urlChanged)
    12.  
    13. public:
    14.  
    15. explicit ImageItem(QQuickItem *parent = nullptr);
    16.  
    17. virtual void paint(QPainter* painter);
    18.  
    19. QVariant getUrl() const;
    20. void setUrl(const QVariant &value);
    21.  
    22. signals:
    23.  
    24. void urlChanged();
    25.  
    26. private:
    27. QVariant url;
    28.  
    29. };
    To copy to clipboard, switch view to plain text mode 

    And Cpp:

    Qt Code:
    1. #include "ImageItem.h"
    2.  
    3. //---------------------------------------------------------------------------
    4.  
    5. ImageItem::ImageItem(QQuickItem *parent) :
    6. QQuickPaintedItem(parent)
    7. {
    8. }
    9.  
    10. QVariant ImageItem::getUrl() const
    11. {
    12. return url;
    13. }
    14.  
    15. void ImageItem::setUrl(const QVariant &value)
    16. {
    17. url = value;
    18. emit urlChanged();
    19. }
    20.  
    21. void ImageItem::paint(QPainter* painter)
    22. {
    23. paint(painter);
    24. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,969
    Thanks
    232
    Thanked 627 Times in 617 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QML image not refreshing when evoked in separate method

    In startQmlEngine(), you declare an instance of QQmlApplicationEngine as a local variable inside the method. As soon as the method ends, this local variable goes out of scope and is destroyed. Any components created using that engine instance are also destroyed, and any signal / slot connections made are automatically disconnected.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Dec 2018
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QML image not refreshing when evoked in separate method

    Quote Originally Posted by d_stranz View Post
    In startQmlEngine(), you declare an instance of QQmlApplicationEngine as a local variable inside the method. As soon as the method ends, this local variable goes out of scope and is destroyed. Any components created using that engine instance are also destroyed, and any signal / slot connections made are automatically disconnected.
    Thanks you are genius.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,284
    Thanks
    36
    Thanked 1,519 Times in 1,470 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML image not refreshing when evoked in separate method

    Just some additional observation which might not be applicable depending on how your final code will look like:

    * you don't need to derive from QQuickItem if you are only using the object for transporting data via properties
    * if the property is a URL then the C++ type for this would be QUrl
    * you only need qmlRegisterType() and the import if you are instantiating the type from within QML or if the type provides enums that you want to access by name

    Cheers,
    _

Similar Threads

  1. Replies: 6
    Last Post: 22nd May 2011, 09:52
  2. Replies: 0
    Last Post: 31st December 2010, 13:08
  3. Replies: 1
    Last Post: 25th November 2010, 11:37
  4. Put in a separate thread a method of a class
    By franco.amato in forum Qt Programming
    Replies: 0
    Last Post: 19th March 2010, 23:45
  5. Calling same method from separate threads
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 19th July 2007, 08:55

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.