Results 1 to 6 of 6

Thread: Using QQuickView in separate thread problems

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2009
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Using QQuickView in separate thread problems

    Hi,

    thanks for hint. I did update code accordingly and it start working:

    Qt Code:
    1. MyThread::MyThread() {
    2. m_view = new QQuickView();
    3. connect(this, SIGNAL(show()), m_view, SLOT(show()));
    4. connect(this, SIGNAL(qmlSetSource(QUrl)), m_view, SLOT(setSource(QUrl)));
    5. }
    6.  
    7. void MyThread::run() {
    8.  
    9. qDebug() << "thread() " << currentThread();
    10. emit qmlSetSource(QUrl(QStringLiteral("qrc:/test/loadingView.qml")));
    11. emit show();
    12. exec();
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class MyThread : public QThread {
    2.  
    3. Q_OBJECT
    4. public:
    5. MyThread();
    6.  
    7. public slots:
    8. void run();
    9.  
    10. private:
    11.  
    12. QQuickView *m_view;
    13.  
    14. signals:
    15. void show();
    16. void qmlSetSource(QUrl url);
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 

    One thing which remain is warning printed in console when run example:

    13:26:30.192 WARNING: QObject::setParent: Cannot set parent, new parent is in a different thread
    13:26:30.416 WARNING: Updates can only be scheduled from GUI thread or from QQuickItem::updatePaintNode()

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Using QQuickView in separate thread problems

    Might be caused by some other code that you haven't posted yet.

    The first one for example could be caused by creating an instance of a QObject derived class in run() and passing "this" as the parent.

    Cheers,
    _

  3. #3
    Join Date
    May 2009
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Using QQuickView in separate thread problems

    Attaching full source (qml remains unchanged):

    main.cpp
    Qt Code:
    1. #include <iostream>
    2. #include <QGuiApplication>
    3. #include <QQuickView>
    4. #include "mythread.h"
    5. #include <qdebug.h>
    6.  
    7. using namespace std;
    8.  
    9. int main(int argc, char ** argv)
    10. {
    11. QGuiApplication app(argc, argv);
    12. qDebug() << "app.thread() " << QGuiApplication::instance()->thread();
    13.  
    14. MyThread thread;
    15. thread.start();
    16.  
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    mytheread.cpp
    Qt Code:
    1. #include "mythread.h"
    2. #include <QDebug>
    3. #include <QString>
    4. #include <QMutexLocker>
    5. #include <QTime>
    6.  
    7.  
    8. MyThread::MyThread() {
    9. m_view = new QQuickView();
    10. connect(this, SIGNAL(show()), m_view, SLOT(show()));
    11. connect(this, SIGNAL(qmlSetSource(QUrl)), m_view, SLOT(setSource(QUrl)));
    12. }
    13.  
    14. void MyThread::run() {
    15.  
    16. qDebug() << "thread() " << currentThread();
    17. emit qmlSetSource(QUrl(QStringLiteral("qrc:/test/loadingView.qml")));
    18. emit show();
    19. exec();
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

    mythread.h
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QMutex>
    6. #include <QQuickView>
    7. #include <QQmlApplicationEngine>
    8.  
    9. class MyThread : public QThread {
    10.  
    11. Q_OBJECT
    12. public:
    13. MyThread();
    14.  
    15. public slots:
    16. void run();
    17.  
    18. private:
    19.  
    20. QQuickView *m_view;
    21.  
    22. signals:
    23. void show();
    24. void qmlSetSource(QUrl url);
    25.  
    26. };
    27.  
    28. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Using QQuickView in separate thread problems

    Works for me.

    I copied the QML content from the thread start and modified it to use a Rectangle as the top level element as QQuickView expects an Item.
    I also change the URL to the local file.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 7th June 2015, 08:06
  2. -how to run a function in separate thread
    By shivendra46d in forum Newbie
    Replies: 22
    Last Post: 28th October 2013, 06:20
  3. How to Have a class run in a separate thread.
    By sona1111 in forum Newbie
    Replies: 7
    Last Post: 29th August 2013, 06:44
  4. GUI Updates from separate Thread
    By sa5webber in forum Newbie
    Replies: 5
    Last Post: 16th June 2012, 20:08
  5. new QWidget in separate thread
    By magland in forum Qt Programming
    Replies: 15
    Last Post: 7th February 2008, 12: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
  •  
Qt is a trademark of The Qt Company.