Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

Access a label from inside another thread

$
0
0
Currently i have code that start and stops a thread that counts to 10000. What i now what to do is pass the current number inside the QObject thread to the ui thread so that i can set the text for the label. I thought it might be using connect but i just can’t seemed to get it right. maybe something like this ? connect(this,SIGNAL(worker()),&ui,SLOT(ui->label->settext((QString::number(thread->timealive)))); If anyone can help or point me in the right direction or even better have a basic example so i can get my head round it. Code listed below: Dialog.h #ifndef DIALOG_H #define DIALOG_H   #include <QDialog>   namespace Ui { class Dialog; }   class Dialog : public QDialog {     Q_OBJECT     public:     explicit Dialog(QWidget *parent = 0);     ~Dialog();     private slots:     void on_pushButton_clicked();       void on_pushButton_2_clicked();       void on_pushButton_3_clicked();       void on_pushButton_4_clicked();       void on_pushButton_5_clicked();       void on_pushButton_6_clicked();   private:     Ui::Dialog *ui; };   #endif // DIALOG_H object.h #ifndef OBJECT_H #define OBJECT_H   #include <QObject> #include <QDebug> #include <QThread> #include <QApplication> #include <QMutex>   class Object : public QObject {     Q_OBJECT private:     int timealive; public:     explicit Object(QObject *parent = 0);     void setup(QThread &thread, QObject &object);     void set_time(int number);     int get_time();     bool Stop; signals:   public slots:     int worker(); };   #endif // OBJECT_H dialog.cpp #include "dialog.h" #include "ui_dialog.h"   Dialog::Dialog(QWidget *parent) :     QDialog(parent),     ui(new Ui::Dialog) {     ui->setupUi(this); }   Dialog::~Dialog() {     delete ui; }   void Dialog::on_pushButton_clicked() {     connect(&thread,SIGNAL(),this,SLOT()     ui->label->setText(QString::number()); }   void Dialog::on_pushButton_2_clicked() {     //this->Sto }   void Dialog::on_pushButton_3_clicked() {   }   void Dialog::on_pushButton_4_clicked() {   }   void Dialog::on_pushButton_5_clicked() {   }   void Dialog::on_pushButton_6_clicked() {   } main.cpp #include "dialog.h" #include "object.h"   int main(int argc, char *argv[]) {     QApplication a(argc, argv);     Dialog w;     w.show();       QThread thread;     Object thread_object;     thread_object.Stop = false;     thread_object.setup(thread,thread_object);       return a.exec(); } object.cpp #include "object.h" #include "dialog.h"   Object::Object(QObject *parent) :     QObject(parent) { }   void Object::setup(QThread &thread,QObject &object) {       object.moveToThread(&thread);       thread.start();     connect(&thread,SIGNAL(started()),this,SLOT(worker())); connect(this,SIGNAL(worker()),&ui,SLOT(ui->label->settext((QString::number(thread->timealive)))); }   int Object::worker() {     int i;     for(i = 0; i < 10000; i++)     {         QMutex mutex;         QMutexLocker locker(&mutex);         this->set_time(i);         qDebug() << i;         if(this->Stop)         {             break;         }     }     Dialog::     return i; }   void Object::set_time(int number) {    timealive = number; }   int Object::get_time() {     return timealive; } on the main form all there is, is a start, stop and a label repeated 3 times. If anyone has any ideas please let me know i’m very new to Qt so this has been quite confusing for me. Cheers Ion

Viewing all articles
Browse latest Browse all 13965