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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:06:07+00:00 2026-06-16T20:06:07+00:00

EDIT2 Here is the solution that works for the following problem of giving a

  • 0

EDIT2

Here is the solution that works for the following problem of giving a QThread exclusively to the object.

I’ve changed the approach for the problem. I don’t want to close QThread in MyClass anymore, cause the following solution seems easier and not too bad looking.

My solution is modification of the solution given here: http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

The problem with that solution was that QObject worker wasn’t really deleted (checked it).

QThread *myThread = new QThread();
SCIntermediary* myObj = new MyClass();
myObj->moveToThread(myThread);
connect(myThread, SIGNAL(started()), myObj, SLOT(setup()));
connect(myObj, SIGNAL(destroyed()), myThread, SLOT(quit()), Qt::DirectConnection);
connect(myObj, SIGNAL(finished()), myObj, SLOT(deleteLater()));
connect(myThread, SIGNAL(finished()), myThread, SLOT(deleteLater()));
myThread -> start();

// do your work bla bla bla

myObj -> finishIt(); // this function only emits finish() signal of myObj
myThread -> wait();

This is the first solution that worked for me destroying both myObj and myThread without any errors or other troubles at all.

ENDOF EDIT

I’m trying to create a class will do some stuff between my client and my server. I’d like it to have it’s own thread. So what I did is:

class Myclass : public QObject {
    Q_OBJECT
public:
    Myclass();
    ~Myclass();

private:
    QThread *my_thread;
    QTcpSocket *sock;
}

Here is how I coded my constructor:

Myclass::Myclass(){
    my_thread = new QThread();
    my_thread -> start();
    moveToThread(my_thread);

    sock = new QTcpSocket(this);
    sock -> connectToHost("host", port);
}

This didn’t work. It didn’t work, because the code for TcpSocket wasn’t executed in the same thread the parent object currently is. So what I decided to do is to create slot and signal for setup and emit it in my constructor. Here is how my code looks right now.

class Myclass : public QObject {
    Q_OBJECT
public:
    Myclass();
    ~Myclass();

public slots:
    void setup();

signals:
    void do_setup();

private:
    QThread *my_thread;
    QTcpSocket *sock;
}

And some of it’s implementation

Myclass::Myclass(){
    my_thread = new QThread();
    my_thread -> start();
    moveToThread(my_thread);

    connect(this, SIGNAL(do_setup()), this, SLOT(setup()));
    emit do_setup();
}

void Myclass::setup(){
    sock = new QTcpSocket();
    sock -> connectToHost("host", port);
}

Now it works and here comes the sad part – it looks terrible! It’s awful and I have no idea how to make it look better or how such pattern should be done in the first place. What would you advice me to do to get the same effect?

Additionally – I have no idea how to write good destructor for this – how to delete QThread smoothly and all the class objects..

EDIT

For now I believe the solution is quite alright – the only problem left is how to write a destructor for that. I have no idea how it can be done here.

  • 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-16T20:06:07+00:00Added an answer on June 16, 2026 at 8:06 pm

    For destructor, check out Maya Posch example:
    http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
    Since your thread is not child of MyClass, perhaps you can call finish() on thread as last thing in destructor and connect destroyed() signal of MyClass to deleteLater for thread?

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

Sidebar

Related Questions

I am having a simple problem that I cannot find a simple solution to.
I'm looking to analyze and compare the following `signals': (Edit: better renderings here: oscillations
EDIT: See this in action here: http://jsbin.com/emobi/5 -- and that's using mouseenter/mouseleave. I have
Short version of Problem Autocomplete works when the input string matches the result string,
i have the following problem. I have to parse a json request into an
I have already looked at this question but there was no solution. Here is
SOLUTION BELOW - Not really the problem I thought it was. I'm adding data
I feel my problem is quite obvious and has trivial solution, but I can't
I have a small problem which I can't seem to find a solution to.
I'm faced with the following problem in rails. I have a form to edit/create

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.