Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does the application crash when I try to start the same thread a second time?
    primarykey
    data
    text
    <p>main.cpp:</p> <pre><code>#include "mainwindow.h" #include &lt;QApplication&gt; int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } </code></pre> <p>mainwindow.cpp:</p> <pre><code>#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui-&gt;setupUi(this); cThread = new QThread(this); cObject = new MyObject(); cObject-&gt;moveToThread(cThread); QObject::connect(ui-&gt;pushButton_3, SIGNAL(clicked()), this, SLOT(close()) ); QObject::connect(cThread, SIGNAL(started()), cObject, SLOT(doWork()) ); QObject::connect(ui-&gt;pushButton_4, SIGNAL(clicked()), this, SLOT(runThreadSlot()) ); QObject::connect(cThread, SIGNAL(finished()), cThread, SLOT(deleteLater()) ); QObject::connect(cThread, SIGNAL(finished()), cObject, SLOT(deleteLater()) ); QObject::connect(cObject, SIGNAL(setStatusBarSignal(QString)), this, SLOT(setStatusBarSlot(QString)) ); } MainWindow::~MainWindow() { delete ui; } void MainWindow::runThreadSlot() { cThread-&gt;start(); } void MainWindow::setStatusBarSlot(QString text) { ui-&gt;statusBar-&gt;showMessage(text); } </code></pre> <p>myobject.cpp:</p> <pre><code>#include "myobject.h" MyObject::MyObject(QObject *parent) : QObject(parent) { } void MyObject::doWork() { emit setStatusBarSignal(QString::number((qint32) QThread::currentThreadId())); QThread::currentThread()-&gt;quit(); return; } </code></pre> <p>mainwindow.h:</p> <pre><code>#ifndef MAINWINDOW_H #define MAINWINDOW_H #include &lt;QMainWindow&gt; #include "myobject.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void runThreadSlot(); void setStatusBarSlot(QString); private: Ui::MainWindow *ui; QThread* cThread; MyObject* cObject; }; #endif // MAINWINDOW_H </code></pre> <p>myobject.h:</p> <pre><code>#ifndef MYOBJECT_H #define MYOBJECT_H #include &lt;QtCore&gt; class MyObject : public QObject { Q_OBJECT public: explicit MyObject(QObject *parent = 0); signals: void setStatusBarSignal(QString); public slots: void doWork(); }; #endif // MYOBJECT_H </code></pre> <p>So the pattern is:</p> <p>pushButton_4 clicked() ---> runThreadSlot() ---> cThread start()</p> <p>The thread immediately kills itself with <code>QThread::currentThread()-&gt;quit();</code>, but when I click on pushButton_4 again, the application crashes.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload