class MyThread : public QThread{ QTextEdit* textEdit; volatile int val; public: void set_textEdit(QTextEdit* p){textEdit=p;} void set_val(const int v){val=v;} void run();};class dlgTest: public QDialog, public Ui_dlgTest{ Q_OBJECT MyThread thread; public: dlgTest(QWidget *parent=0); public slots: void on_pbSetThread_clicked();};dlgTest::dlgTest(QWidget *parent):QDialog(parent){ setupUi(this); thread.set_textEdit(textEdit);} void dlgTest::on_pbSetThread_clicked(){ thread.set_val( spinBox->value() ); thread.start();}void MyThread::run(){ int x; for (int i = 0; i <val; ++i) { x=i*2; textEdit->insertHtml ("test №"+QString::number(x)+" completed"); } }
void MyThread::run(){ int x; for (int i = 0; i <val; ++i) { x=i*2; //textEdit->insertHtml ("test №"+QString::number(x)+" completed"); -- ERROR emit sig_toTextEdit_insertHtml("test №"+QString::number(x)+" completed"); -- GOOD } }
class MyThread : public QThread{ QTextEdit* textEdit; volatile int val; public: void set_textEdit(QTextEdit* p){textEdit=p;} void set_val(const int v){val=v;} void run(); signals: void sendData(const QString &s);};class dlgTest: public QDialog, public Ui_dlgTest{ Q_OBJECT MyThread thread; public: dlgTest(QWidget *parent=0); public slots: void on_pbSetThread_clicked();};dlgTest::dlgTest(QWidget *parent):QDialog(parent){ setupUi(this); thread.set_textEdit(textEdit); QObject::connect(&thread, SIGNAL( sendData(const QString &) ), textEdit, SLOT( insertHtml(const QString &) ), Qt::QueuedConnection );} void dlgTest::on_pbSetThread_clicked(){ thread.set_val( spinBox->value() ); thread.start();}void MyThread::run(){ int x; for (int i = 0; i <val; ++i) { x=i*2; sendData("test №"+QString::number(x)+" completed"); } }
void MyThread::run(){ int x; for (int i = 0; i <val; ++i) { x=i*2; emit sendData("test №"+QString::number(x)+" completed"); //ничего не меняет } }