Try this to start:
// 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);
}
// 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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks