Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt thread connect to qml
QtWS25 Last Chance

Qt thread connect to qml

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goli
    wrote on last edited by
    #1

    I have a main program that create a QDeclarativeView for qml file, and also create a thread.
    i want that the qml file will send signal to start the thread running. this is what i do:
    @
    QObject::connect(view.rootObject(),SIGNAL(startThread()),myThread,SLOT(start()));
    @

    this is working, but this thread need to change a context property in the view when he is running.
    when I do this the program end with memory error.

    @
    void MyThread::run()
    {
    m_view->rootContext()->setContextProperty("newProperty", "blabla");
    }
    @

    I init the m_viewto be pointer to the view in the thread constructor, and I don't know why this failed.

    any idea??

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      It is not allowed to access UI things inside a thread. Your thread must emit a signal which is handled in the main thread then.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goli
        wrote on last edited by
        #3

        But when i wait for a while before the initialization in the thread it works...

        @void MyThread::run()
        {
        int i=0;
        while(i++<10000000000000);
        m_view->rootContext()->setContextProperty("newProperty", "blabla");
        }@

        ??

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          If it works, it's just by pure luck and happenstance. As Gerolf said, you can't access UI things inside of a thread.

          By when they say you can't, it means that while there is nothing that keeps you from trying to access things from your thread, the underlying system is not designed to handle those accesses properly and the results are undefined. That means that in certain situations things could appear to work, but it's not expected, proper, or guaranteed to be repeatable.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goli
            wrote on last edited by
            #5

            Is this valid also when the property i initialize in the qml from the thread,
            (m_view->rootContext()->setContextProperty("newProperty", "blabla");
            ) is used in other qml file , and not from the current qml file that running?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              If you're directly accessing m_view from anywhere other than the main thread, it's incorrect and invalid.

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved