PDA

View Full Version : change progress bar value from a thread



xbehzad
29th November 2016, 10:13
hi every one.
i write this cod.
how i can change progress bar value from my thread?
signal and slots?
how use signal and slots for this program??


main.cpp


#include "mainwindow.h"
#include <QApplication>
#include <QThread>
#include <QDebug>
#include <mainwindow.h>




int i ;













class Thread : public QThread
{
private:
void run()
{
for(i;i<=100;i++)
QThread::sleep(1);

}

};





int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();



//Thread();





qDebug()<<"From main thread: "<<QThread::currentThreadId();

Thread t;
QObject::connect(&t, SIGNAL(finished()), &a, SLOT(quit()));

t.start();


//--------------------


//---------------------------------------








return a.exec();

}









mainwindow.cpp:



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>





extern int i;






MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);


}

MainWindow::~MainWindow()
{
delete ui;

}

void MainWindow::on_pushButton_pressed()
{

// ui->progressBar->setValue(i);


//ui->progressBar->update();

}

anda_skoa
29th November 2016, 12:22
Yes, you can add a signal to your thread class and emit it whenever the thread needs to send new progress info.
Connect that to a slot in your main window.

Cheers,
_

xbehzad
29th November 2016, 15:58
Thnak you cheers:). but how i write a connection for this program?
please give me a simple cod

d_stranz
29th November 2016, 21:58
The Qt documentation (http://doc.qt.io/qt-5/signalsandslots.html) will teach you all about signals, slots, and connections.

xbehzad
30th November 2016, 10:59
Thank you m freinds. im in learning to write connection. i can write simple connection but for this program i thik much time and nothing come to my mind.
please give me a simple connect for this program:confused:

d_stranz
30th November 2016, 16:26
Try this to start:



// main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QThread>
#include <QDebug>
#include <mainwindow.h>

class Thread : public QThread
{
Q_OBJECT

signals:
void progress( int value );

private:
void run()
{
for(int i = 0; i <= 100; i++ )
{
emit progress( i );
QThread::sleep(1);
}
}
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

qDebug()<<"From main thread: "<<QThread::currentThreadId();

Thread t;
QObject::connect(&t, SIGNAL(finished()), &a, SLOT(quit()));
QObject::connect(&t, SIGNAL(progress(int)), &w, SLOT(onProgress(int)));

t.start();
return a.exec();
}

// mainwindow.h

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow( QWidget * parent );
~MainWindow();

public slots:
void onProgress( int i );

// ...
};

// mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::onProgress( int i )
{
ui->progressBar->setValue(i);
}