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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:00:03+00:00 2026-05-21T22:00:03+00:00

I have created a custom QObject class called EncodeThread, which looks as follows: class

  • 0

I have created a custom QObject class called EncodeThread, which looks as follows:

class EncodeThread : public QObject {
    Q_OBJECT

public:
    void set(SWSL::Video* v, QStringList f, QDir vDir);
    void run();

public slots:
    void encode();

signals:
    void encodeProgress(int i);

private:
    SWSL::Video* video;
    QStringList files;
    QDir videoDir;
};

As may be obvious, this class is used for encoding a video using an external library. Encode() contains the actual encoding routine, run() is a function I added while troubleshooting, though it’s obviously non-functional:

void EncodeThread::run() {
    if (currentThread() != this) {
        // caller is in different thread.
        QMetaObject::invokeMethod(this, "encode", Qt::QueuedConnection);
    }
    else {
        encode();
    }
}

The problem is when I use a QThread and the moveToThread() function on the EncodeThread instance, namely that nothing seems to happen. No data is written, and the instance never emits the signal which should save the encoded file to disk.

encThread.set(video, files, videoDir);
connect(&encThread, SIGNAL(encodeProgress(int)), cookVideoProgress, SLOT(setValue(int)));
    connect(&encThread, SIGNAL(finished()), this, SLOT(videoCookEnd()));
    connect(this, SIGNAL(videoEncode()), &encThread, SLOT(encode()));
encThread.moveToThread(&thread);
    thread.start();

The above is how the whole setup is started. EncThread and thread variables are declared in the MainWindow class. I have made the set() function of EncodeThread call encode() after attempts to call encode() from the main thread using signals and QMetaObject failed.

I’m not new to threading, having used native Windows and Linux threads, as well as those of various cross-platform implementations, but QThreads really seem to baffle me. Any suggestions are more than welcome 🙂

  • 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-21T22:00:04+00:00Added an answer on May 21, 2026 at 10:00 pm

    Probably way to late to be any help to you, but here’s a little demo program that puts an EncoderThread class to work. It probably doesn’t quite mesh with your design (which your question only had fragments of), but it demonstrates running an object instance on its own thread and wiring 2 objects on different threads via signals/slots to let them communicate:

    #include <stdio.h>
    #include <QObject>
    #include <QThread>
    #include <QtCore/QCoreApplication>
    
    // QSleeper is just a toy utility class that makes the
    //  protected QThread::sleep() family of functions
    //  publicly accessible.  It's only use is for demo
    //  programs like this
    class Sleeper : QThread
    {
    public:
        static void sleep(unsigned long secs) { QThread::sleep(secs); }
        static void msleep(unsigned long msecs) { QThread::msleep(msecs); }
        static void usleep(unsigned long usecs) { QThread::usleep(usecs); }
    
    };
    
    
    // an Encoder class that maintains itself on is own thread
    class EncodeThread : public QObject {
        Q_OBJECT
    
    public:
        EncodeThread();
    
    public slots:
        void encode();
    
    signals:
        void encodeProgress(int i);
        void finished();
    
    private:
        QThread myThread;
    };
    
    EncodeThread::EncodeThread() {
        moveToThread(&myThread);
        myThread.start();
    }
    
    
    void EncodeThread::encode()
    {
        printf("EncodeThread::encode() on thread %u\n", (unsigned int) QThread::currentThreadId());
    
        for (int i = 0; i < 6; ++i) {
            // encode for 1 second or so
            printf("EncodeThread::encode() working on thread %u\n", (unsigned int) QThread::currentThreadId());
            Sleeper::sleep(1);
            emit encodeProgress(i);
        }
    
        emit finished();
        printf("EncodeThread::encode() - done\n");
    }
    
    
    
    
    // a controller to manage and monitor an EncoderThread instance
    class VideoEncoderController : public QObject
    {
        Q_OBJECT
    public:
        void start();
    
    public slots:
        void setValue(int);
        void encodingDone();
    
    signals:
        void encodingBegin();
    };
    
    void VideoEncoderController::start()
    {
        printf("VideoEncoderController::start() on thread %u\n", (unsigned int) QThread::currentThreadId());
        emit encodingBegin();
    }
    
    void VideoEncoderController::setValue(int x)
    {
        printf("VideoEncoderController::setValue(int %d) on thread %u\n", x, (unsigned int) QThread::currentThreadId());
    }
    
    void VideoEncoderController::encodingDone()
    {
        printf("VideoEncoderController::encodingDone() on thread %u\n", (unsigned int) QThread::currentThreadId());
    }
    
    
    
    
    // a demo program that wires up a VideoEncoderController object to
    //  an EncoderThread
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        EncodeThread encThread;
        VideoEncoderController controller;
    
        QObject::connect(&encThread, SIGNAL(encodeProgress(int)), &controller, SLOT(setValue(int)));
        QObject::connect(&encThread, SIGNAL(finished()), &controller, SLOT(encodingDone()));
        QObject::connect(&controller, SIGNAL(encodingBegin()), &encThread, SLOT(encode()));
    
        printf("hello world on thread %u\n", (unsigned int) QThread::currentThreadId ());
    
        controller.start();
    
        return a.exec();
    }
    
    
    
    #include "main.moc"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created custom exception class public class Web2PDFException : Exception { public Web2PDFException(string
I have created a Custom preference which has the following constructor public CoordinatesPreference(Context context,
I have created custom aplication resource: class Custom_Entitymanager extends Zend_Application_Resource_ResourceAbstract{ //... public function init
I have created custom jQuery UI widget called uiPopover, very similar to UI-dialog (in
I have created custom cell in which there are n number of image views.
I have created a custom tag that looks like this: def textField = {
I have created my custom MembershipProvider. I have used an instance of the class
I have created a custom attribute that I am using on my class MyClass
I have created a custom class for my dates and need move dates forward
I have created a custom tab layout which look like this,I want to make

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.