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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:56:53+00:00 2026-06-16T05:56:53+00:00

The mainwindow.cpp: #include ui_mainwindow.h #include <QtCore> /* ****** Thread part ****** */ myThread::myThread(QObject *parent)

  • 0

The mainwindow.cpp:

#include "ui_mainwindow.h"
#include <QtCore>

/* ****** Thread part ****** */
myThread::myThread(QObject *parent)
    : QThread(parent)
{
}

void myThread::run()
{
    while(1){
        qDebug("thread one----------");
        emit threadSignal1();
        usleep(100000);
    }
    exec();
}

myThread2::myThread2(QObject *parent)
    : QThread(parent)
{
}

void myThread2::run()
{
    while(1){
        qDebug("thread two");
        emit threadSignal2();
        usleep(100000);
    }
    exec();
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    onethread = new myThread(this);
    onethread->start(QThread::NormalPriority);

    twothread = new myThread2(this);
    twothread->start(QThread::NormalPriority);

    connect(onethread, SIGNAL(onethread->threadSignal1()),
            this, SLOT(mySlot1()));
    connect(twothread, SIGNAL(threadSignal2()),
            this, SLOT(mySlot2()),Qt::QueuedConnection);
}

void MainWindow::mySlot1()
{
    ui->textEdit1->append("This is thread1");
}

void MainWindow::mySlot2()
{
    ui->textEdit1->append("This is thread2");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    ui->textEdit1->append("textEdit1");
    ui->textEdit2->append("textEdit2");
}

The mainwindow.h:

#define MAINWINDOW_H

#include <QMainWindow>
#include <QThread>

namespace Ui {
class MainWindow;
}

class myThread : public QThread
{
  Q_OBJECT

public:
    myThread(QObject *parent = 0);
    void run();

signals:
    void threadSignal1();
};

class myThread2 : public QThread
{
  Q_OBJECT

public:
    myThread2(QObject *parent = 0);
    void run();

signals:
    void threadSignal2();
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void mySlot1();
    void mySlot2();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    myThread *onethread;
    myThread2 *twothread;
};

#endif // MAINWINDOW_H

Please check the above code. It can give the qDebug output normally while the textEdit1/2 have no any output. And it seems to be a multi-thread signal/slot connect issue. Who can help? Thanks!

  • 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-16T05:56:54+00:00Added an answer on June 16, 2026 at 5:56 am

    You need to define the slots as slots:

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    public slots:
        void mySlot1();
        void mySlot2();
    ...
    

    Also a few other things. You don’t need the exec() call in each of the threads. They are going into an endless loop anyways and would never reach that statement. And, the purpose of exec() is to start an event loop in the thread so that it can receive and process events, like having its own slots. You are just emitting.

    Are you sure this connection works?

    connect(onethread, SIGNAL(onethread->threadSignal1()),
            this, SLOT(mySlot1()));
    

    It should probably be:

    connect(onethread, SIGNAL(threadSignal1()),
            this, SLOT(mySlot1()));
    

    I believe QueuedConnection will be implied for the connections because they are targeting a slot on an owner in a different thread than the emitter.

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

Sidebar

Related Questions

public MainWindow() { CommandManager.AddExecutedHandler(this, ExecuteHandler); } void ExecuteHandler(object sender, ExecutedRoutedEventArgs e) { } Error
I have the very simple following code: main.cpp #include ui_library_browser.h #include <QtGui/QApplication> #include StartWindow.h
Here is my code for my MainWindow : MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QSqlDatabase
If I define the function in mainwindow.cpp the function works, but when I define
I have a MainWindow application I'm working on to learn C++ and Qt (C++
I have MainWindow with a button, under the button click event I want MainWindow
I want to give the name of MainWindow.xib file in plist file through code,
I am using a QStackedWidget on my mainWindow. The firstPageWidget on stackedWidget contains 3
I have the following dependency property in my MainWindow class (inherits from WPF's Window)
I have a view that is loaded in the MainWindow.xib. It is just a

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.