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 646831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:39:20+00:00 2026-05-13T21:39:20+00:00

I have a background thread and the thread calls some methods that update the

  • 0

I have a background thread and the thread calls some methods that update the UI (in order to show progress bars and show additional info in text areas).

If I modify some UI widget values, a “Cannot send events to objects owned by a different thread” assertion error is raised.

Looking at forums, I read that I could use QMetaObject::invokeMethod method but it just works if I pass it the Qt::DirectConnection flag that actually raises the same error shown above.

If I use Qt::QueuedConnection or Qt::AutoConnection, the invokeMethod returns false.

My code looks similar to this:

.h:

class A : public QMainWindow
{
  Q_OBJECT

  QProgressBar* pb;

  public slots:
    bool m(bool, int);
};

class B
{
  A* a;

  public:
    void handleEvent();
};


.cpp:

bool A::m(bool x, int y)
{
  pb->setValue(y);
  return x;
}

void B::handleEvent()
{
  //a->m(true, 12); //raises an assertion error

  bool r;
  //bool ret = QMetaObject::invokeMethod(a, "m", Qt::DirectConnection, Q_RETURN_ARG(bool, r), Q_ARG(bool, true), Q_ARG(int, 12)); //raises the same assertion error error

  bool ret = QMetaObject::invokeMethod(a, "m", Qt::AutoConnection, Q_RETURN_ARG(bool, r), Q_ARG(bool, true), Q_ARG(int, 12)); //is ignored and ret contains false.
}

Do you know what is going on or what i am doing wrong? or maybe, can someone suggest me another approach to deal with my newbie problem?

Thanks in advance,

Ernesto

  • 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-05-13T21:39:21+00:00Added an answer on May 13, 2026 at 9:39 pm

    I haven’t used invokeMethod() myself, but to do this, I usually just use signals and slots. For instance, you could create a signal as a member of class B that is connected to the slot in class A that updates the progress:

    class B : public QObject
    {
        Q_OBJECT
    
        A* a;
    
    signals:
        void update_signal(bool, int);
    
    public:
        void handleEvent();
    };
    
    B::B()
    {
        //assuming a already points to the correct place...
        connect(this, SIGNAL(update_signal(bool,int), 
                a, SLOT(m(bool,int)), Qt::QueuedConnection);
    }
    
    void B::handleEvent()
    {
        emit update_signal(true, 12);
    }
    

    A::m() will have to return void in this case but that is not a problem because when using a queued connection, you cannot get a return value anyway since the call is asynchronous (emit update_signal(true,12) may return before the slot function is called making it impossible to have a return value ready).

    You can actually perform this connection anywhere as long as you have pointers to an object of type A and an object of type B. This makes the signals and slots very flexible since you could completely decouple A from B, yet still allow them to communicate through signals and slots. For instance:

    class B : public QObject
    {
        Q_OBJECT
    
    signals:
        void update_signal(bool, int);
    
    public:
        void handleEvent();
    };
    
    void B::handleEvent()
    {
        emit update_signal(true, 12);
    }
    
    class A : public QMainWindow
    {
        Q_OBJECT
    
        QProgressBar* pb;
    
        public slots:
            void m(bool, int);
    };
    
    void A::m(bool x, int y)
    {
        pb->setValue(y);
    }
    
    int main()
    {
        A* a = new A();
        B* b = new B();
    
        QObject::connect(b, SIGNAL(update_signal(bool, int)),
                a, SLOT(m(bool, int)), Qt::QueuedConnection);
    
        //...
    }
    

    In this case, b does not have to store a pointer or know anything about a yet they can communicate through a thin well-defined channel.

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

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Similarly to any Linux machine, you have different devices for… May 14, 2026 at 5:07 pm
  • Editorial Team
    Editorial Team added an answer if (Pattern.matches(".*[éèàù].*", input)) { .... } add whatever accents you… May 14, 2026 at 5:07 pm
  • Editorial Team
    Editorial Team added an answer Normally information like this will be found in one of… May 14, 2026 at 5:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.