What is the problem with moveToThread() ?



  • here is the simple code I am sharing. After running this..gives me
    <warning: Exception at 0x754c812f, code: 0x406d1388: Startup complete, flags=0x0 (first chance)>

    @
    main.cpp
    =======-
    #include "SafeThreadDesign.h"
    #include <QApplication>
    #include <QThread>
    #include <QDebug>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    SafeThreadDesign w;
    qDebug()<<"Inside main : thread Id is :"<<QThread::currentThreadId();
    w.show();

    return a.exec&#40;&#41;;
    

    }
    SafeThreadDesign.cpp

    #include "SafeThreadDesign.h"
    #include "ui_SafeThreadDesign.h"
    #include "QThread"
    #include "Worker.h"
    #include <QDebug>

    SafeThreadDesign::SafeThreadDesign(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SafeThreadDesign)
    {
    ui->setupUi(this);
    qDebug()<<"Inside constructor SafeThreadDesign : thread Id is :"<<QThread::currentThreadId();
    startNewThread();
    }

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

    void SafeThreadDesign::startNewThread()
    {
    qDebug()<<"Inside SafeThreadDesign::startNewThread() : thread Id is :"<<QThread::currentThreadId();
    t = new QThread( this );
    workerObj = new Worker( );
    workerObj->moveToThread( t );
    connect( t,SIGNAL(started()),workerObj,SLOT( startNewThread()), Qt::DirectConnection );
    //connect( workerObj,SIGNAL(fnshd()),t,SLOT(quit()) );
    t->start();
    }
    SafeThreadDesign.h
    ===================--
    #include <QMainWindow>
    #include <QThread>
    #include <Worker.h>
    namespace Ui {
    class SafeThreadDesign;
    }

    class SafeThreadDesign : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit SafeThreadDesign(QWidget *parent = 0);
    ~SafeThreadDesign();

    private:
    Ui::SafeThreadDesign ui;
    private slots:
    void startNewThread();
    public:
    QThread
    t;
    Worker* workerObj;
    };
    worker.cpp

    #include "Worker.h"
    #include <QDebug>
    #include <QThread>
    Worker::Worker()
    {
    }

    void Worker::startNewThread()
    {
    qDebug()<<"Inside starting : thread Id is :"<<QThread::currentThreadId();

    }
    worker.h

    #include <QObject>

    class Worker : public QObject
    {
    Q_OBJECT
    public:
    Worker();
    private slots:
    void startNewThread();
    };
    @

    Where is the problem?Please need help



  • Hi, please wrap your code with

    @@@

    and

    @@@



  • You are attempting a direct connection between two object which are not in the same thread. That's not possible.



  • Thank You
    [quote author="1+1=2" date="1385964173"]Hi, please wrap your code with

    @@@

    and

    @@@[/quote]



  • then how moveToThread() works..example?

    [quote author="Asperamanca" date="1385969870"]You are attempting a direct connection between two object which are not in the same thread. That's not possible.[/quote]



  • One example can be found "linked from this post":http://qt-project.org/forums/viewthread/14806



  • i tried without direct connection, same result..please suggest.

    [quote author="Asperamanca" date="1385969870"]You are attempting a direct connection between two object which are not in the same thread. That's not possible.[/quote]



  • Hi, can you make sure that your connection is successful?

    The return value of connect is true or false?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.