Results 1 to 6 of 6

Thread: Using QQuickView in separate thread problems

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

    Default Using QQuickView in separate thread problems

    Hi,

    I'm trying to display qml file with help of QQuickView running in separate thread. My platform is beaglebone (am33xx qt5.5 compiled by yocto project with opengl).

    I debug problem a bit and only after disabling of line 523 (in qtbase/src/gui/kernel/sources/qwindow.cpp)

    d->platformWindow->setVisible(visible);

    it was possible to use showFullScreen() in QquickView. The other show() function still doesn't work (means that the execution of the program is frozen after this place in code). The showFullScreen() is working restricted, it can show simple qml file, but doesn't show animation. There are some warnings in log file like :

    WARNING: QObject::moveToThread: Current thread (0x6ff08) is not the object's thread (0x112740). Cannot move to target thread (0x156560)
    WARNING: Updates can only be scheduled from GUI thread or from QquickItem::updatePaintNode()

    When don't use separate thread and put everything in main.cpp then everything works as expected (including animations). Any ideas what could be wrong in my implementation? Thanks.

    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 

    mythread.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. engine = new QQmlApplicationEngine();
    11. window = NULL;
    12. }
    13.  
    14. void MyThread::run() {
    15.  
    16. qDebug() << "thread() " << currentThread();
    17.  
    18. m_view->setSource(QUrl(QStringLiteral("qrc:/test/loadingView.qml")));
    19. m_view->show();
    20.  
    21. exec();
    22. }
    23.  
    24. void MyThread::setWindow(QQuickWindow* ptr) {
    25. if (ptr)
    26. window = ptr;
    27. else {
    28. window = new QQuickWindow();
    29. }
    30. }
    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. void setWindow(QQuickWindow* ptr);
    18.  
    19. private:
    20.  
    21. QQmlApplicationEngine *engine;
    22. QQuickWindow *window;
    23. QQuickView *m_view;
    24. QObject *topLevel;
    25.  
    26. };
    27.  
    28. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    qml file:
    Qt Code:
    1. import QtQuick 2.0
    2. import QtQuick.Window 2.2
    3.  
    4. Window {
    5. property var value: 0
    6. property string colGreyVeryLight: "#cccccc"
    7. property string colGreyLight: "#999999"
    8. property string colGrey: "#444444"
    9. property string colGreyDark: "#262a2b"
    10. property var timerIntervalTime: 250
    11.  
    12. id: screen
    13. width: 800
    14. height: 480
    15. color: "black"
    16. objectName: "list"
    17.  
    18. MouseArea {
    19. id: mouseArea
    20. width: 480
    21. height: 360
    22. anchors.rightMargin: 0
    23. anchors.bottomMargin: -497
    24. anchors.leftMargin: 8
    25. anchors.topMargin: 497
    26. anchors.fill: parent
    27. }
    28.  
    29. Text {
    30. id: loadingText
    31. x: 300
    32. y: 67
    33. width: 180
    34. height: 30
    35. color: "#ffffff"
    36. text: qsTr("Loading...")
    37. font.bold: true
    38. verticalAlignment: Text.AlignVCenter
    39. horizontalAlignment: Text.AlignHCenter
    40. font.pointSize: 14
    41. font.pixelSize: 14
    42. }
    43.  
    44. Timer {
    45. interval: timerIntervalTime; running: true; repeat: true
    46. onTriggered: (value == 4) ? value = 0 : value++
    47. }
    48.  
    49. Rectangle {
    50. id: rect1
    51. x: 65
    52. y: 192
    53. width: 50
    54. height: 50
    55. color: (value == 0) ? colGreyVeryLight : ((value == 1) ? colGreyLight : (value == 2) ? colGrey : colGreyDark)
    56. }
    57.  
    58. Rectangle {
    59. id: rect2
    60. x: 203
    61. y: 192
    62. width: 50
    63. height: 50
    64. color: (value == 1) ? colGreyVeryLight : ((value == 2) ? colGreyLight : (value == 3) ? colGrey : colGreyDark)
    65. }
    66.  
    67. Rectangle {
    68. id: rect3
    69. x: 354
    70. y: 192
    71. width: 50
    72. height: 50
    73. color: (value == 2) ? colGreyVeryLight : ((value == 3) ? colGreyLight : (value == 4) ? colGrey : colGreyDark)
    74. }
    75.  
    76. Rectangle {
    77. id: rect4
    78. x: 511
    79. y: 192
    80. width: 50
    81. height: 50
    82. color: (value == 3 ) ? colGreyVeryLight : ((value == 4) ? colGreyLight : colGreyDark)
    83. }
    84.  
    85. Rectangle {
    86. id: rect5
    87. x: 674
    88. y: 192
    89. width: 50
    90. height: 50
    91. color: (value == 4 ) ? colGreyVeryLight : colGreyDark
    92. }
    93.  
    94. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Using QQuickView in separate thread problems

    GUI, like QtQuick or QtWidgets, can only be used by the thread running the QApplication, i.e. usually that's the main thread.

    That's ok in your code, as the QQuickView is generated by the main thread, but then you try to call the view's methods from the secondary thread.

    If you want the secondary thread to trigger these functions, connect signals to these slots and emit them from the secondary thread.
    The connect() mechanism will then take care of calling the slots in the thread of the receiver object, which here is the main thread and this safe.

    Cheers,
    _

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

    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

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

    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,
    _

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

    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 

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.