change progress bar value from a thread
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
Code:
#include "mainwindow.h"
#include <QApplication>
#include <QThread>
#include <QDebug>
#include <mainwindow.h>
int i ;
{
private:
void run()
{
for(i;i<=100;i++)
}
};
int main(int argc, char *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:
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
extern int i;
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_pressed()
{
// ui->progressBar->setValue(i);
//ui->progressBar->update();
}
Re: change progress bar value from a thread
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,
_
Re: change progress bar value from a thread
Thnak you cheers:). but how i write a connection for this program?
please give me a simple cod
Re: change progress bar value from a thread
The Qt documentation will teach you all about signals, slots, and connections.
Re: change progress bar value from a thread
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:
Re: change progress bar value from a thread
Try this to start:
Code:
// main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QThread>
#include <QDebug>
#include <mainwindow.h>
{
Q_OBJECT
signals:
void progress( int value );
private:
void run()
{
for(int i = 0; i <= 100; i++ )
{
emit progress( i );
}
}
};
int main(int argc, char *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
{
Q_OBJECT
public:
~MainWindow();
public slots:
void onProgress( int i );
// ...
};
// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onProgress( int i )
{
ui->progressBar->setValue(i);
}