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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:16:13+00:00 2026-06-12T23:16:13+00:00

So new to Qt. Read the wiki and C++ Gui programming book and they

  • 0

So new to Qt. Read the wiki and C++ Gui programming book and they say subclass QThread. Found that this is not the recommended way now.

So I have some practice code here and I have some questions about if this is correct. So I would really appreciate someone having a look please.

So I created a class with the QThread as a private member to use for the movetothread. When creating it, I made sure not to specify a parent. Therefore my first question is, is this ok to do?

Second question comes from the m_thread->quit(); I found that my connect to finished wasn’t being emitted until I did this. So is this the correct way? I read http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ and saw that the connect between finished and deleteLater in the same thread but not 100% sure if this should be used with quit.

Finally, with the talk of deleteLater, does this mean I dont need
delete m_thread

Thanks for anyones time.

Code here. Simple QDialog with a pushbutton.
worker.cpp

#include "worker.h"

worker::worker(QObject *parent) :
    QObject(parent)
{
    stopped = false;
}

void worker::setupAndRun()
{
    m_thread = new QThread();
    connect(m_thread,SIGNAL(started()),this,SLOT(doWork()));
    connect(m_thread,SIGNAL(finished()),this,SLOT(onComplete()));
    connect(m_thread,SIGNAL(finished()),m_thread,SLOT(deleteLater()));
    this->moveToThread(m_thread);
    m_thread->start();
}

void worker::doWork()
{

    for(int i = 0; i < 20000; i++)
    {
        if (this->stopped)
            break;
        qDebug() << i << " : " <<  Q_FUNC_INFO << m_thread->currentThreadId();
    }
    // --- I think the quit calls the finished signal?
    m_thread->quit();

}

void worker::onComplete()
{
    qDebug() <<  Q_FUNC_INFO << "Called " << m_thread->currentThreadId();
}

worker.h

#ifndef WORKER_H
#define WORKER_H

#include <QObject>
#include <QDebug>
#include <QThread>

class worker : public QObject
{
    Q_OBJECT
public:
    explicit worker(QObject *parent = 0);
    void setupAndRun();
signals:

public slots:
    void doWork();
    void onComplete();
private:
    // --- The thread that I use to movetothread with.
    // Is this ok?
    QThread *m_thread;
    bool stopped;
};

#endif // WORKER_H

Dialog push button

void Dialog::on_pushButton_clicked()
{
    m_worker = new worker();
    m_worker->setupAndRun();
}
  • 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-12T23:16:14+00:00Added an answer on June 12, 2026 at 11:16 pm

    I did not spot anything much at a glance, but something:

    Since m_thread is member variable, you should handle it a bit differently. You could initialize it to null in the constructor, and also set m_thread = null after calling m_thread->quit(), since it will get deleted shortly after and you do not want a dangling pointer.

    With this code, you should connect the finished signal to m_worker’s deleteLater() too, as it appears there is no other code to delete that. You should also take care of threads when application exits, for example with qAddPostRoutine, so that all threads terminate and all related objects get deleted.

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

Sidebar

Related Questions

I read this story on slashdot today where they announce a new parallel programming
I read that the new delegate method can attach more than one event to
I read that: Object sync = new Object(); /* inter to monitor */ try
I've read that Firefox 3.5 has a new feature in its parser ? Improvements
I am really new to Java and I read that synchronized is very expensive
I am new in web development, and read some wiki and discussions about MVC.
I read on OSDev wiki, that protected mode of x86 architecture allow you to
I'm doing the exercises in Stroustrup's new book Programming Principles and Practice Using C++
So I have read this - https://wiki.archlinux.org/index.php/Python And it is clear from this wiki
I have used the module Mail::Webmail::Gmail to read the new messages in my Gmail

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.