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

  • Home
  • SEARCH
  • 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 3626838
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:48:46+00:00 2026-05-18T23:48:46+00:00

I have the main (GUI) thread which creates a QThread. In the QThread, I

  • 0

I have the main (GUI) thread which creates a QThread.

In the QThread, I am calling a C function that needs to display a QMessageBox. So far, I simply used:

void notify(char *str)
{
  QMessageBox::information(0, "", QString(str));
}

in the C++ code and called it from the C code. This worked fine without threads, but now with threads I am getting errors, because one cannot call the GUI functions from another thread.

Usually, this could be circumvented by using signals like the answer to this question suggests; however, I doubt I can do this from the C code.

So, how can I make the C code communicate with the GUI thread and tell it to display a QMessageBox?

Thanks.

P.S.

If possible, I’d like to do this without touching the C code (as of now, there is simply an extern void notify(char *) declaration in the C code’s headers, and if possible I’d like it to stay at that.

  • 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-18T23:48:47+00:00Added an answer on May 18, 2026 at 11:48 pm

    Assuming you have a QWidget or QMainWindow derived class for your GUI, you could add the following to it:

    class MyWidget : public QWidget
    {
        Q_OBJECT;
    public:    
        MyWidget()
        {
            connect(this, SIGNAL(notify_sig(QString)),
                    this, SLOT(notify_slot(QString)),
                    Qt::QueuedConnection);
        }
    
        void notify(QString str)
        {
            emit notify_sig(str);
        }
    
        signals:
            void notify_sig(QString str);
    
        slots:
            void notify_slot(QString str)
            {
                QMessageBox::information(0, "", str);
            }
    };
    

    Here you have a public function notify() that is a member of the widget class. Calling MyWidget::notify() results in a signal being sent to itself through a queued connection (which will result in the slot being called in the GUI thread). Now the C notify() call just needs to call the widget/window’s notify() function. This can be tricky since you don’t really have a pointer to the widget available in the C notify() function.

    Normally, the C interface would allow the user to pass a void* value in and would then return that value with the notify call. This would allow you to pass in the pointer to MyWidget when the C function is called and then cast it back to MyWidget in the notify() implementation.

    MyWidget* wid = ...;
    C_function(arg1, ..., wid);
    
    //...
    
    void notify(char* str, void* userdata)
    {
        MyWidget* wid = static_cast<MyWidget*>(userdata);
        wid->notify(QString(str));
    }
    

    If you can’t change the C interface, you may need to use some kind of global way of getting the widget/window’s pointer.

    Note that I have not tested any of this code and there may be an easier way to do this.

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

Sidebar

Related Questions

I have my main GUI thread, and a second thread running inside it's own
My Goal I would like to have a main processing thread (non GUI), and
If you have worked with gui toolkits, you know that there is a event-loop/main-loop
I have a Python/wxPython program where the GUI is the main thread and I
I have a Qt application that has two threads: the main thread that handles
i have a table called category in which i have main category ids and
I have a main asp.net app, which is written in asp.net 1.1. Runnning underneath
I have a main window (#1) on my webpage from which I open a
I have a main table that I must get data from. I have a
I have a main TCL proc that sources tons of other tcl procs in

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.