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. error When use moveToThread

error When use moveToThread

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.6k Views 3 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.
  • H Offline
    H Offline
    hikaris
    wrote on last edited by hikaris
    #1

    Hi, rencently I'm coding on a video processing program, and trying to move the time cost processing in to another thread, by subclassing QObject and move it to another thread by moveToThread.

    class VideoEncoder : public QObject
    {
        VideoEncoder();
    }
    

    Here's an another class called VideoController, which manages VideoEncoder:

    #include <QThread>
    #include "VideoEncoder.h"
    class VideoController : public QWidger
    {
        ....
    public:
        void init();
    private:
        QThread *encoderThread_;
        VideoEncoder *videoEncoder_;
    

    here's my moveToThread code.

    void VideoController::init()
    {
        videoEncoder_ = new VideoEncoder;
        encoderThread_ = new QThread;
        videoEncoder_->moveToThread(encoderThread_);  // Get Errors!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // cannot initialize object parameter of type 'QObject' with an expression of type 'VideoEncoder'
    }
    

    If I move the line of

    #include "VideoEncoder.h"
    

    from header file to cpp file,
    and add a forward declaration

    class VideoEncoder
    

    in header file
    The error disappears.

    Could someone help me through it. It really confuses me.
    微信截图_20201212152300.png

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by JohanSolo
      #2

      Not directly related to your question, but your VideoEncoder is a QWidget, and therefore must lie in the main thread. GUI objects must live in the main thread.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      H 1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @hikaris said in error When use moveToThread:

        The error disappears.

        Please show the full code. I would guess you create a circular include.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • JohanSoloJ JohanSolo

          Not directly related to your question, but your VideoEncoder is a QWidget, and therefore must lie in the main thread. GUI objects must live in the main thread.

          H Offline
          H Offline
          hikaris
          wrote on last edited by
          #4

          @JohanSolo nope, VideoEncoder is a QObject. VideoController is a QWidget, and it lives in main thread.

          kshegunovK 1 Reply Last reply
          0
          • H hikaris

            @JohanSolo nope, VideoEncoder is a QObject. VideoController is a QWidget, and it lives in main thread.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            Did you forget to add Q_OBJECT for VideoEncoder?

            Read and abide by the Qt Code of Conduct

            H 1 Reply Last reply
            0
            • kshegunovK kshegunov

              Did you forget to add Q_OBJECT for VideoEncoder?

              H Offline
              H Offline
              hikaris
              wrote on last edited by
              #6

              @kshegunov no, I did add Q_OBJECT for VideoEncoder, and also VideoController

              mranger90M 1 Reply Last reply
              0
              • H hikaris

                @kshegunov no, I did add Q_OBJECT for VideoEncoder, and also VideoController

                mranger90M Offline
                mranger90M Offline
                mranger90
                wrote on last edited by
                #7

                @hikaris After adding Q_OBJECT did you do a clean/run qmake/rebuild ?:

                1 Reply Last reply
                2
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Again: please ost your both headers. I'm pretty sure there is a circular include which prevents the compiler from knowing that your class derives from QObject - that's exactly what the error message tells you, nothing else.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  H 1 Reply Last reply
                  3
                  • Christian EhrlicherC Christian Ehrlicher

                    Again: please ost your both headers. I'm pretty sure there is a circular include which prevents the compiler from knowing that your class derives from QObject - that's exactly what the error message tells you, nothing else.

                    H Offline
                    H Offline
                    hikaris
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher you are right, thank you.

                    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