Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What is the problem with moveToThread() ?
Qt 6.11 is out! See what's new in the release blog

What is the problem with moveToThread() ?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    blue_sky
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Hi, please wrap your code with

      @@@

      and

      @@@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • B Offline
          B Offline
          blue_sky
          wrote on last edited by
          #4

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

          @@@

          and

          @@@[/quote]

          1 Reply Last reply
          0
          • B Offline
            B Offline
            blue_sky
            wrote on last edited by
            #5

            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]

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • B Offline
                B Offline
                blue_sky
                wrote on last edited by
                #7

                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]

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dbzhang800
                  wrote on last edited by
                  #8

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

                  The return value of connect is true or false?

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved