Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8032825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:30:42+00:00 2026-06-05T01:30:42+00:00

I know that we can’t use GUI-interaction in non-GUI threads ( QThread ). But

  • 0

I know that we can’t use GUI-interaction in non-GUI threads (QThread).
But I don’t know if we can or can’t interact with model (QAbstractItemModel) in threads and if True then how to do it in the right way?

I honestly searched something about this in Google and on SO and it seems like there are no relevant answers to my question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:30:44+00:00Added an answer on June 5, 2026 at 1:30 am

    What do you mean by “interact with model”? If you mean that you want to access the model from multiple threads by directly manipulating it, then you have to serialize the access to the model. Since there are so many methods within the model, I’d suggest you do not add a mutex within the model — it’d be very tedious and error prone as it’d be too easy to forget a mutex locker. Instead, use the fact that your model inherits QObject, and thus can accept events.

    1. Your gui thread accesses the model directly.
    2. Other threads interact with the model by posting events to it (and perhaps receiving reply events).
    3. The gui thread will process those events serially with any other access, thus protecting your model from concurrent access.

    Other threads can of course receive replies from the model — also via events. You’d have two event base classes: a Request class that is used to request things from the model, and then there’d be a Response event base class that the model would use to reply. The Request class should have a QObject* sender member, so that the model would know what QObject to post the reply event to. You’d probably want both the request and reply to carry identical identifier (say a serially increment int), so that requests and responses could be matched up.

    You must implement all the threaded code that interacts with the model via events not by reimplementing QThread::run(), but within a QObject. After you instantiate the QObject, simply move it to a separate thread. QThread’s default implementation of run() will spin an event loop to keep your QObject executing if there are any events, signals or timers ready for it. A zero-duration timer is a way of keeping a thread permanently busy, but make sure you don’t do too much processing in one go, otherwise you’ll delay processing of incoming events.

    You can also use signals and slots, but you cannot call them directly, you can only:

    1. connect() to them,
    2. invoke them via QMetaObject::invokeMethod with Qt::QueuedConnection.
    3. invoke them through a functor (say, a lambda) executed in the main thread context; see this answer for how to do that.

    Behind the scenes, when you connect signals to slots of QObjects that reside in separate threads, Qt creates a connection that marshals each signal into a QMetaCallEvent, and then unmarshals it in the thread where the QObject with target slot lives.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that I can use cmp, diff, etc to compare two files, but
I know that it's a subject that can raise a lot of debate, but
I know that there can be multiple values for an email, but I'm not
We know that, we can set a permalink structure for wordpress from settings. But,
I know that we can protect .Net applications from decompiling by using obfuscators. But
I know that can co-exists with web form no problem as Hanselman mention here
I want to know that can mouse click event detects colors or even strings.
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
I know that I can get word completion through CTRL+N & CTRL+P and code
I know that this can be easily done by using if(i%5 == 0 OR

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.