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. QThread and QObject
Qt 6.11 is out! See what's new in the release blog

QThread and QObject

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 4.8k 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.
  • W Offline
    W Offline
    wimschuiteman
    wrote on last edited by
    #1

    Hello

    I have a problem with Qthread. I would like to run a Functie of my Serial class in a thread. And start the thread in my mainwindow class.
    But when I run my application I get the following error:
    @Start Thread
    QThread: Destroyed while thread is still running
    The program has unexpectedly finished.
    Allflowlift.exe exited with code -1073741819@

    the Mainwindow
    @void MainWindow::Serial_Connect()
    {
    qDebug() << "Start Thread";
    QThread cThread;
    Serial sSerial;
    sSerial.Serial_Thread_Start(cThread);
    sSerial.moveToThread(&cThread);
    cThread.start();
    bSerialOn = true;
    }@

    The Serial.cpp:
    @Serial::Serial(QObject *parent)
    :QObject(parent)
    {
    }

    void Serial::Serial_Thread_Start(QThread &cThread)
    {
    connect(&cThread, SIGNAL(started()), this, SLOT(Test()));
    }

    void Serial::Test()
    {
    qDebug() << "Tread";
    }@

    The Serial.h

    @class Serial : public QObject
    {
    Q_OBJECT
    public:
    Serial(QObject *parent = 0);
    void Serial_Thread_Start(QThread &cThread);

    private slots:
    void Test();

    signals:

    };@

    Know someone maybe what I'm doing wrong?

    Regards,
    Wim

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You are declaring the thread on a stack. It goes out of scope on line 10. of the first snippet you've provided, and is destroyed. You should refactor that bit - either declare the thread in your MainWindow class, or use a pointer.

      (Z(:^

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wimschuiteman
        wrote on last edited by
        #3

        I have now set "QThread cThread;" in my mainwindow.h

        The applicatie run now but. The Thread run not the code of the Serial::Test() functie.

        The Serial.cpp
        @void Serial::Serial_Thread_Start(QThread &cThread)
        {
        connect(&cThread, SIGNAL(started()), this, SLOT(Test()));
        qDebug() << "Connect Thread";
        }

        void Serial::Test()
        {
        for(int i = 0; i < 100; i ++)
        {
        qDebug() << "Tread" << i;
        }
        }@

        Regards,
        Wim

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

          But you still did not follow sierdzio's advice to modify "Serial sSerial;", right?

          1 Reply Last reply
          0
          • W Offline
            W Offline
            wimschuiteman
            wrote on last edited by
            #5

            I have declare the thread in my MainWindow.

            @namespace Ui {
            class MainWindow;
            }

            class MainWindow : public QMainWindow
            {
            Q_OBJECT

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

            private:
            QThread cThread;
            Ui::MainWindow *ui;
            };@

            Regards,
            Wim

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

              Yes, I see, but your

              @
              Serial sSerial;
              @

              still "goes out of scope on line 10. of the first snippet you’ve provided, and is destroyed. "

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                modify
                @Serial sSerial;
                sSerial.Serial_Thread_Start(cThread);
                sSerial.moveToThread(&cThread);@
                to:
                @Serial* sSerial=new Serial(this);
                sSerial->Serial_Thread_Start(cThread);
                sSerial->moveToThread(&cThread);@

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                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