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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:27:05+00:00 2026-06-03T07:27:05+00:00

I have a QThread defined in MyThread.cpp and .h. There I have a private

  • 0

I have a QThread defined in MyThread.cpp and .h. There I have a private QTcpSocket.

class MyThread: public QThread {

    Q_OBJECT

public:
    MyThread();
    virtual ~MyThread();

public slots:
    void reconnect();
signals:

protected:
    void run(void);

private:
    QTcpSocket *socket;
};

void MyThread::run(void) {
    bool connected = false;

    connected = prepareSocket(QString::fromLatin1((Global::directionIPSerialServer).toLocal8Bit().data()), 8008, socket);
}

On the other hand, I have a set of functions defined in Functions.cpp and .h, which are not defined into a Functions class, but a namespace.

bool Functions::prepareSocket(QString address, int port, QTcpSocket *socket) {
    socket->connectToHost(address, port);
    if(!socket->waitForConnected(Global::maxTimueToConnectByTCP)){
        qDebug()<<"Error: "<<socket->errorString();
        return false;
    }
    return true;
}

Then, correct me if I’m wrong, when I call a function into Functions to connect the MyThread’s QTcpSocket to a host, I’m supposing I am on the same thread, since I’m calling the function from the thread I’ve created and not a different one.

Despite this, on socket->connectToHost I am getting QObject: Cannot create children for a parent that is in a different thread. twice.

Why is that? What’s wrong?

  • 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-03T07:27:07+00:00Added an answer on June 3, 2026 at 7:27 am

    Try: QObject::moveToThread. This should do the trick.

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

Sidebar

Related Questions

I have a class like this class GUI : public QWidget, public QThread When
I have a QThread derived class that communicates with the main thread by sending
I have an object that is derived from QThread and the class definition includes
I'm working with QThread and slots/signals mechanism; I know there's a lot of threads
Can a class inherited from QThread and having run method can have other methods
I have this resource allocator class public class ResourceAllocator { ArrayList<Request> queue = new
I have a QThread that contains a QUDPsocket (socket is member not local to
Have you ever obfuscated your code before? Are there ever legitimate reasons to do
I have a C++/Qt QThread worker thread, which stores its current status as an
I have a QMainWindow with three widgets inside that are promoted to a class

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.